07-23-2016, 08:55 PM
(07-21-2016, 06:02 PM)Rick Johnson Wrote: This code seems to work, but I'm flying by the seat of my pants here. The docs say we're responsible for the memory used by the ArtVector; does the clear() function suffice for aforementioned memory management? I saw in hdicoreMemory.h a template (I've never used one of those before!) that deletes each object pointed to in the vector, then performs a .clear(). Isn't the ArtVector returned by matchingArt, etc., analogous to an address list of the relevant objects, rather than complete copies of them? I'd be most grateful if someone can keep me out of trouble with this...
The vector stores pointers to Art objects, which were dynamically allocated on the heap. As such, by simply clearing the vector, you are leaking the memory being pointed to by the elements in the vector. The templated functions in hdicoreMemory.h are exactly the right tool for this circumstance. Simply pass "vMatches" to the hdi::core::cleanupVector() function and it takes care of everything.
With regards to removing individual elements from a vector, your sample code is very close. However, the iterator object is no longer valid after you have erased the element to which it refers, and so the behavior of the overloaded "++" operator in the "for" loop is undefined. As such, make a copy of the iterator, call the overloaded "++" operator on the original, and then pass the copy to the erase method. Don't forget to delete the memory for the Art pointer before erasing the element, and don't forget that the call to the "++" operator in the "for" loop will mess with this situation, so I recommend switching to a "while" loop.
Otherwise, you could simply create a secondary vector of Art pointers, and push the desired elements from "vMatches" into it. Then, after you have processed the elements in the secondary vector, clear it, and call hdi::core::cleanupVector() - passing "vMatches".