all groups > flash actionscript > october 2005 >
You're in the

flash actionscript

group:

HELP!! Deleting Objects inside of a function


HELP!! Deleting Objects inside of a function chrebel
10/24/2005 9:55:23 PM
flash actionscript: This code will create and delete an object if you just place it on a frame, but
if you put it inside of a function then the object wont delete. Why is that
and how can I get around it?


var test:Object = new Object();
test.myValue = "hello world!";
trace( test.myValue );
if( !delete test )
{
trace( "no delete" );
}
else
{
trace( test.myValue );
}

Re: HELP!! Deleting Objects inside of a function NSurveyor
10/24/2005 10:03:39 PM
Why do you need to delete a variable inside a function? It seems pointless,
because when you make a local var inside a function, it won't exist outside the
function. For example:

hi();
function hi(){
var x = "hi";
trace(x);//outputs hi
}
trace(x);//outputs undefined

Re: HELP!! Deleting Objects inside of a function chrebel
10/24/2005 10:50:37 PM
Re: HELP!! Deleting Objects inside of a function NSurveyor
10/24/2005 10:59:49 PM
Re: HELP!! Deleting Objects inside of a function flashdefined
10/25/2005 5:58:25 AM
First of all, *never* use:

test = null;

This doesn't release the variable from memory, just assigns a null value to it
(trace it and it returns "null", not "undefined").

use:

delete test;

Some of what you said is confusing, so I'm making some assumptions here. For
instance, I assume when you say add "objects" you are talking about movieclips,
not actual Object() objects.

Moving something "off the stage" doesn't do anything, unless you mean you are
using the "delete" command to remove them. If you are adding movieclips,
always use "myClip.removeMovieClip()" to remove the movie from memory, for
objects always use "delete myObject".

As to where you store information for a clip, if you are using classes (and
you should start if you're not, they are much easier to deal with) then attach
an AS class to your library movieclips (under 'properties' > AS 2.0 class) that
use setters/getters for your values. Then, when you use removeMovieClip() to
remove the movie, all associated variables and values are destroyed as well.

Note: you don't have to use getters/setters for this to work, but it makes for
a much cleaner class. For that matter, this works if you just attach the
variables right onto the movieclip, like myMovie.myVar = 9, but the more you do
this the messier your application will become.
Re: HELP!! Deleting Objects inside of a function Robert Tweed
10/25/2005 6:35:53 AM
[quoted text, click to view]

test = null;

Re: HELP!! Deleting Objects inside of a function Robert Tweed
10/25/2005 8:32:37 AM
[quoted text, click to view]

Of course it does. ActionScript uses reference counting (Flash 8 uses
mark and sweep, but it's backwards compatible). When there are no longer
any references to the object, it is removed from the heap. If this
variable is the only reference, setting the variable to any other value
like null, 0, whatever you like, will remove the object from RAM.

Remember that the OP is talking about local variables in a function,
e.g., "var someVariable;". There is no way to physically delete such a
variable from the stack until the function ends and it can be removed
automatically.

From the ActionScript help:
"Predefined objects and properties, and variables declared with
var, may not be deleted."

The variable will still be using a few bytes of RAM, but that is freed
when the function ends and the local variable goes out of scope.
Hopefully the few bytes of stack space isn't what the OP is worried
about - it's whatever space is used by the [presumably large] object
bound to the variable.

If there are a lot of variables using up stack space and that actually
matters (like in a recursive function - normally it wouldn't matter at
all) then you'd have to declare a single object with var, and allocate
properties within it. Those properties can then be deleted when they are
no longer needed (although that method would be a lot slower because of
all the extra memory allocation and garbage collection going on in the
background, so I wouldn't recommended it for general use).

Re: HELP!! Deleting Objects inside of a function Robert Tweed
10/25/2005 8:55:14 AM
[quoted text, click to view]

Just thought I'd add something to this. While the technique of setting a
variable to null to mark an object for deletion does work with mark &
sweep, it doesn't work instantly like it does with reference counting.
This means that anything that creates and deletes a lot of objects _may_
use different amounts of RAM depending on whether it's run on the Flash
7 or Flash 8 player, depending on how quickly the Flash 8 engine gets
around to deleting the unused objects. There's a good discussion about
the differences here:

http://www.kaourantin.net/2005/09/garbage-collection-in-flash-player-8.html

A particularly good illustration from the comments is this:

Tinic Uro said...
"BIT-101: One example: Particle systems. If you have objects
representing particles, reuse the objects when they die
instead of allocating new ones. Allocating large amounts of
objects in a short time can kill the GC since unlike with the
referencing counter model object are not freed right away
anymore. So when objects die, put them into a recycle
container from which you create more particles."

Of course you can't just reuse the space for an object if you want a
totally different type of object - in that case you'll still be invoking
the GC, perhaps without realising it. But this is a good example because
it shows a situation where you have a large number of objects, all the
same type, which are continually being "deleted" and "created", but in
such an instance, making your own mini memory manager for recycling
those objects is a good idea.

There isn't really any discussion of whether the same applies to movie
clips or not. For example, it might be better to put movieclips offstage
temporarily and reuse them instead of unloading and reattaching them,
but then again it might not. That's probably a situation that would call
for some profiling.

AddThis Social Bookmark Button