Releasing Dynamic sprites from memory

Started by Wonkyth, Mon 07/09/2009 08:08:56

Previous topic - Next topic

Wonkyth

In  the manual, for all the CreateFrom functions it say this:

"IMPORTANT: If the DynamicSprite instance is released from memory (ie. there is no longer a DynamicSprite* variable pointing to it), then the sprite will also be removed from memory. Make sure that you keep a global variable pointer to the sprite until you are finished with it, and at that point call Delete."

I'm not quite sure what it means here.
Does it just mean that if I release it, it's lost?
"But with a ninja on your face, you live longer!"

NsMn

That's a warning in case you have the Pointer declared within a function or room script, I believe.

Wonkyth

"But with a ninja on your face, you live longer!"

Khris

#3
Quote from: NsMn on Mon 07/09/2009 08:11:02
That's a warning in case you have the Pointer declared within a function or room script, I believe.

Variables declared in room scripts (but outside functions) retain their value, so that shouldn't be a problem.

SSH

They retain their value when you come back to the room, but I'm not sure the reference counts when it comes to deallocating sprites...
12

Wonkyth

So, as long as each room has all the dynamic sprites it needs, then I shouldn't run into problems?
I'm afraid that your post confused me a bit, SSH. :-\
"But with a ninja on your face, you live longer!"

monkey0506

#6
I think SSH's point was that he, like I, wasn't clear* if the DynamicSprite is allocated from the room script then when that room is unloaded (the player leaves that room) whether* the DynamicSprite* will be unloaded from memory as well. Because the pointer is being removed from memory (i.e., it is not a global pointer) then the sprite itself would be removed as well.

If you have a global pointer pointing to that sprite then of course it wouldn't be automatically removed (because there is a pointer pointing to it). But if the only DynamicSprite* pointing to that sprite is local (whether to a function or a room script) then that pointer is going to be deallocated and thereby so will the sprite be removed as well.

*NOTE: As denoted by CJ below, this isn't actually the case. But if the pointer is local to a function then it will get deallocated and the sprite would be deleted.

Wonkyth

"But with a ninja on your face, you live longer!"

Pumaman

Quote from: monkey_05_06 on Tue 08/09/2009 01:37:29
I think SSH's point was that if the DynamicSprite is allocated from the room script then when that room is unloaded (the player leaves that room) the DynamicSprite* will be unloaded from memory as well. Because the pointer is being removed from memory (i.e., it is not a global pointer) then the sprite itself would be removed as well.

Just to clarify, this is not the case.

Any variables declared outside of functions in a room script will be maintained in memory.

Therefore it is possible to create a memory leak if you (for example) create a large dynamic sprite in a room, and then leave the room without deleting it and the player never comes back to that room.

monkey0506

#9
Okay, I stand corrected. I assumed that with the variable being removed from memory it wouldn't be considered as a valid pointer anymore. ::) But of course as long as it's a state-saving room then the value of the pointer would be retained and there shouldn't be an issue.

Would problems arise in non-state-saving rooms though?

Pumaman

Interesting point, actually this might be a memory leak situation if you have a pointer declared in the room script of a non-state-saving room; it could well be that this pointer will never get freed. It's something I need to check at some point.

SMF spam blocked by CleanTalk