SOLVED Why do I have to assign a value to a variable twice when using sprite_delete ?

otterZ

Member
Thanks to Yal pointing me in the right direction I'm now externally loading PNG images and all is seemingly well. However, I don't know why but I have to declare a variable and assign it twice when adding an external PNG, then using sprite_delete, once in the create event and once again in the destroy event.

If I just declare and assign a variable once and not again in the destroy event an error pops up saying that the external PNG image no longer exists. I wonder if GM2 automatically deletes the externally loaded PNG after it is used? I simply don't know. Here's an example of the coding:

GML:
// In the create event
spr = sprite_add("external_png",1,0,0,0,0);

// In the destroy event
spr = sprite_add("external_png",1,0,0,0,0); //assigning it twice
sprite_delete(spr);
In the manual it says to make sure to use sprite_delete to delete an externally loaded sprite/image or you could run into memory problems, so I want to make sure that it is deleted. I am using Gamemaker Studio 2.
 

curato

Member
make sure you have the scope right for spr. if you declared it var spr then it wouldn't be referable out side of the the event you are in. If you declare it without the var then it should be available within that instance of the object.
 

otterZ

Member
Thanks curato, yes I declared it as spr and not var spr, so the variable's scope should extend to all events within that object/instance . . . I'm assuming.
Good question though as I could have been using a local variable, but in this case definitely an instance variable.
 

sylvain_l

Member
GML:
// In the create event
spr = sprite_add("external_png",1,0,0,0,0);

// In the destroy event
spr = sprite_add("external_png",1,0,0,0,0); //assigning it twice
sprite_delete(spr);
bad idea, that should lead to a memory leak.
you realize that if your assign a new sprite to your spr, the sprite deleted isn't the one assigned in the create event but only the one created in the destroy event.

if the spr isn't anymore available in the destroy event you should check why, but clearly not create a dummy one.
 
However, I don't know why but I have to declare a variable and assign it twice when adding an external PNG, then using sprite_delete, once in the create event and once again in the destroy event.
As @sylvain_l has pointed out, you are literally doubling your memory leak by doing this. My guess would be that somewhere along the line you are assigning the spr variable to something else, and thus losing the reference the created sprite.
 

otterZ

Member
bad idea, that should lead to a memory leak.
you realize that if your assign a new sprite to your spr, the sprite deleted isn't the one assigned in the create event but only the one created in the destroy event.

if the spr isn't anymore available in the destroy event you should check why, but clearly not create a dummy one.
As @sylvain_l has pointed out, you are literally doubling your memory leak by doing this. My guess would be that somewhere along the line you are assigning the spr variable to something else, and thus losing the reference the created sprite.
Thank you Sylvian_1 and RefresherTowel, this makes sense as I was assigning the variable value in spr to a global variable as in: global.variable = spr; This probably explains why I'm losing the reference.
I will re-code it in a way where that's not going to happen. Thanks again.
 

TsukaYuriko

☄️
Forum Staff
Moderator
This is tagged as solved, but allow me to expand a bit on what's been said and maybe provide a push in the right direction...

How does the global variable come into play here? What is it used for? Maybe there's an alternative way to handle what you're trying to achieve there, reducing the overall complexity of the system and simplifying further maintenance work on it.

Also, even if you remove the sprite_add duplication, you still have a potential memory leak. You have sprite_delete in the Destroy event. This event is only called when the instance is explicitly destroyed via instance_destroy or instance_change, but not when you leave the room. Therefore, if you leave the room without destroying the instance, you have a memory leak. This code should be in the Clean Up event, as that gets called not only when you explicitly destroy the instance, but also when it is implicitly removed from the instance list while changing rooms.
 

Roldy

Member
However, I don't know why but I have to declare a variable and assign it twice when adding an external PNG, then using sprite_delete, once in the create event and once again in the destroy event.
I'd suggest you post your actual code. You are most likely just over looking something.
 

otterZ

Member
Maybe there's an alternative way to handle what you're trying to achieve there, reducing the overall complexity of the system and simplifying further maintenance work on it.
Thank you TsukaYuriko, using sprite_delete in the clean up event, rather than the destroy event makes absolute sense. I will do this.

Simplifying the code / system is where I'm at right now. I have started from scratch and will come up with a more efficient way. I just need to test out some different ways and see if they work okay.

Good advice, much appreciated.

I'd suggest you post your actual code. You are most likely just over looking something.
Thanks Roldy, I will first of all re-write the code / system more efficiently, then if I encounter any set backs I will post up the entire code used in a separate post. I agree that it is easier if I post up the code as something overlooked can then be picked up upon.
 
Top