Jump to content
Search Community

Null Object reference in ContentDisplay when disposing.

jstarrdewar test
Moderator Tag

Recommended Posts

I'm not sure if I'm misusing the SWFLoader's rawContent or not:

 

The use case is that I am accessing the rawContent as a MovieClip, storing it in an instance variable, and then adding it to the stage. Later on when I'm destroying things for garbage collection, I remove it from the stage. At the same time, I call dispose(true) on all of my SWFLoaders, indiscriminately (I'm not accessing rawContent from all of them in the same way). When I do that, I get a null object reference on line 383 of ContentDisplay.

 

I solved the problem by making the following modification:

if (_rawContent.parent && _rawContent.parent == this)

 

I just wanted to know what your opinion of this is. Is calling dispose(true) instead of dispose() useless in this case?

Link to comment
Share on other sites

What version of ContentDisplay are you using? It sounds like you might have a pretty stale version because the latest one has that code wrapped in an if (_rawContent != null), so you shouldn't be getting any null reference errors. Maybe I missed something, though - can you make sure you've got the latest version and then try again?

 

And yes, if you want to completely obliterate the loader AND its content, you should dispose(true), not dispose().

Link to comment
Share on other sites

It's the latest version. I downloaded it last night right before I posted to make sure that you hadn't already addressed the issue. The trouble seems to be that rawContent is not getting set to null. I tried to manually set it to null by doing this:

loader.rawContent = null

 

However I got an error that the rawContent was a read only property. I didn't try setting the ContentDisplay.rawContent to null though, because I wasn't storing a reference to the ContentDisplay object.

 

Here's the whole function with my modification:

	public function set rawContent(value:*):void {
		if (_rawContent != null && _rawContent != value) {
			if (_rawContent.parent && _rawContent.parent == this) {
				removeChild(_rawContent);
			} else if (_cropContainer != null && _rawContent.parent == _cropContainer) {
				_cropContainer.removeChild(_rawContent);
				removeChild(_cropContainer);
				_cropContainer = null;
			}
		}
		_rawContent = value as DisplayObject;
		if (_rawContent == null) {
			return;
		}
		addChildAt(_rawContent as DisplayObject, 0);
		_update();
	}

Link to comment
Share on other sites

The following code also avoids the issue without modifying ContentDisplay:

 

var content:ContentDisplay = LoaderMax.getLoader(url).content;
var movieClip:MovieClip = content.rawContent as MovieClip;
content.rawContent = null;
//work with movieClip, then call dispose(true) on the loader

 

But I think it looks a bit convoluted.

Link to comment
Share on other sites

Could you send me a super simple example FLA that demonstrates the issue because it makes absolutely no sense to me since the line you mentioned couldn't possibly have a null object reference because it's nested in a condition that ensures _rawContent != null. So even if _rawContent.parent is null, that wouldn't break anything because when it checks to see if _rawContent.parent == this, that's the same as saying if null == this which would be a perfectly valid conditional statement. Where could the null object reference come into play there?

 

Maybe I'm missing something obvious, though - I'd sure like to see an FLA that I could publish to get the error myself and then poke around a bit. Would you be so kind as to post something like that (along with any support files)? No need to include production files.

 

And by the way, you really don't need to set rawContent to null like that. As long as you're calling dispose(true) at some point later, everything will be cleaned up.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...