Legacy GM string_insert causing memory leaks in GMS?

S

Silversea

Guest
My game was experiencing a memory leak recently and by removing all other objects and code, I've narrowed the problem down to this:

Code:
//Draw Event
//draw nmbv with 3 digits, adding 0 to front if number < 100

sbno = nmbv;

repeat(2)
{
if string_length(string(sbno)) < 3
sbno = string_insert("0",sbno,0);
}

draw_text(x+30,y-20,sbno);
Thing is, I can't think of any possible reason why this would be happening. Is there something with string_insert that is raising the memory?

The memory use increases at a rate of 256.06KB every 5 seconds, and the additional memory load does not disappear even when the objects are deleted, or rooms are changed. I've left the game alone for 2 minutes and the file use has gone from 12MB to 20MB. And it stays at 20MB until the next time this object is encountered, and then it continues rising.
 

Alexx

Member
Try changing to:
Code:
var sbno = nmbv;
How does that effect the memory usage?

A side note: Good practice to only have drawing code in a drawing event. Move other code to a step event or other where possible.
 
S

Silversea

Guest
Try changing to:
Code:
var sbno = nmbv;
How does that effect the memory usage?

A side note: Good practice to only have drawing code in a drawing event. Move other code to a step event or other where possible.
No effect.

I'm definitely aware that non-drawing code in the Draw event isn't generally optimal, but I'm not sure if there is an alternative here for what I'm doing. I have a lot more code run in the draw event that is not impacting the memory at all, so I don't know if that is the specific issue.

The memory leak is definitely in these four lines below. So is there some other way to accomplish this that might not have a leak issue?

Code:
repeat(2)
{
if string_length(string(sbno)) < 3
 sbno = string_insert("0",sbno,1);
}
 
Last edited by a moderator:
S

Silversea

Guest
Ok, I guess I "fixed" this problem by moving the code elsewhere, and storing the string in a local array. So then I could use that in the draw event instead of making it there.

Still this seems like an unintentional effect of string_insert, unless I'm missing something.
 
Last edited by a moderator:

Alexx

Member
That was my 2 cents.

You can try something like:
Code:
score_text=string(score);
while string_length(score_text)<6 score_text="0"+score_text;
Or something like:
Code:
score_text=string(score);
score_length=string_length(score_text);
size=6-score_length;
for (i = 0; i < size; i += 1)
{
    score_text="0"+score_text;
}
Let me know if this changes the memory issue.
 
S

Silversea

Guest
That was my 2 cents.

You can try something like:
Code:
score_text=string(score);
while string_length(score_text)<6 score_text="0"+score_text;
Or something like:
Code:
score_text=string(score);
score_length=string_length(score_text);
size=6-score_length;
for (i = 0; i < size; i += 1)
{
    score_text="0"+score_text;
}
Let me know if this changes the memory issue.
Yep, no memory leak with that code in the Draw event. So this is strictly a problem with string_insert.

Also huh, I didn't realize you could just add strings like that...or maybe I did, and I just forgot.
 
Top