XRPtr<Interface> (Compact 2013)

3/28/2014

An XRPtr<Interface> object encapsulates a COM interface pointer to a XAML for Windows Embedded object and is called a smart pointer. The template class manages resource allocation and deallocation through function calls to the IUnknown methods: QueryInterface, AddRef, and Release.

Syntax

template<typename Interface> class XRPtr

Methods

Method

Description

XRPtr<Interface>::AddRef

Calls the AddRef method of IUnknown on the encapsulated interface pointer.

XRPtr<Interface>::Attach

Encapsulates a raw interface pointer in this smart pointer's type.

XRPtr<Interface>::Detach

Extracts and returns the encapsulated interface pointer, and relinquishes ownership so that the returned interface will not be released by this smart pointer.

XRPtr<Interface>::Release

Calls the Release method of IUnknown on the encapsulated interface pointer.

Operators

Operator

Description

XRPtr<Interface>::operator ->

Assigns a new value to an existing XRPtr<Interface> object.

XRPtr<Interface>::operator &

Returns the address of the interface pointer contained in this class.

XRPtr<Interface>::operator =

Either sets the interface pointer, copies the interface pointer and increments the reference count, or allows for the assignment of NULL to the class.

Remarks

Smart pointers manage the reference count of an object. A smart pointer will automatically call IUnknown::Release when the smart pointer goes out of scope.

We recommend that you use smart pointers when you call methods that pass IXRDependencyObject-derived objects in XAML for Windows Embedded, instead of explicitly calling IUnknown::QueryInterface to convert a generic interface into the required object type.

All objects in XAML for Windows Embedded have smart pointer aliases declared for them. For example, to create a smart pointer instead of a raw interface pointer to IXRButton, you can create a smart pointer of type IXRButtonPtr. You can also create a smart pointer by using the template class type XRPtr<XRButton>.

The following example code creates a smart pointer by using a smart pointer alias:

IXRButtonPtr pButton;
pApplication->CreateObject(&pButton);

The following example code creates a smart pointer by using the template class:

XRPtr<XRButton> pButton;
pApplication->CreateObject(&pButton);

XRPtr<Interface> stores the return value from an API call (such as a call to IXRApplication::CreateObject), even if the return value is NULL. When you use XRPtr<Interface> with a NULL value, a debug break occurs as a warning that your code might later fail. Check the stack trace for the source of your error. Optionally, in your code, you can undefine the SHIP_BUILD preprocessor definition by using #undef (FMerge.exe) before you use #include (FMerge.exe) to include xrptr.h, and then rebuild and rerun your project. If ERRORMSG is available, debug output appears that tells you the reason for the failure.

Requirements

Header

XRPtr.h

sysgen

SYSGEN_XAML_RUNTIME

See Also

Reference

Classes for Application Management