If I then remove the '#' so that the length of $raw is evaluated, the script then outputs "1 " - which is as I would expect.use strict; use warnings; use GD; for(1 .. 1) { x(100, 100); print $_, " "; } sub x{ my $im = GD::Image->new( @_); my $raw = $im->gd; my $im2 = GD::Image->newFromGdData( $raw ) or die "$!, $^E"; my $raw2 = $im2->gd; # my $len = length($raw); if($raw ne $raw2) {print length($raw), " ", length($raw2), " ERROR + "} }
Note that DESTROY() prints "Destroying " - just so you can see that/when it's being called. It's probably less confusing if you comment that out.use warnings; package my_gd; use Inline C => Config => BUILD_NOISY => 1, LIBS => '-LD:/gd/gdwin32 -lbgd', INC => '-ID:/gd/gdwin32/include'; use Inline C => <<'EOC'; #include <stdio.h> #include <gd.h> SV * new(int x, int y) { gdImagePtr * image; SV * obj_ref, *obj; New(1, image, 1, gdImagePtr); if(image == NULL)croak("Failed to allocate memory in new()"); obj_ref = newSViv(0); obj = newSVrv(obj_ref, "my_gd"); *image = gdImageCreate(x, y); sv_setiv(obj, (IV)image); SvREADONLY_on(obj); return obj_ref; } SV * gdgdImageCreateFromGdPtr(SV * data, int len) { gdImagePtr * image; SV * obj_ref, *obj; New(1, image, 1, gdImagePtr); if(image == NULL)croak("Failed to allocate memory in gdgdImageCre +ateFromGdPtr()"); obj_ref = newSViv(0); obj = newSVrv(obj_ref, "my_gd"); *image = gdImageCreateFromGdPtr(len, SvPV(data, len)); sv_setiv(obj, (IV)image); SvREADONLY_on(obj); return obj_ref; } SV * gdgd(SV * image) { void * data; int size; SV * t; data = gdImageGdPtr(*((gdImagePtr *)SvIV(SvRV(image))), &size); t = newSVpv((char*) data, size); gdFree(data); return t; } void DESTROY(SV * image) { printf("Destroying "); Safefree((gdImagePtr *)SvIV(SvRV(image))); } EOC $| = 1; for(1 .. 10) { x(100, 100); print $_, " "; } sub x{ my $im = new(@_); my $raw = gdgd($im); my $im2 = gdgdImageCreateFromGdPtr($raw, length($raw)); my $raw2 = gdgd($im2); # print length($raw), " "; if($raw ne $raw2) {print "ERROR"} }
In reply to Re^9: Segfault on second (identical) call to a sub
by syphilis
in thread Segfault on second (identical) call to a sub
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |