asdg>> technotes>> why is movieclip a separate datatype

Version: Flash 5
Date Added: December 3, 2002

This information originally appeared in chapter 15 of ActionScript: The Definitive Guide, First Edition. In the interest of space, it was removed from the second edition, but is reproduced here for posterity.


For the most part, movie clips behave exactly like objects. However, movie clips are not just another class-they are technically their own distinct datatype. Gary Grossman, the creator of ActionScript, explains the difference between the internal implementation of the movieclip and object datatypes as follows:

Movie clips are implemented separately from objects internally in the Player, although both manifest almost identically in ActionScript. The primary difference lies in the way that they are allocated and deallocated. Regular objects are reference-counted and garbage-collected, whereas the lifetime of movie clips is timeline-controlled or explicitly controlled with the duplicateMovieClip(), attachMovie(), createEmptyMovieClip(), and removeMovieClip() functions.

If you declare an array using x = new Array() and then set x = null, ActionScript will immediately detect that there are no remaining references to the Array object (i.e., no variables referring to it), and garbage-collect it (i.e., free the memory it used). Periodic mark-and-sweep garbage collection eliminates objects containing circular references. (That is, advanced techniques are used to ensure that memory is freed when two unused objects refer to each other.)

Movie clips don't behave the same way. They come into and go out of existence depending on the placement of objects on the timeline. If they are created dynamically (e.g., with duplicateMovieClip()) they are disposed of only when removeMovieClip() is used on them.

References to objects are pointers (memory address references); reference-tracking and garbage-collection protect the user from dangling pointers and memory leakage. References to movie clips, however, are soft references--the reference actually contains an absolute target path. If you have a movie clip named foo, and then set x = foo (which makes x a reference to clip foo), and then delete foo using removeMovieClip(), and then create another clip named foo, the reference x will again be valid (it will point to the new foo clip).

Regular objects are different--the existence of a reference to an object prevents that object from being removed in the first place. So if movie clips were objects, removeMovieClip() wouldn't remove the object from memory so long as the variable x made reference to it. Furthermore, if you create a second movie clip named foo, the old foo and the new foo can exist simultaneously, although the old foo would no longer be rendered.

So, a separate movieclip type is appropriate because of its important differences from the object type. For similar reasons, the typeof operator reports "function" for functions, even though functions are also akin to objects in many ways.