vr has asked for the wisdom of the Perl Monks concerning the following question:
At some point in my program, it seems that easiest (fastest, etc.) thing to do is to directly replace raw image data with data from Perl scalar. Needless to say, Imager API (both Perl and C) don't provide for this, but I thought I'd get away with following subroutine (here it is with a test program that, of course, makes no sense):
use strict; use warnings; use Imager; use Inline with => 'Imager'; use Inline C => << 'END_OF_C'; void _replace( Imager img, SV* str ) { unsigned char* ptr = SvPV_nolen( str ); myfree( img-> idata ); img-> idata = ptr; } END_OF_C my $i = Imager-> new( xsize => 99, ysize => 99 ); for ( 'A' .. 'F' ) { print "$_\n"; my $s; $i-> write( data => \$s, type => 'raw' ); my $j = $i-> copy(); # Here, it's rumored, some nasty things # happening, that involve $j and $s. # But maybe nothing happens. _replace( $i, $s ); } __END__ ~$ perl test.pl A B *** Error in `perl': double free or corruption (!prev): 0x0000000002d6 +7990 *** Aborted (core dumped)
Unfortunately, my C is below-zero level. I understand I should prevent Perl from freeing memory that now is Imager responsibility, but don't know how. And, actually, replacement itself works OK. There's only error freeing memory, that happens just sometimes. This program seems to reproduce it always.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Trying dirty trick with Imager and Inline C, but having difficulties. Can you help?
by Anonymous Monk on Aug 01, 2015 at 09:08 UTC | |
by vr (Curate) on Aug 01, 2015 at 11:28 UTC | |
by Anonymous Monk on Aug 02, 2015 at 07:50 UTC | |
by vr (Curate) on Aug 02, 2015 at 21:24 UTC |