PDA

View Full Version : completely remove objects



patrickjv
June 29th, 2007, 01:16 PM
I read http://www.kirupa.com/forum/showthread.php?p=2129547 but still can't manage to remove completely

I have a MC with linkage class "zzz". MC has a square shape and 3 frames and in 3rd frame actions

trace(this.name);
stop();

When I put in main timeline actions

var i:int;
var child:Array = new Array();
for(i=1;i<=5;i++){
child[i] = new zzz();
addChild(child[i]);
child[i].name = "child"+i;
child[i].x = i*20;
child[i].y = i*20;
removeChild(child[i]);
child[i] = null;
delete child[i];
}

All 5 are visually removed but the trace actions within them still perform ("child1" to 5), so apparently they run/play anyway.

How to remove them properly? As you see I tried delete and setting to null but won't help

anybody?

Aquilonian
June 29th, 2007, 09:10 PM
maybe cos its too soom for the garbage colector to remove them, but he will sooner or later?

patrickjv
June 29th, 2007, 10:37 PM
maybe cos its too soom for the garbage colector to remove them, but he will sooner or later?


I don't think that is the problem ... if you remove the stop(); action in MC "zzz"... and let them loop forever they keep tracing the name and you'll see that they never are removed.

Dazzer
June 29th, 2007, 11:12 PM
The garbage collector doesn't collect until it sees a need to, I believe.

patrickjv
June 29th, 2007, 11:50 PM
ok. So there's no way to 'force' a cleanup? absurd in my opinion, thinking about my case (-> I run a function with for-loop addChild creating a number of childs which are used to pre-calculate text widths into an array. This function, or actually the childs, need to be cleaned/removed immediately when done since the childs contain a timeline which is not meant for the added childs in the function. The timeline is meant for childs created later on, the childs from the function will throw errors when playing the timeline)

Dazzer
June 30th, 2007, 12:35 AM
1) The idea is that the GC does things for you. You don't tell it to do anything.
2) There's a reason why we say "Try not to use Timeline scripting in AS3".

IMHO, timeline scripting should be limited at MOST to stop(), and play() functions.

patrickjv
June 30th, 2007, 12:50 AM
1)Ok, that's a point of view. Mine is that in that case AS2 is better since removemovieclip would not give 'redundant' clips playing timeline and throwing error messages like I do now.
2)It's not an option. I have (commercial) components which are installed through .mxp. When dragging an installed component to new .fla, the external AS class file(s) is not included/saved in folder of the .fla. I tried that already. So it doesn't work for my customers.

I'm encountering new issues by the minute so try to solve them on a 'best-solution-i-can-think-of' with my limited knowledge of as3. I'm aware of the as3 principles but they don't always do it for me

Dazzer
June 30th, 2007, 01:08 AM
the component classes are probably in a default class path folder ? check your publish classpaths.

also are those commercial components meant for flex, or flash? AS2 or AS3?

You seriously shouldn't be running into these problems I think...


I run a function with for-loop addChild creating a number of childs which are used to pre-calculate text widths into an array.
I'm not sure why you're doing it that way... Maybe if you tell me exactly what you're trying to do we can solve that in a more elegant fashion. If you can't fix it, then do it another way, right?

Sirisian
June 30th, 2007, 01:49 AM
This is rather odd. When you remove it then ideally the stage loses reference to it. If will be removed from the time line and everything except being removed from memory which isn't a problem since you are not accessing that memory since no reference should point to it.

Also what is this?
child[i] = null;
delete child[i];
making the reference null then deleting a null reference. Albeit most languages will just let it be and not tell you how wrong that is. Maybe you meant to do:
delete child[i];
child[i] = null;
However knowing AS3 and it's garbage collection it will throw a warning or error. Just do:
child[i] = null;
And I know that the stage references to the child[i], but just out of curiosity why do you have:
child[i].name = "child"+i;
I mean you have an array of references. What's the point of naming the objects?
I'm sure there is a reasonable answer to this.

Dazzer
June 30th, 2007, 02:03 AM
Yes but even being out of the display list doesn't guarantee that it does not get ENTER_FRAME Events... right?

patrickjv
June 30th, 2007, 02:04 AM
the component classes are probably in a default class path folder ? check your publish classpaths.
They might be, even probably are since I added <file name="component.as" destination="$flash/Components" /> to the .mxi file. Neither the .fla nor .as is found on disk after install (they are installed though since the component appears in the list in flash cs3)
Nevertheless, when a user drags the component from component panel to a .fla, it does not include the .as file into the folder of that .fla. So the component won't work. I tested that.
ps: It ís possible I'm doing something wrong or are unaware of how to do it properly since in AS2 all was internal without .as files...


also are those commercial components meant for flex, or flash? AS2 or AS3?
I already have them for flash 6,7 and 8, as1 and as2. Now I'm a busy bee converting them to as3.


You seriously shouldn't be running into these problems I think...
ha, I wish I wasn't.. it's keeping me very busy.


I'm not sure why you're doing it that way... Maybe if you tell me exactly what you're trying to do we can solve that in a more elegant fashion. If you can't fix it, then do it another way, right?
well. For the particular problem I found a solution.
What I gave as example is only a simplified model. In my actual model I have
1) a function with addchild in a for-loop which is performed only once using MC containing dynamic text fields. Text fields values are set and widths are extracted for positioning values into an arry. Content removed after function (which I didn't manage as in example above)
then
2) a frame loop with same idea but they remain visible.. they use the positioning values calculated in 1). 1) is nessesary to be able to implement 2)

childs in 2) work with a timeline with scripting. I used its library source/item for 1) as well but these childs of 1) give errors because they point to values which they don't have in ... childs of 1) actually shouldn't be playing their timelines.
I solved this by using a blanc MC as source for 1) addchild children and the actual MC with playing timeline for 2). I won't elaborate but that works. I still have garbage left but they won't give errors.

To get an idea what the components do see patrickjansen.net/components/


feedback is appreciated, as you said... I'm trying to drive my way around issues and there's a lot to solve.
I'm going to post another thread about people saying as3 is fast... I am seeing the opposite :|

Dazzer
June 30th, 2007, 02:12 AM
Hahaha
Nice site :)

Well... What I would do is use a TextField object, enter in the text, and then use AutoSize to get the widths. Either that or use textWidth. Remember you don't need to add the TextFields on a displayList anyway. So you can just reuse 1 TextField.

Using AutoSize it is possible to figure out the width of the textfield and position the textfield appropriately anyway .Maybe I'm talking crap, cuz I'm still not seeing what you're trying to do with the for loop stuff...

Some very nice effects though! :D - drools - wish I could do stuff like that...

patrickjv
June 30th, 2007, 02:12 AM
child[i] = null;
delete child[i];

just trying anything I can think of... Forget them.
In the end, the childs are not removed.... they arent... the example is really simple, try it yourself and make the MC loop... it keeps tracing its name. At no point it stops.

Still makes me conclude that there MUST be something strange. All childs don't show anymore but all keep tracing their name so still are present and playing their timelines.

Hope there is some way to really remove them

patrickjv
June 30th, 2007, 02:17 AM
Hahaha
Nice site :)

Well... What I would do is use a TextField object, enter in the text, and then use AutoSize to get the widths. Either that or use textWidth. Remember you don't need to add the TextFields on a displayList anyway.

Using AutoSize it is possible to figure out the width of the textfield and position the textfield appropriately anyway.

Some very nice effects :D - drools - wish I could do stuff like that...

tnx for compliment
I need to position and calculate widths, depending on font choice and size. After that I need to check aligment choice of user and recalculate positions accordingly. I actually do use textWidth for this ;)
(ps: The component offers option to set an array to override multiple letter ranges within the complete text with other fonts/sizes, so can't calculate whole text in itself with textWidth.)
Hope that explains why I'm being so difficult, haha.

Dazzer
June 30th, 2007, 02:22 AM
Patrick. when you call Delete, it deletes the reference. Not the object.
There's no way to physically delete the object.

Adding a child to the DisplayLists also does not mean it is Child in the literal Object Composition sense. It only means that it on a displayList and will be displayed in that heirarchy. This is different from AS2, where one could simply put this.parent.doSomething.

Thus since they are no longer connected to the heirarchy you shouldn't have to worry about errors?

The timeline is meant for childs created later on, the childs from the function will throw errors when playing the timeline)
If you are getting errors here, please tell me what errors.

patrickjv
June 30th, 2007, 02:32 AM
If you are getting errors here, please tell me what errors.

I circumvented it already but will elaborate anyway.
Actually the child from 1) and childs from 2) are given a "sub-child"(s) containing the dynamic text field. The child from 1) is given a .name which differs from the .name I give to the childs from 2)

I used same MC as child for both 1) and 2) and same MC "sub-child" in both.

The MC uses its .name in references and calculations (actually I extract a numeric part from the name). child 1) could not use those and gives error.
Basically that happened

Now I use a different MC as source for child in 1 then the one in 2... MC from 1 is blank and MC from 2 has timeline ... Since both can be assigned the same "sub-child" it works fine. Problem solved.

Aquilonian
June 30th, 2007, 07:14 AM
You put all of them inside a array, isnt it?
So,after null them why dont you go
childs = new array();
?
You could also search the forums for active graph, it will help you to see the memory usage
And finally
blabla = null;
is when you create it with var

delete blabla;
is for dynamic thing

patrickjv
June 30th, 2007, 01:01 PM
One final time summing up and giving up.

A library MC with linkage class "zzz". MC has 10 frames and in 10th frame action
trace(this.name);

in main timeline put

var i:int;
var child:Array = new Array();
for(i=1;i<=5;i++){
child[i] = new zzz();
addChild(child[i]);
child[i].name = "child"+i;
removeChild(child[i]);
}

Anybody know what to do to completely remove the childs... make the movie stop tracing the child names?

senocular
June 30th, 2007, 01:58 PM
see:
http://www.gskinner.com/blog/archives/2006/06/as3_resource_ma.html
note the series links on the right.

As mentioned before, the GC will not collect that movie clip from memory until it needs to. Its up to you to stop any processes that may persist within that movie clip until that happens. This includes removing listeners and stopping movie clip playback (where actions on frames might continue to run).

You can force a GC clean with a hack - its mentioned in the link above (3rd one in the series I think) - though its not recommended and unsupported.

Blommestein
July 9th, 2007, 04:22 AM
One final time summing up and giving up.

A library MC with linkage class "zzz". MC has 10 frames and in 10th frame action
trace(this.name);

in main timeline put

var i:int;
var child:Array = new Array();
for(i=1;i<=5;i++){
child[i] = new zzz();
addChild(child[i]);
child[i].name = "child"+i;
removeChild(child[i]);
}

Anybody know what to do to completely remove the childs... make the movie stop tracing the child names?


see:
http://www.gskinner.com/blog/archives/2006/06/as3_resource_ma.html
note the series links on the right.

As mentioned before, the GC will not collect that movie clip from memory until it needs to. Its up to you to stop any processes that may persist within that movie clip until that happens. This includes removing listeners and stopping movie clip playback (where actions on frames might continue to run).

You can force a GC clean with a hack - its mentioned in the link above (3rd one in the series I think) - though its not recommended and unsupported.

So in the script he posted above, the movieclips WILL be removed from memory eventually then?

Aquilonian
July 9th, 2007, 07:12 AM
As a developer, you must accept that fact that you will have no way of knowing when (or even if) your inactive objects will get deallocated. You must also be aware that inactive objects will continue to execute indefinitely (until the GC deallocates it), so code will keep running (ex. enterFrames), sounds will keep playing, loads will keep happening, events will keep firing, etc.


To summarize, in AS3 deleting objects is no more.Redesign to not need to delete them
or
make them inactive and later reuse them.