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.

AppGameKit Classic Chat / Warning! Memory leaks!!!

Author
Message
Alex_Peres
AGK Master
15
Years of Service
User Offline
Joined: 13th May 2009
Location: The Milky Way Galaxy
Posted: 8th May 2014 19:55
AGK 108.21, Tier 2, Windows 8.1, VS 2010.

agk::Str() function - leaks memory.
Be carefull with it!

Probably in Tier 1 the same bug!

P.S. Dark GDK - also has the same bug in similar function.
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 8th May 2014 20:13
Remember that YOU have to release the memory!

-- Jim - When is there going to be a release?
Alex_Peres
AGK Master
15
Years of Service
User Offline
Joined: 13th May 2009
Location: The Milky Way Galaxy
Posted: 8th May 2014 20:21 Edited at: 8th May 2014 20:21
JimHawkins
You don't know what you are talking about...
george++
AGK Tool Maker
17
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 8th May 2014 20:25 Edited at: 8th May 2014 20:26
I don't know if you are right but it seems that you are announce the end of the world
Don't panic, everything will be OK
The Zoq2
15
Years of Service
User Offline
Joined: 4th Nov 2009
Location: Linköping, Sweden
Posted: 8th May 2014 21:43
In t2, any functions returning a char* will leak memory unless you delete the strings manually. Char* is just a pointer to some chars on the heap (or is it the stack) that does not get deleted automatically.

To safley clean the memory, you need to do something like this


Also, make sure you don't delete a pointer that doesn't exist, it will cause a memory error and crash your program

You can solve that by doing this instead of just delete[]



Also, working with char* isn't such a good idea, you need to manage the memory and there are very few functions for doing anything. Using a string class is a better alternative. I would suggest using std::string, it's a standard and works really well. To use it with agk, you must first include the library by adding #include <string> to the top of the h file you want to use it in.

Then you can use it like this


Say ONE stupid thing and it ends up as a forum signature forever. - Neuro Fuzzy
Alex_Peres
AGK Master
15
Years of Service
User Offline
Joined: 13th May 2009
Location: The Milky Way Galaxy
Posted: 8th May 2014 22:08
Just try to put in main Loop cycle this -
The Zoq2
15
Years of Service
User Offline
Joined: 4th Nov 2009
Location: Linköping, Sweden
Posted: 8th May 2014 23:05
As I said in my previous post, that will cause a memory leak because you don't clean up the content that the char* returned by agk::Str() is pointing at.

It's a bit annoying at first but pointers are really useful in C++ when you get used to them

Say ONE stupid thing and it ends up as a forum signature forever. - Neuro Fuzzy
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 9th May 2014 00:16
Alex_Peres

Say sorry nicely.

-- Jim - When is there going to be a release?
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 9th May 2014 00:23
It's not really a memory leak, it's a failure to clean up after your own code.
Every time you call that command in your loop,you are creating a new string. At 60FPS, I believe you are creating 10.8KB of lost data every minute.

Quote: "JimHawkins
You don't know what you are talking about"

Firstly, he is correct in what he told you.
Secondly, Jim really does know what he's talking about.
Thirdly, please try to be more polite to the people who are trying to help you. This community is full of very knowledgeable and helpful people.

unlikely
12
Years of Service
User Offline
Joined: 5th Mar 2012
Location: Ohio, USA
Posted: 9th May 2014 00:24
It does even say this explicitly in the documentation.......
Alex_Peres
AGK Master
15
Years of Service
User Offline
Joined: 13th May 2009
Location: The Milky Way Galaxy
Posted: 9th May 2014 01:35
Ok, sorry guys.
But how should I clean this?
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 9th May 2014 01:36
Don't worry, BatVink - I can dish it out as well!

It's an interesting question. I've looked at the Pascal interface, and as far as I can see the only functions/procedures that require an AppGameKit string are Error and Warning. These will almost certainly be on the stack, so I don't see any potential leak here.

The TAGKScene class is heavily overloaded, so Print() can take a multitude of inputs, but usually that's going to be an AnsiString.

I would strongly recommend not putting debug values into SetWindowTitle() because that's a slow OS call, and the value will be wrong because of the time it takes to do it.

-- Jim - When is there going to be a release?
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 9th May 2014 11:37
if u need this syntax maybe make a own str function that return just a string.
see code example from "The Zoq2" above.

AGK 108 (B)19 : Windows 8.1 Pro 64 Bit : AMD Radeon HD 6670
Alex_Peres
AGK Master
15
Years of Service
User Offline
Joined: 13th May 2009
Location: The Milky Way Galaxy
Posted: 9th May 2014 21:33
Thanks guys. But I know about possibility to create my own c++ function. Just wanted to warn you about that bug.
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 9th May 2014 22:26
Alex - It's not a bug!

-- Jim - When is there going to be a release?
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 12th May 2014 02:37
JimHawkins is absolutely right. It is not a bug.

And The Zoq2 showed the correct code for cleaning up the string.

Replaced this:


With this:


You don't need to create new strings all the time for how you are using them. Just reuse a buffer to create the text you want.

Cheers,
Ancient Lady
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 14th May 2014 03:39
I wrote a class to handle this in AGK. The class handles all of the clean up, no freeing on your part.

link: http://forum.thegamecreators.com/?m=forum_view&t=207906&b=41&msg=2486767#m2486767

Don't use this on const char* though eg AGKSting str = "Hello World"; /* don't do this */
TDavid
11
Years of Service
User Offline
Joined: 7th Feb 2013
Location:
Posted: 18th May 2014 11:50
"if you are calling this command from tier 2 this string must be deleted when you are done with it."

I assume this doesn't apply to tier 1.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 27th May 2014 07:12
Tier 1 seems to handle this all sort of automagically, maybe.

There are no commands to release dynamically created memory (except for memblocks).

A test program might be created that does nothing but use commands that generate strings and then watch the memory usage (doable in Windows with the Task Manager).

Cheers,
Ancient Lady

Login to post a reply

Server time is: 2024-11-25 05:41:49
Your offset time is: 2024-11-25 05:41:49