Advertisement

CScriptArray memory leak on inserted items from C++ side?

Started by March 21, 2019 01:56 AM
4 comments, last by Zelerin 5 years, 8 months ago

Hello!  I'm experiencing a couple of memory leaks and would like to see if anyone knows the correct way to clean them up.

The second leak concerns an issue with cscriptarray (version 2.33.0).  Basically, I'm finding when I do something like this (result is of type CScriptArray, and AEntity is a custom pass-by-reference that will register with the garbage collector upon construction):


result->InsertLast(new AEntity(engine_ptr, *iter));

When this line is ran, AEntity is inserted as expected, but when the script ends, Valgrind reports the AEntity instances are not cleaned up, but the CScriptArrays are.  AEntity instances outside CScriptArrays are cleaned up fine.  I'm thinking I'm doing something wrong, but am not sure what.  If I just missed something in the documentation, feel free to tell me and I'll go recheck.  Does anyone have any ideas what I'm doing wrong?  Thanks in advance for the help!

InsertLast will add one reference to reference counted objects. Assuming AEntity starts off with a reference count of 1 you're leaking the reference by not doing Release() after adding it to the array.

Advertisement

This fixed both my problems.  Thanks for your quick response!

A followup question:  In these situations, where I know I don't want to hold a reference to the object on the C++ side, is it safe to have the GC count start out as 0 when it registers with the engine?  Or does it need to be 1 at first to prevent any weird GC problems, and then be decremented after?  I'd like to automate this as much as possible behind constructors to keep the problem from returning.

The reference count should always be valid, otherwise you could end up with edge cases where objects have a count of 0 while being treated as valid. That could cause crashes.

When you put it that way, it makes complete sense.  Easy enough to do, and at least it sticks out in Valgrind if done wrong.  Thanks a lot for your help!  This is something I can document pretty easily as 'rules for working with AngelScript' in my code.  Oddly enough, I've found just about everything else in AngelScript easy to work with.

This topic is closed to new replies.

Advertisement