Is it neccessary to clear a list before...

BlitzMax Forums/BlitzMax Beginners Area/Is it neccessary to clear a list before...

sswift(Posted 2006) [#1]
Losing the pointer to it? I'm 99% sure the answer is no, but I want to be sure. It's possible there's a circular linkage in the inner workings that prevents it from being freed.


Grey Alien(Posted 2006) [#2]
I clear then free out of paranoia.
BUT I don't go into each link and kill each type. Is *THAT* OK I wonder?


H&K(Posted 2006) [#3]
Im sure its been posted here that one of the times the GC fall down is self referanced Tlists.

The only way to be sure would be to test it.

If its a non-time critical event, then I would be tempted to loop throu the whole list and Null all the links. But as Grey has implied, mabe thats just double paranoia


sswift(Posted 2006) [#4]
I don't see any reason why you should need to clear the types manually, unless you're talking sprites from my sprite system in a list since you have to free those to have them be truly freed cause they're in their own list.

But the lists I was just worried about whether the links in the list have pointers back to the list. But I don't think Mark did something like that.

This memory management stuff is so damn confusing. It was so much easier to be sure stuff was freed properly when I had to do it manually. I have to recurse ten levels back in my code while keeping everyhting in my head to try to figure out that A being freed causes B to be freed which causes C and D to be freed, which causes E to be freed which is a sprite attached to a path so I have to make sure E also gets a free() function called for it so it is removed from it's own internal list, which causes F which is a path to be freed...

It's just freaking nuts. I'm afraid to touch my own code! And you KNOW how well I've documented my code.

Anyway I think my latest change to my game and sprite systme didn't break anything. I hope. Hell I don't even know if what I fixed actually needed fixing. I just fixed it to require you to free paths manually because I could no longer be confident they were being freed automatically no matter how much I stared at the code and tried to follow the processes. So this should be safer.


tonyg(Posted 2006) [#5]
I think the answer is 'It depends' but I might have got something wrong here as I don't understand what's going on.
This seems to work 'as expected'...

with the clear not needed.
This seems not to release the memory for each instance...

unless you uncomment the 'clear'.


Dreamora(Posted 2006) [#6]
tonyg: the second example works as expected as well.
Reason it is not freed is that the list is local to the app scope and local are not directly handled by the GC. So there is no guarantee on when they are actually freed.

make it a global and it won't bug you anymore :-)


tonyg(Posted 2006) [#7]
Hmmm... 100% sure memory wasn't being released in that second code. However, I changed it to global and, you're right, memory was released.
I then did a file/new, copy/pasted the code and, with the local, it's releasing the memory.
I can only say it's late on a Friday.


Grey Alien(Posted 2006) [#8]
hmm, say I have a type which has TImages and TSounds in it (as fields) for example, then I add a load of these types to a list. If I call list.clear, each of the types' pointers will get set to null (and later GCed) but what about the pointers to the TImages and TSounds that are part of the types? Or do I need a specific free method in the type that nulls the TImage/TSound pointers? Thanks.


Dreamora(Posted 2006) [#9]
TImage will be freed, there are actually no problem. (list.clear -> all instances in the list get freed if no other references are present. so if the type with the image is freed, the image will be as well)

TSound: In that case I would add stopchannel etc to the delete method of the type you put into the list, to make sure the sound is correctly finished. Don't know if this really is needed or if a sound is stopped etc on its own. But so far it seems like sounds have been the largest "leakage" and crash problem when trying to play to many channels etc.


Grey Alien(Posted 2006) [#10]
Dreamora: Thanks. So it's safe to call list.clear even if the types have images (and other types) contained in them, cool. That's what I'd hoped anyway. As for the TSound info thanks.