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.


In reply to Trying dirty trick with Imager and Inline C, but having difficulties. Can you help? by vr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.