Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1075918 Posts in 44152 Topics- by 36120 Members - Latest Member: Royalhandstudios

December 29, 2014, 03:03:07 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Unreferencing everything inside removed children AS3
Pages: [1]
Print
Author Topic: Unreferencing everything inside removed children AS3  (Read 662 times)
RyanHuggins
Level 1
*



View Profile WWW Email
« on: June 08, 2013, 12:14:32 PM »

I will try to make this question as simple as possible for your sake and for mine

Code:

// this is how I create an instance of a level
_STAGEONE = new StageOne();
addChild(_STAGEONE);

//------

// this is how I remove it
if (_STAGEONE && _STAGEONE.parent)
{

     _STAGEONE.parent.removeChild(_STAGEONE);
     _STAGEONE = null;

}


Before I remove _STAGEONE, do I have to remove every reference and event listener from the instances that I use inside of it to avoid a memory leak?

I has a few buttons, a HUD, a vector or two and whatnot. Do I have to remove the references to all of those things before I remove the _STAGEONE, or is removing _STAGEONE in this manner enough?

If you need more code to help, I will absolutely provide it. I've been searching all day and reading about the GC and removing references and nulls and stuff. I can't seem to determine if this is correct.

Edit: Also, what should I be aiming for as far as memory usage?

My whole concern with this is that when I remove the HUD (ie the STAGEONE) and then put it back (I have it on a staggered loop for testing), the memory use stays the same for the first two or three cycles and then starts infringementing by roughly 100kb every time the HUD reappears. I'm not sure if this is normal and I'm not really sure if it's a memory leak either, since it would never actually loop like this, so I don't know!

Essentially it goes...
Create HUD: memory use is 18.402
Remove HUD: memory use is 18.211
Create HUD: memory use is 18.512
Remove HUD: memory use is 18.310
Create HUD: memory use is 18.660
loop forever
« Last Edit: June 08, 2013, 02:20:55 PM by RyanHuggins » Logged

ThemsAllTook
Moderator
Level 10
******


Alex Diener


View Profile WWW
« Reply #1 on: June 08, 2013, 02:54:53 PM »

My AS3 is pretty rusty, but as I recall, there's an optional argument you can pass to addEventListener to make it use a weak reference. That way, you don't have to remove the listener explicitly to avoid leaking. Might help a bit.
Logged
RyanHuggins
Level 1
*



View Profile WWW Email
« Reply #2 on: June 08, 2013, 03:50:05 PM »

I turned all of my references to weak references and the problem still persists. I'm painstakingly experimenting with my code to see what causes it and it seems that where I create objects (like bitmaps and shapes and stuff) use memory, but when they're discarded don't completely get rid of it all.

I'm a noob though, so I could be completely wrong.
Logged

biomechanic
Level 3
***


View Profile
« Reply #3 on: June 09, 2013, 02:16:46 AM »

Try explicitly removing event listeners with removeEventListener. Do it everywhere - if you have instances of custom classes (like HUD elements and such) inside StageOne, make sure you can request them to remove their listeners as well, and if they have children of custom classes make sure the kids remove listeners too. All the way down.
Logged
RyanHuggins
Level 1
*



View Profile WWW Email
« Reply #4 on: June 09, 2013, 02:08:18 PM »

Okay, well I've done everything I feel is physically possible to do to remove all the references and whatnot. The problem persists so I assume it's one of two things.

1.) Flash uses more ram to create an instance than it frees when it deletes one.

or

2.) I'm really fucking up somewhere.

Here is a link to an example of the issue. Click and Hold to remove HUD. Release click to spawn a new one.

https://dl.dropboxusercontent.com/u/62523364/Ryan/Practice/BV%202013/bin/BVTIGS.swf

Edit: After enough frantic clicking it looks like the GC activates after about 100 clicks or so? Can someone verify for me?

« Last Edit: June 09, 2013, 03:49:11 PM by RyanHuggins » Logged

biomechanic
Level 3
***


View Profile
« Reply #5 on: June 09, 2013, 11:11:21 PM »

Yep, Mem Use goes up to 16-19 and then it drops.
Logged
Xienen
Level 3
***


Greater Good Games


View Profile WWW
« Reply #6 on: June 10, 2013, 04:46:46 AM »

From my limited experience, the GC in AS3 runs quite infrequently.  It does, however, always run when the system gets low on memory, so if you get close to exhausting the memory on a particular machine and you've correctly removed references, the GC should kick in.
Logged

RyanHuggins
Level 1
*



View Profile WWW Email
« Reply #7 on: June 10, 2013, 11:36:11 PM »

Omg. (Thanks for testing by the way guys). I added in ONE MORE class and added A SINGLE instance of another object to the game and suddenly it doesn't work correctly anymore.

I wish someone would tell me if this was normal. :U Otherwise I may just develop in another language or use FlashPunk or some shit.

Edit: After more editing I realized that some VERY STUPID things were causing the GC to not run? I could really use some clarification from someone or a reference for another browser-deployable language to use...

Here it goes.

This code allows for the GC to run at some point
Code:
private function manageScore():void
{
var scoreCurrent:String;

scoreCurrent = _scoreCurrent as String;

// update score
if (_scorePrevious != _scoreCurrent)
{
_myScoreField.text = "$" + scoreCurrent;
}
}

But if I use this almost identical code (I presume), the thing doesn't occur
Code:
private function manageScore():void
{
// update score
if (_scorePrevious != _scoreCurrent)
{
_myScoreField.text = "$" + String(_scoreCurrent);
}
}

Then there's the other obnoxious line...

This works...
Code:
_hudBgRightBg[0].y = _hudBgRightBg[0].y - 1;

However this doesn't
Code:
_hudBgRightBg[0].y -= 1;

Like forreal? Is this my fault? Am I doing something horribly wrong? Or is something wrong with my compiler maybe? I honestly don't know, man, but it's the most frustrated I've been in awhile. xD
« Last Edit: June 11, 2013, 02:11:34 AM by RyanHuggins » Logged

nikki
Level 10
*****


View Profile Email
« Reply #8 on: June 11, 2013, 05:17:39 AM »

I suspect
Code:
var scoreCurrent:String;

scoreCurrent = _scoreCurrent as String;

will trigger the GC at some point yes.

apart from removing event listeners and nulling out stuff I don't know any more things. maybe one of these links are helpful

http://divillysausages.com/blog/tracking_memory_leaks_in_as3
http://forums.adobe.com/message/2783898
http://stackoverflow.com/questions/665094/debugging-flex-as3-memory-leaks
http://gskinner.com/blog/archives/2006/06/as3_resource_ma.html
Logged
RyanHuggins
Level 1
*



View Profile WWW Email
« Reply #9 on: June 13, 2013, 07:26:06 PM »

@Nikki, thanks for helping out. I am not certain I have figured out how to fix the problem persay, but it's currently GCing and if I'm careful and test often, I hope I can keep it that way.

The links are great too btw, I read 2 of them prior, but the other ones were helpful with learning about how to deal with the GC.

If it fucks up and gets laggy again, I might switch over to a library like FlashPunk or Flixel. Smiley
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic