> all methods that return an iterator return nil on an empty TSet.
Yes, TSet does do that.
>@dbannon, before freeing memory allocated for an item, it should be removed from the container. It seems that this code does not cause memory leaks: ....
Thank AVK. I may have confused you with my previous post. The snippit I posted that leaks does so because I set a pointer to point to the record using S.Min.GetData, not because of an incomplete dispose. It appears that calling S.Min.GetData always creates a new iterator using the create statement in the Min function. Min itself is not a creator but it calls create and returns the result.
So, easy answer, every call to S.Min requires a Free and just
don't do things like -
dispose(S.Min.GetData);
//or
p := S.Min.GetData;
In both the above cases, you loose track of the iterator so cannot free it.
So, the right way to free/dispose, yes, I see what your suggestion is trying to do, makes sense. But my version works fine, no leaks and no crashes and a shorter code and ever so slightly quicker. To avoid confusion, here it is again -
it := S.Min;
repeat
p := it.GetData;
dispose(p);
until not it.Next;;
it.Free;
S.Free;
Create (just the one) iterator, call its GetData function to get a pointer to a record, dispose the record and repeat until the iterator's Next function is false. Then free the iterator and then the TSet itself. I am not removing the pointer from the TSet, just disposing of what ever its pointing to. And heattrc tells me that I have no leaks.
I have been putting quite a lot of data through the TSet in the last hour or so, it does seem quick to me. But I have not yet tried the AVL version to compare.
QUESTION : If I write up this wiki page, do I (ie 'we') say we don't recommend people use this unit because it has a few unexplained behaviors ?
EDIT : I also find it a bit strange that you need to free and recreate that iterator every time you use it for something different. There is no resetting it back to Min because, as mentioned, that "creates" a new one.