Return an array of strings
You realize that those two lines do different things, right? The first one assigns to each element in the array. The second one inserts an additional elements.size() new elements to the array.
As the array is holding handles, i.e. pointers to objects, the InsertAt() is really expecting a pointer to the pointer to the object. It will dereference the pointer before calling the AddRef() method, which is why it was crashing.
However, as SiCrane mentioned, InsertAt() will actually increase the array to make room for the element, so you would have ended up with an array twice the size you wanted.
As you seem to have allocated the array with the expected size already, the correct is to do what you ended up doing, i.e.
The array will call Release() on the handles when destroyed though. Do you use reference counting on the Elements, or is the Release() behaviour just a dummy function? If you are using reference counting you need to make sure you update the reference count correctly.
However, as SiCrane mentioned, InsertAt() will actually increase the array to make room for the element, so you would have ended up with an array twice the size you wanted.
As you seem to have allocated the array with the expected size already, the correct is to do what you ended up doing, i.e.
*((Element **)arr->At(n++)) = *it;
The array will call Release() on the handles when destroyed though. Do you use reference counting on the Elements, or is the Release() behaviour just a dummy function? If you are using reference counting you need to make sure you update the reference count correctly.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
Yes, I'm using reference counting and Release() is in fact a proper method so I've added a call to increase the ref count before inserting the element into the array. And ye, I should have realized that insertAt has to take a pointer to the pointer in my case, my bad. This is what sometimes happens when you code late at night
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement