Why is a closed NetConnection that does not have event or reference listeners stored in memory?

It seems that if flash.net.NetConnection is created and connected to an HTTP URL (such as an AMFPHP gateway), this instance is never picked up by garbage collection even after it has been closed and the only link set to zero.

On the other hand, if the instance is tied to zero (as it would be when playing video / mp 3 files), the instance is cleared from memory.

To clarify, the following connection will be stored in memory:

var stickyConn:NetConnection = new NetConnection(); stickyConn.connect("http://myserver/amfphp/gateway.php"); stickyConn.close(); stickyConn = null; 

While the following connection will be immediately removed from memory:

 var tempConn:NetConnection = new NetConnection(); tempConn.connect(null); tempConn.close(); tempConn = null; 

Some things that I have already tried to solve this problem:

  • set the client to an empty object (since the default value for the client is NetConnection itself)
  • call connect(null) before closing the connection
  • after closing the connection, call connect(null) and close it again

Has anyone encountered this problem before? Is there a solution for this?

+6
source share
4 answers

I created multi-player FLV / Mp4 players that use AS3 quite often. When I use a service such as Akamai or the internal NetConnection class for Adobe, I always remember the client object .

this is a NetConnection property that ALL callback methods are called on. This instance of NetConnection this used by default. If you set another object for the client property, callback methods will be called on that object.

Thus, you can easily understand how the Garbage Collection has never been applied to each component in the same way. So where is stickyConn = null; stops playback only when you never declared “Weak link”, “Trash collection” does not know what to look for.

I have had success with various ways based on a specific player:

Just specifying NetConnectionObj.client = this . But what if your NetConnection is extended or implements an interface? Just use the null Dictionary object: var d: Dictionary = new dictionary (true) ;, From here, garbage collection will recognize "d" as a weak link and automatically reset it;

Therefore, your fragment will look something like this:
var Dc:Dictionary = new Dictionary(true);
NetConnection:NetConnection.client = Dc;

or some options with the same intent.

I know this works, so lend a hand if you need help ...

0
source

Perhaps I was vague regarding the last answer regarding GC and vocabulary objects. Consider this snippet. I wrote this quickly, but I'm trying to explain the concept of what solves your problem; mainly because I have dealt with this before:

 public class Main extends MovieClip { private var connection:NetConnection; private var __nData:*; private var _instance:*; private var _closure:Function; private var _D:Dictionary; public function Main() { connection = new NetConnection(); connection.addEventListener(NetStatusEvent.NET_STATUS, _nsHandle) connection.connect(null); } public function _nsHandle(event:NetStatusEvent):void { try { connection = new NetConnection(); connection.connect(null); connection.client = RegisterForGC(event.target); RegisterForGC(connection); } finally { __nData = event.target.netConnection; } } public function RegisterForGC(NCObject:*):* { _instance = NCObject; _closure = function ():void {} _listener = function (e:Event):void {} _D = new Dictionary(true); _D[_listener] = "A"; _D[_instance] = "B"; _D[_closure] = "C"; try { new LocalConnection().connect( "A" ); new LocalConnection().connect( "B" ); } catch (anything:*) { } return _instance; } } 
0
source

I'm not sure, but your example tells you that you declare your vars on stage / frame.

Close (); all you need for this to work HOWEVER .... from what I found with NetConnection, for some reason, if all the vars / functions are not declared in the External class, for example. public public functions, It remains in memory even after using close ();

He pulled out my hair, figuring it out using the audio stream. However, as soon as I moved all the encodings to an external class, close (); actually closed the connection.

If your code is on a frame on the scene or inside the MC, I would create a class and declare vars and functions in the specified external Class.as and for some stupid reason it works.

Hope this helps.

0
source

Are you using a NetStream object, but not deleting it upon completion? I only ask because I rarely see NetConnection without a NetStream object far behind it.

-1
source

All Articles