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 / variable persistence

Author
Message
mindsclay
12
Years of Service
User Offline
Joined: 1st May 2012
Location: Rocklin, CA, USA
Posted: 29th Dec 2014 02:32
Do the variables created within a function persist in memory or are they dump when leaving the function?

I am asking for "clean-up"/optimization reasons. Like deleting sprites and images when I leaving a function.

home.wavecable.com/~mindsclay
BMacZero
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 29th Dec 2014 05:32
Variables with basic types (integer, float, string) are local unless explicitly declared otherwise (global myVar).

However, it's important to note that the values of variables are completely independent from the resources they actually represent, and resources are never deleted unless you delete them explicitly. For example, in this code:

The variable 'i' is local to the function, but the image represented by the value of 'i' will not be automatically unloaded. 'i' is just a way to reference that image.

Also note that variables with UDT types (type/endtype) are always global (at least in V1).

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 29th Dec 2014 14:30
its depends on the language.
normal it should be reserved memory for local variables if you call a function. after the function the memory will be freed.
its necessary if you call a function recursive.

there is no unlocal so seems agk do the job

AGK 108 (B)19 + AppGameKit V2 Alpha .. : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
dirahl
9
Years of Service
User Offline
Joined: 2nd Jan 2015
Location:
Posted: 4th Jan 2015 03:10
BMacZero has a good point. If you create a sprite and display it on the screen in a function, and then the function exits, you will notice that the sprite does not go away on screen once the function exits. If you didn't stash the value of the sprite anywhere, you have no means of working out what its ID was to delete it. The computer cannot reclaim the memory from that sprite until the program exits. That's called a memory leak.

Therefore, always keep the IDs of objects returned by AppGameKit that you do not intend to hang around for the whole game, so that you can delete them when you're done with them.
paulrobson
10
Years of Service
User Offline
Joined: 22nd Nov 2014
Location: Norfolk, England
Posted: 4th Jan 2015 19:24
Or alternatively, specify the IDs as constants and keep an #insert file of constants so your code looks readable

If you want to return something complicated from a function the best way to do it is to define a user defined type which holds the structure you want and pass this into a function as a reference parameter.
mindsclay
12
Years of Service
User Offline
Joined: 1st May 2012
Location: Rocklin, CA, USA
Posted: 7th Jan 2015 01:25
I state that I am deleting images and sprites. I am asking about all other variables.

home.wavecable.com/~mindsclay
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 7th Jan 2015 10:01 Edited at: 7th Jan 2015 10:02
+1 for BMacZero's explanation, it's something that most of us take for granted but is important for newcomers to AppGameKit to know.

In this example, myImage will be cleared up automatically, when the function ends:


In this example, myImage will be cleared up automatically, when the program ends. You cannot delete global variables, you can only reset their value to zero or nothing.:


Quidquid latine dictum sit, altum sonatur
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 7th Jan 2015 10:10
It would be nice to have a bit in the help files on memory management and scope in the Basic interpreter.

-- Jim - When is there going to be a release?

Login to post a reply

Server time is: 2024-11-25 15:01:18
Your offset time is: 2024-11-25 15:01:18