Subsequent to this new convention I have been doing some more computer programming and have come to learn that in (Safe)free()-ing variables there is a difference between ones that are "automatically" allocated and ones that are "dynamically" allocated. I am told that the difference is that the former are allocated on the stack, the latter on the heap. Apparently you can only free() dynamically allocated ones on the heap; doing it on the other ones triggers the error. I guess I am doing that inside the destructor. In any case, commenting out the Safefree makes the error go away, for better or worse.
| [reply] |
Apparently you can only free() dynamically allocated ones on the heap
AIUI, when you create using Newx(), you are allocating dynamically - and you'll leak memory if you don't Safefree() those dynamically allocated variables when they go out of scope.
Incidentally, DESTROY() doesn't have to be a perl sub.
You could dispense with that perl sub and place it in the C code as something like (untested):
void DESTROY(Node* self) {
warn("destroying self");
Safefree(self);
}
However, you may well find that there are additional things to Safefree (again untested):
void DESTROY(Node* self) {
warn("destroying self");
Safefree(self-> parent);
Safefree(self-> sv);
Safefree(self);
}
Admittedly, you've then lost the identification of the actual object that's being Safefree'd - though I think that could be easily addressed.
And I'm not sure how the self-referential nature of the Node object impacts upon things.
Cheers, Rob | [reply] [d/l] [select] |
| [reply] |