Interfacing with the XPCOM cycle collector: Difference between revisions

Update to new macro, add info about tearoffs and aggregation
No edit summary
 
(Update to new macro, add info about tearoffs and aggregation)
Line 29: Line 29:
# Change the line <tt>NS_DECL_ISUPPORTS</tt> to <tt>NS_DECL_CYCLE_COLLECTING_ISUPPORTS</tt> in the definition of <tt>nsFoo</tt>.
# Change the line <tt>NS_DECL_ISUPPORTS</tt> to <tt>NS_DECL_CYCLE_COLLECTING_ISUPPORTS</tt> in the definition of <tt>nsFoo</tt>.
# Add a line <tt>NS_DECL_CYCLE_COLLECTION_CLASS(nsFoo)</tt> within the public portion of definition of <tt>nsFoo</tt>.
# Add a line <tt>NS_DECL_CYCLE_COLLECTION_CLASS(nsFoo)</tt> within the public portion of definition of <tt>nsFoo</tt>.
# Add a line <tt>NS_INTERFACE_MAP_ENTRY_CYCLE_COLLECTION(nsFoo)</tt> to the interface map of <tt>nsFoo</tt> in <tt>nsFoo.cpp</tt>.
# Add a line <tt>NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(nsFoo)</tt> to the interface map of <tt>nsFoo</tt> in <tt>nsFoo.cpp</tt>.
# Change the line <tt>NS_IMPL_ADDREF(nsFoo)</tt> to <tt>NS_IMPL_CYCLE_COLLECTING_ADDREF(nsFoo)</tt> in <tt>nsFoo.cpp</tt>.
# Change the line <tt>NS_IMPL_ADDREF(nsFoo)</tt> to <tt>NS_IMPL_CYCLE_COLLECTING_ADDREF(nsFoo)</tt> in <tt>nsFoo.cpp</tt>.
# Change the line <tt>NS_IMPL_RELEASE(nsFoo)</tt> to <tt>NS_IMPL_CYCLE_COLLECTING_RELEASE(nsFoo)</tt> in <tt>nsFoo.cpp</tt>.
# Change the line <tt>NS_IMPL_RELEASE(nsFoo)</tt> to <tt>NS_IMPL_CYCLE_COLLECTING_RELEASE(nsFoo)</tt> in <tt>nsFoo.cpp</tt>.
Line 35: Line 35:


It is possible that your class has more complicated structure than this picture. For example, your class may have multiple <tt>nsISupports</tt> base classes, which requires the use of some <tt>*_AMBIGUOUS</tt> macros that perform a disambiguating downcast. Or your class may have a complicated ownership structure, such that the simple <tt>NS_IMPL_CYCLE_COLLECTION_CLASS_<i>N</i></tt> macros are insufficient; in this case you might need to implement the ''Traverse'' and ''Unlink'' methods of your helper class manually. It's helpful even in these cases to use the <tt>NS_IMPL_CYCLE_COLLECTION_TRAVERSE_{BEGIN,END}</tt> and <tt>NS_IMPL_CYCLE_COLLECTION_UNLINK_{BEGIN,END}</tt> macros. You can see an example of their use in some more complicated classes such as <tt>content/base/src/nsGenericElement.cpp</tt>.
It is possible that your class has more complicated structure than this picture. For example, your class may have multiple <tt>nsISupports</tt> base classes, which requires the use of some <tt>*_AMBIGUOUS</tt> macros that perform a disambiguating downcast. Or your class may have a complicated ownership structure, such that the simple <tt>NS_IMPL_CYCLE_COLLECTION_CLASS_<i>N</i></tt> macros are insufficient; in this case you might need to implement the ''Traverse'' and ''Unlink'' methods of your helper class manually. It's helpful even in these cases to use the <tt>NS_IMPL_CYCLE_COLLECTION_TRAVERSE_{BEGIN,END}</tt> and <tt>NS_IMPL_CYCLE_COLLECTION_UNLINK_{BEGIN,END}</tt> macros. You can see an example of their use in some more complicated classes such as <tt>content/base/src/nsGenericElement.cpp</tt>.
If your class has tearoffs or is being aggregated by other classes it is important to make the tearoff classes or the outer classes participate in cycle collection too, not doing so could lead to the cycle collector trying to collect the objects too soon.
65

edits