Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^7: Wrapping a C shared library with Perl and XS

by Anonymous Monk
on May 08, 2017 at 12:37 UTC ( [id://1189794]=note: print w/replies, xml ) Need Help??


in reply to Re^6: Wrapping a C shared library with Perl and XS
in thread Wrapping a C shared library with Perl and XS

Sorry for nudging you again, but I think that this needs to be fixed, because this tutorial is intended for people with little knowledge of XS and C, and memory management is one of the aspects in which Perl differs greatly from C. It's quite easy to imagine someone being too used to reference counting of Perl to think that in C, allocated memory should be manually returned to OS.

Maybe it's just me, bitten by segfaults, memory corruption and leaks almost every time I write in plain C. And tutorial code is inevitably copied without much incentive to change it, since it produces the right output...

And the fix is simple: a free(c_array); inserted between the for (i=0; i<3; i++) {/*...*/} loop (which uses the pointer) and inline_stack_done; (which cleans up at the end of void _arr). A real world library in place of xswrap example would probably document that the pointer returned by unsigned char * arr(void) fully belongs to the user and needs to be freed after use.

Replies are listed 'Best First'.
Re^8: Wrapping a C shared library with Perl and XS
by stevieb (Canon) on May 08, 2017 at 14:42 UTC

    Thanks for the reminder... I had completely forgot about this. Your recommended fix has been edited into the post.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1189794]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found