2007. október 29., hétfő

Async APIs and Callback Functions

"Making the FileStream a member variable is also essential. If the FileStream were instead declared inside someFunction(), it would go out of scope when someFunction() returned and could be garbage collected before the file is read or events are delivered. Many people are surprised by this, but note that the only reference to the FileStream object is the member variable. The FileStream object has references on event listeners, etc. but only references to an object will protect it from collection."

Now, this will almost never happen to you because (a) the garbage collector runs incrementally and it'll take it a while to collect the FileStream object and (b) most file reads will finish and dispatch a complete event long before then. But it can happen, which of course means it'll probably only show up when doing big important demos.

http://blogs.adobe.com/simplicity

"It turns out I was incorrect about this and that some related objects, such as URLStream, are guaranteed to remain live until after they have dispatched a completion event. This behavior is both more useful and more intuitive; after all, it only makes sense that there would be a reference to the URLStream somewhere in the system while it's hanging around receiving data."

http://blogs.adobe.com/simplicity

0 megjegyzés: