Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Bug Reports / Memory leak - Strings in Types in Arrays

Author
Message
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 9th Aug 2008 13:08
When you make an array from a type, and the type contains a string, you get a memory leak. The code is below, it requires the enhancement pack to get the memory readout, although you could watch it in the Task Manager.

String Arrays are fine
Types that are numeric only are fine
The string can be first in the type, or any position



IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 9th Aug 2008 15:15
Make it much more obvious:


If the memory is getting freed, then the above program will never fail. If it isn't then the above program will crash due to lack of memory before it gets to the 20th iteration (20 times 100MB = 2GB addressable memory available to a standard windows program).

It crashes on the 16th iteration for me.

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 11th Aug 2008 10:40
Thanks Ian. I also forgot to mention I get this as far back as 6.4, haven't tested any further back.

Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 12th Aug 2008 00:19 Edited at: 12th Aug 2008 00:20
Wtf...


[b]Yuor signutare was aresed by a deslyxic mud...
BOX2D V2 HAS HELP FILES! AND A WIKI!

Attachments

Login to view attachments
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 12th Aug 2008 14:15
Perfectly normal - Windows is increasing the allocation of virtual memory space to your program on demand.

LeeBamber
TGC Lead Developer
24
Years of Service
User Offline
Joined: 21st Jan 2000
Location: England
Posted: 5th Sep 2008 14:13
Regular strings in DBPro automatically free themselves when the program exits, but strings created inside a custom typed array must be freed by the user. Place the following line at line 13 of the above program to resolve the leak:

t(0).s = free string$()

This command is from U70 BETA3. For versions below this one, use the alternate command:

t(0).s = free string()

I agree the UNDIM command could automate this process, and certainly the help system should describe the need to free strings within a typed array which will be present in U70 BETA4.

"Small, smart and running around the legs of dinosaurs to find enough food to survive, bedroom coders aren't extinct after all "
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 5th Sep 2008 14:28
Thanks Lee, appreciated

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 5th Sep 2008 18:46 Edited at: 5th Sep 2008 19:19
If I use this solution (free string() and free string$()) it doesn't have any effect mid-program. Is this only a fix when exiting the program?


[EDIT] This works for arrays of a fixed element count. I created a situation that I'll try to untangle from my main code to demonstrate at some point. Essentially, I make a check list of display modes, then ADD TO QUEUE through each available mode. Using the above method stills results in a memory leak.

I have now fixed the memory leak by DIMming the array to the number of items in the checklist. Howeber, I still can't reproduce the same error on all dynamic arrays.

Freddix
AGK Developer
21
Years of Service
User Offline
Joined: 19th Sep 2002
Location: France
Posted: 12th Sep 2008 16:06
And what happen if your app crash ?
The memory is lost definitively until the next windows restart ?

Gandalf said: "All we have to decide is what to do with the time that is given to us"
Odyssey-Creators - X-Quad Editor - 3DMapEditor
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 12th Sep 2008 19:14
No, windows will recover all memory allocated to a program that has ended, whether it crashed or exited cleanly.

Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 28th Sep 2008 21:55
Not in windows 98 and lower it won't

[b]Yuor signutare was aresed by a deslyxic mud...
BOX2D V2 HAS HELP FILES! AND A WIKI!
GIDustin
15
Years of Service
User Offline
Joined: 30th May 2008
Location:
Posted: 1st Jan 2009 22:19 Edited at: 1st Jan 2009 22:56
Just checking in to see the status on this problem. I think this is the reason behind the leak in my game.

I tried all the following ways to cleanly kill an array with no luck:

undim(tempArray())
empty array tempArray()
for i = array count(tempArray()) to 0 step -1 : array delete element tempArray(), i : next i

I am making a card game, and each card is a UDT with several strings. Each card gets moved from one global array to another based on its position on the board/deck, so every time a card moves and is deleted from one array and added to another, I leak. It is really obvious later in the game when I have several cards out.

It would be rather difficult for me to implement this "free string()" thing due to the shear number of places where an array element is deleted.

Edit: Just ran a simple test. UDTs aren't the only problem, at least for me. The following keep leaking memory as well. It is how I handle debug information. Throughout the game I add strings to a global array, and each sync I put the strings on the screen (I use the d3d_text command, not print, but changed it so you dont need DLLs). The following contains a string array, no UDT, and it still leaks;

Deego
15
Years of Service
User Offline
Joined: 21st May 2008
Location:
Posted: 22nd Jan 2009 20:03 Edited at: 22nd Jan 2009 20:05
Any closer to fixing this bug? What about workarounds? I want to be able to call different sets of strings in a function by only changing the array number.

Like this:


The only workaround I see is replacing the string in my types with an individual array, so instead of triv(N).q, triv(N).a1, etc., I would have q(N), a1(N), etc.. Which doesn't seem too bad a route to go, but it would be easier to keep it all in one array.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 22nd Jan 2009 22:45
The solution is above, posted by Lee. Use FREE STRING$() to reclaim the memory used by the string elements.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd Jan 2009 22:03
Quote: "It crashes on the 16th iteration for me."


Out of sheer curiosity I just tried IanM's code and it always crashes after just 1 iteration for me - but the sys mem value varies between 2 and 5 even with no other apps open. What does this tell me? (Other than the obvious, that is. )
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 23rd Jan 2009 23:11
Your system is legendary! I'm amazed it can even start up a DBPro program, let alone what you manage to get it to do.

Incidentally, as a short-term fix for this bug, just precede the UNDIM command with a CLEAR ARRAY command from my plug-ins - my code correctly deallocates the strings from UDT's in 7.1

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 24th Jan 2009 00:06
Quote: "Your system is legendary! I'm amazed it can even start up a DBPro program, let alone what you manage to get it to do."


Yes, I'm going to shed a few tears when it finally dies.
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 29th Jun 2010 14:06
Fixed in 7.5

Both EMPTY ARRAY and UNDIM now correctly deal with strings within UDT arrays too. You no longer need to assign FREE STRING$ to all of your array strings before destroying the array.

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 1st Jul 2010 23:34
hoorah!

IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
GIDustin
15
Years of Service
User Offline
Joined: 30th May 2008
Location:
Posted: 2nd Jul 2010 01:23
I assume this also fixes when you just re-assign a value as well then?

I am very happy, although I am very sad for the free string$() command. Its moment in the spotlight has dwindled...

Login to post a reply

Server time is: 2024-04-25 06:59:22
Your offset time is: 2024-04-25 06:59:22