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.

Newcomers AppGameKit Corner / Deleting instance of a UDT

Author
Message
Koakuma
9
Years of Service
User Offline
Joined: 13th Aug 2015
Location:
Posted: 13th Aug 2015 22:07
Hi! I recently got AppGameKit via the humble bundle and I've just begun working on a project for practice in tier 1, but I'm having some issues with fundamental OO.

How do I delete a user-defined object? For example, having a function which says -

and later, a function that says -

seems to result in the array being shortened, as expected, but the now dereferenced object seems to still exist, so memory usage gradually increases. Couldn't find anything on google or in the online documentation about this, so I ask here. Is there a function that can do this?
I know that tier 1 is weak when it comes to OO, but deleting an object to free up memory should be possible, no?

Additionally, are there any other limits of OO like this in AppGameKit?
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 13th Aug 2015 22:48
Welcome to the forums

Quote: "but the now dereferenced object seems to still exist"


Which object? The sprite? The UDT? or the array element?
What makes you say that it still exists, how did you test it?

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Koakuma
9
Years of Service
User Offline
Joined: 13th Aug 2015
Location:
Posted: 13th Aug 2015 23:10
@BatVink
The UDT. The sprite and array element were both deleted as intended.
I tested it by making a simple loop of "hold Z to fire a bullet object", opened task manager and watched as my program rapidly began to eat all my memory. (Rapidly in terms of a UDT that only contains two integers, haha)
As far as I saw, the memory did not then decrease. (I left it alone a good while)
Is it simply that AppGameKit does have a garbage collector, and when a program is assigned memory, it does not remove that assignment? I'm not too familiar with OS memory handling.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 14th Aug 2015 01:30
It must be something else.

I ran the code below for 60,000 iterations, and it remained at a constant 29.4 MB.




Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Koakuma
9
Years of Service
User Offline
Joined: 13th Aug 2015
Location:
Posted: 14th Aug 2015 05:24
How odd.
I just ran your code too, no changes in memory usage.
I just ran my code again for 10,000 iterations. Steady memory increase from 30 MB to 70 MB.

I then changed instance.a in your code from being initialized to 999 to instead being set to

and immediately noticed a memory leak.
I thought maybe it could be that I am not deleting the image created with LoadImage, but in my code I am not repeatedly loading the image - I have it defined as a constant upon initial booting of the app.
I changed the line from

to

Therefore, I am going to conclude that it is something wrong with the Create/DeleteSprite functions. Am I forgetting to do something with these sprites that I really should be doing?

(Complete edited code that causes memory leak: )
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 14th Aug 2015 10:46
The problem is your images. You keep loading the same image repeatedly and never deleting them.

This construct:



only works if you load an image once, and keep it forever. Where you are deleting and creating images/sprites continuously, you should do this:



Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Koakuma
9
Years of Service
User Offline
Joined: 13th Aug 2015
Location:
Posted: 14th Aug 2015 20:50
So setting it as a constant doesn't load the image into the constant and simply reference it when I call createsprite?

I had assumed that by doing

I was only loading the image once. That is not the case, then?
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 14th Aug 2015 22:02
A constant is simply replacement text. At compilation time, it replaces the constant with the text assigned to it.

The variable you use to reference the image is just a number, but it's important to track the images. For example, you will lose track of your loaded images if you do this:

img = loadImage("image1.png")
img = loadImage("image2.png")
img = loadImage("image3.png")
img = loadImage("image4.png")

As soon as you load image2, you can't trace image1 because you overwrote the value in img. This is very similar to your code - you can't find the image reference to delete it.

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Koakuma
9
Years of Service
User Offline
Joined: 13th Aug 2015
Location:
Posted: 18th Aug 2015 22:52
Having given it some time to think about it, I suppose I have a couple more questions now:

First of all, how would I achieve the goal I'm after? A global would work, right? I'd just have to remember to not change it. There's no way to lock a global to be unchangeable, is there?

Secondly, would loading an image into a local type's sprite and letting the local type go out of scope cause a memory leak?
For example,

Would I have to deletesprite and deleteimage mT.sprite after inserting it into the array, or would that also delete the sprite and image of allTypes[allTypes.length]?
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 19th Aug 2015 14:56
Quote: "First of all, how would I achieve the goal I'm after? A global would work, right? I'd just have to remember to not change it. There's no way to lock a global to be unchangeable, is there?"

Use a constant IE:


Quote: "Secondly, would loading an image into a local type's sprite and letting the local type go out of scope cause a memory leak? "

No. It gets garbage collected.

Quote: "Would I have to deletesprite and deleteimage mT.sprite after inserting it into the array, or would that also delete the sprite and image of allTypes[allTypes.length]? "

If you create a sprite it will exist until you delete it. The sprite reference is just an integer, not a pointer. You need to store the integer in a variable you can access later if you want to delete it unless you use DeleteAllSprites.

Using AppGameKit V2 Tier 1
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 19th Aug 2015 16:25
Quote: "would loading an image into a local type's sprite and letting the local type go out of scope cause a memory leak"


The way you have worded this could lead to confusion. You haven't loaded the image or the sprite into the Type. You have created an image and a sprite, and they exist in the application. The values in your type are simply a way to reference the image and sprite.

Think of it like a waiter taking your order. He writes it on a piece of paper (the value in your Typed variable) and the chef makes the meal (your image and sprite). If you throw away the piece of paper, your meal doesn't disappear. But if you throw away the paper, you have no way of knowing who ordered it.

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt

Login to post a reply

Server time is: 2024-11-21 20:22:12
Your offset time is: 2024-11-21 20:22:12