Open main menu

CDOT Wiki β

Changes

Discussion of Using XPCOM Components

553 bytes added, 15:58, 19 November 2008
nsCOMPtr
nsCOMPtr is designed to help developers not leak memory, and to make it easy to work with interfaces. It is a so-called smart pointer: a template class that acts just like a regular pointer, but which adds AddRef, Release, and QueryInterface.
An XPCOM object is accessed via a pointer to an abstract base class (the interface type). Each interface pointer keeps track of how many references to it are being held. As new variables are created and clients get new references, AddRef is called, and the number goes up. When interface pointers go out of scope, the nsCOMPtr decreases the reference count, and when that goes to 0, deletes the object automatically.  The [https://developer.mozilla.org/En/NsISupports/QueryInterface QueryInterface] method of nsCOMPtr is used to do runtime type discovery, allowing us to see if a type implements an interface, and if it does, obtain a pointer to that interface. For example, we know that nsICookieManager2 inherits from nsICookieManager, so given an nsICookieManager we might wish to obtain a pointer to an nsICookieManager2 in order to get extra functionality:  nsCOMPtr<nsICookieManager2> cookieMgr2 = do_QueryInterface(cookieMgr); if (cookieManager2) { ... }
=== Services ===