In Perl, I have a blessed scalar reference which contains an IV that is
the pointer to a C struct which contains the data for the object.
I've added a method which calls realloc() to reallocate more space, and
update the IV appropriately:
copy = self;
if ((self = (Node*) realloc(self, (size_t) SIZE(count+num)))
== NULL)
croak("cannot add another child: realloc failed");
if (copy != self) {
SvREADONLY_off((SV*)SvRV(n));
sv_setiv((SV*)SvRV(n), (IV) self);
SvREADONLY_on((SV*)SvRV(n));
}
In testing, this crashes.
However, when using Devel::Peek, if following the method call I add
a call to dump the object:
$x->_add_children;
Dump($x);
it does not crash. Everything runs fine, the tests pass, etc. (Well,
almost; it's crashed once in dozens of tries.)
Comparing the results of Dump() before and after show just that the IV
has changed (which is what I expect).
I've tried increasing reference counts or using malloc/memcpy manually,
but still get the same errors. My amateurish guess is that the new
memory is being deallocated.
I'm using perl 5.8.7, compiled with MS Visual C++ Toolkit 2003 (the free
version).