in reply to How to keep C variables "alive" between invocations with Inline::C
You can set the buffer of a scalar to the C struct, and set SvLEN to zero so Perl doesn't try to free it. Doing almost anything to that scalar will replace the buffer, though, so you might want to have another reference to the C struct somewhere so you can properly free it. But if you're just trying to pass it to your own Perl code, it shouldn't be a problem. See Re: Writing to Pointer Values (PV) in XS.
If you want Perl to free it, allocate the struct using Perl's memory functions and set SvLEN appropriately.
In both cases, no destructor is going to be called. That's why I think you might want to use an object on the Perl side. It doesn't look like you want to provide direct access to the internal details of the struct to the Perl code, so I don't see why you'd want to avoid using an object.
Update: Added second and third paragraphs.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to keep C variables "alive" between invocations with Inline::C
by Limbic~Region (Chancellor) on Sep 27, 2010 at 21:16 UTC | |
by ikegami (Patriarch) on Sep 27, 2010 at 22:50 UTC | |
by syphilis (Archbishop) on Sep 28, 2010 at 08:42 UTC |