How to delete an object?

How do I delete an object, especially a DisplayObject in the display list, permanently?

is there no function that just does that(like ‘delete’ in AS2)?

the code is actually quite long, spanned in different classes, but here is the basic idea of what i am doing:
-in the document class, i have a Loader object, movieLoader, this loads swf’s or images.
-in another class, instantiated inside the doc class, I have a contentHolder Sprite object. When the movieLoader of the first class finishes loading, contentHolder adds movieLoader as it’s child.
-i basically want to delete the movieLoader that has become a child of contentHolder, OR, just delete the movieHolder instance instead.

lol i’m pretty much lost with this. here’s the part of my project that’s dealing with the loading stuff.

Document Class


...
private var _contentLoader:Loader = new Loader();
_contentLoader.addEventListener(Event.COMPLETE, content_loaded);
_contentLoader.load("some_CPU_intensive.swf");
...
private function content_loaded(event:Event):void{
	trace("content loaded");
	MovieItem(_shuffler.focusedItem).addContent(_contentLoader);
	MovieItem(_shuffler.focusedItem).hideSnapshot();
	_shuffler.mouseChildren = true;
	_menu.mouseEnabled = true;
	_menu.mouseChildren = true;
}

MovieItem class


private var _contentHolder:Sprite = new Sprite();
...
addChild(_contentHolder);
...
public function addContent(content:DisplayObject):void{
//I want to delete any children _contentHolder might have before adding 'content' as it's new child
//but removeChild is not enough to delete it from memory...
	var tempHolder:DisplayObject = content;
	try{
		_contentHolder.removeChildAt(0);
	}catch (error:Error){
		
	}
	content.alpha = 0;
	TweenLite.to(content, 1, {alpha: 1}); 
	_contentHolder.addChild(tempHolder);		
}

I just want to have one swf playing at any one time, but I could have several MovieItems, but the problem is noticed when I have several CPU intesive swf’s playing in the background. How do i remove those swf’s?
here is a link of what i am talking about, (click on the 5th item several times to open multiple instances of it.)
http://mindfock.com/site/MindfockProject/mf.htm

i think i might have a solution to my problem, but first i need to do this:


_contentHolder.getChildAt(0) = null;

to nullify it’s child(ren)…
but doing that throws an error… and also, if the child at 0 is null, how will I remove it then? :confused:

ok, here is an updated code which deals with the adding and loading of the swf
Document class (still the same)


...
private var _contentLoader:Loader = new Loader();
_contentLoader.addEventListener(Event.COMPLETE, content_loaded);
_contentLoader.load("some_CPU_intensive.swf");
...
private function content_loaded(event:Event):void{
    trace("content loaded");
    MovieItem(_shuffler.focusedItem).addContent(_contentLoader);
    // ^ what happens here? is _contentLoader passed by reference, or by value? 
    MovieItem(_shuffler.focusedItem).hideSnapshot();
    _shuffler.mouseChildren = true;
    _menu.mouseEnabled = true;
    _menu.mouseChildren = true;
}

MovieItem class updated


...
private var _contentHolder:Sprite;
...
public function addContent(content:DisplayObject):void{
	// check if _contentHolder exisits. If so, remove and nullify it
	try{
		removeChild(_contentHolder);
	}catch (e:Error){
		trace("enk!");
	}
	_contentHolder = null;
	// create new _contentHolder and set it up
	_contentHolder = new Sprite();
	addChildAt(_contentHolder, (getChildIndex(letterbox) - 1));
	initMasker();
	_contentHolder.x = -_width / 2 + 10;
	_contentHolder.y = -_height / 2 + 10;
	masker.x = _contentHolder.x;
	masker.y = _contentHolder.y;
	_contentHolder.mask = masker;
	_contentHolder.alpha = 0;
	TweenLite.to(_contentHolder, 1, {alpha: 1}); 
	
	var tempHolder:DisplayObject = content;//reference to content being added
			
	_contentHolder.addChild(tempHolder);//add content into _contentHolder
			
	//set content to null. Doesn't seem to do anything (?)
	content = null; 
	tempHolder = null; 
}
// takeSnapshot: this is where I want the _contentHolder and its children to be deleted
public function takeSnapshot():void {
		_taken = true;
		this.mouseChildren = false;
		snapshotData = new BitmapData(_width - 20, _height - 20, false, 0xFFFFFF);
		snapshotData.draw(_contentHolder);
				
		snapshot.bitmapData = snapshotData;
				
		snapshotHolder.visible = true;
		snapshotHolder.alpha = 1;
				
		// remove _contentHolder from display list
		try{
			trace("contentHolder deleted");
			removeChild(_contentHolder);
		}catch (error:Error){
			trace("contentHolder cannot be deleted");
		}
		_contentHolder = null; // nullify it for GC. 
		//trace("snapshotTaken");
}

I’ve searched my code, and those are the only 2 functions that _contentHolder is ever referenced. And in the Document class, content_loader is never added as a child to the main stage, only passed as argument to addContent function in MovieItem class.

I tried nullifying everything in the MovieItem class (i might have missed something I didn’t see), but it’s still giving me problems… :-/

any help would be very much appreciated…

hmm… no one’s replying… but I’m not giving up.
I did a little test:
i have a blank swf (testSwf.swf) with this code inside:


var i:int = 0;
function tracing(event:Event):void{
	trace("trace " + i + " " + event.target.parent);
	i++;
}
this.addEventListener(Event.ENTER_FRAME, tracing);

which outputs:


trace 0 [object Stage]
trace 1 [object Stage]
trace 2 [object Stage]
trace 3 [object Stage]
trace 4 [object Stage]
trace 5 [object Stage]
trace 6 [object Stage]
trace 7 [object Stage]
trace 8 [object Stage]
trace 9 [object Stage]
...
...

I have another movie (testLoader) which loads that swf into it. it has a btn movieclip in it:


var l:Loader = new Loader();
var s:Sprite = new Sprite();

l.addEventListener(Event.COMPLETE, loaded);
btn.addEventListener(MouseEvent.CLICK, unloaded);
addChild(s);

l.load(new URLRequest("testSwf.swf"));

function loaded(e:Event):void{
	s.addChild(e.target.loader);
	
	trace("loaded");
}
function unloaded(e:Event):void{
	l.unload();
}

now, when I click on btn to unload the swf, i get this in the output:


...
trace 10 [object Loader]
trace 11 [object Loader]
trace 12 [object Loader]
trace 13 [object Loader]
trace 14 [object Loader]
trace 15 null
trace 16 null
trace 17 null
trace 18 null
...

i’m having similar effects in my project… it’s clear that the movie is still in there, but it doesn’t have a parent…
what’s going on?

You have to remove the event listeners that’s listening to the item you are trying to delete from memory.

^how do I do that? is it possible to do it in the movie doing the loading?

Okay - here is the bad news - it is completely impossible to do clear all the references and get the object to delete from the parent object. It cannot be done, and it’s a big fat flaw in AS3. It makes public gallery sites ■■■■■■ near impossible to run, since you cannot clear the loaded objects from the memory without helper code inside the loaded swf.

What you’re going to have to do is set up your loaded objects to clear their own references through some sort of public function, and make sure to call that before removing the object.

:-/

what should the code be in that function? the code being on the timeline of the loaded swf.
i found this [U]article[/U] (just last night sigh), and i think i have the same problem.
If I do, will removing the event listeners in the loaded swf be enough to clear them from memory?

Well, that article is very interesting. Did you notice the post mentioning that some changes have been made to FP10 to try and mitigate this?

Of course I doubt that we’ll have any proper fixes for FP9, which we’re all still using, which is a pity. Kind of like the FP8/AS2 whoops-we-don’t-trigger-KEY_UP-events-properly bug that worked in FP7/AS2 and FP9/AS2 but remains broken if you publish FP8/AS2. Sigh… :wink:

man, this sucks… I thought it would be an easy fix. so, having the code at post #7, do you think removing all event handlers in my loaded swf do the trick?
(edit: I have movieclips in there having enterframe event handlers, do I also need to remove them all? :worried: )

what about loading images or swf’s with no code (just timeline animation), will there still be a problem?