Discussion:
problem with newSViv and changing int value
(too old to reply)
Eric Kort
2008-01-28 22:06:55 UTC
Permalink
Unfortunately, when I attempt to recreate the error I am about to describe in a tidy little function, I can't. But within the larger context of my image processing routine, I am encountering the following strangeness:

I am trying to update an element of an AV using av_store, and all is well if I do this:

int newval;
...
for(y = 1; y < iGridHeight - 1; y++) {
for(x = 1; x < iGridWidth - 1; x++) {
newval = 12;
av_store(avGrid, y * iGridWidth + x, newSViv(newval));
}
}

But if I try to increment newval, it dies after 10 iterations (and always after 10 iterations):

int newval=12;
...
for(y = 1; y < iGridHeight - 1; y++) {
for(x = 1; x < iGridWidth - 1; x++) {
newval++;
av_store(avGrid, y * iGridWidth + x, newSViv(newval));
}
}

With this kind of error:

*** glibc detected *** perl: munmap_chunk(): invalid pointer: 0x00007fff7f692410 ***
======= Backtrace: =========
/lib64/libc.so.6[0x2b472ba4739e]
...

Sadly, when I break this out into a little demo function, it runs fine:

void worksFine (SV* sv_ar)
{
AV* av_ar;
int i, newv=2;

av_ar = (AV*)SvRV(sv_ar);

for(i=0; i<1000000; i++) {
newv++;
av_store(av_ar, 1, newSViv(newv));
}
return;
}

Nevertheless, I wonder if you have any insights into what is going wrong (or does the situation dependency of my problem suggest I am doing something evil with my memory management elsewhere?)

Thanks,
Eric
This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient(s) please contact the sender by reply email and destroy all copies of the original message. Thank you.
Nicholas Clark
2008-01-29 10:16:25 UTC
Permalink
Post by Eric Kort
*** glibc detected *** perl: munmap_chunk(): invalid pointer: 0x00007fff7f692410 ***
As you seem to be on Linux, have you tried using valgrind on the offending
program? The cause might actually be somewhere completely different, and
often it is helpful in finding it.

Nicholas Clark
Eric Kort
2008-01-29 15:11:24 UTC
Permalink
Sent: Tuesday, January 29, 2008 5:16 AM
To: Kort, Eric
Subject: Re: problem with newSViv and changing int value
Post by Eric Kort
Unfortunately, when I attempt to recreate the error I am about to
describe in a tidy little function, I can't. But within the larger
context of my image processing routine, I am encountering the
*** glibc detected *** perl: munmap_chunk(): invalid pointer: 0x00007fff7f692410 ***
As you seem to be on Linux, have you tried using valgrind on
the offending program? The cause might actually be somewhere
completely different, and often it is helpful in finding it.
I assumed it would be hard/impossible to use valgrind on XS code run by a perl script.

Turned out to be easy (valgrind --leak-check=yes --trace-children=yes perl test.pl)

And yes, the problem was memory mismanagement remote to the place where the problem finally reared its ugly head.

Thanks,
Eric
Nicholas Clark
This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient(s) please contact the sender by reply email and destroy all copies of the original message. Thank you.
Loading...