WeakReference Class
In this article
Supports the WRL infrastructure and is not intended to be used directly from your code.
class WeakReference;
Represents a weak reference that can be used with the Windows Runtime or classic COM. A weak reference represents an object that might or might not be accessible.
A WeakReference
object maintains a strong reference, which is a pointer to an object, and a strong reference count, which is the number of copies of the strong reference that have been distributed by the Resolve()
method. While the strong reference count is nonzero, the strong reference is valid and the object is accessible. When the strong reference count becomes zero, the strong reference is invalid and the object is inaccessible.
A WeakReference
object is typically used to represent an object whose existence is controlled by an external thread or application. For example, construct a WeakReference
object from a reference to a file object. While the file is open, the strong reference is valid. But if the file is closed, the strong reference becomes invalid.
The WeakReference
methods are thread safe.
Name | Description |
---|---|
WeakReference::WeakReference | Initializes a new instance of the WeakReference class. |
WeakReference::~WeakReference | Deinitializes (destroys) the current instance of the WeakReference class. |
Name | Description |
---|---|
WeakReference::DecrementStrongReference | Decrements the strong reference count of the current WeakReference object. |
WeakReference::IncrementStrongReference | Increments the strong reference count of the current WeakReference object. |
WeakReference::Resolve | Sets the specified pointer to the current strong reference value if the strong reference count is nonzero. |
WeakReference::SetUnknown | Sets the strong reference of the current WeakReference object to the specified interface pointer. |
WeakReference
Header: implements.h
Namespace: Microsoft::WRL::Details
Supports the WRL infrastructure and is not intended to be used directly from your code.
virtual ~WeakReference();
Deinitializes the current instance of the WeakReference
class.
Supports the WRL infrastructure and is not intended to be used directly from your code.
ULONG DecrementStrongReference();
Decrements the strong reference count of the current WeakReference
object.
When the strong reference count becomes zero, the strong reference is set to nullptr
.
The decremented strong reference count.
Supports the WRL infrastructure and is not intended to be used directly from your code.
ULONG IncrementStrongReference();
The incremented strong reference count.
Increments the strong reference count of the current WeakReference
object.
Supports the WRL infrastructure and is not intended to be used directly from your code.
STDMETHOD(Resolve)
(REFIID riid,
_Deref_out_opt_ IInspectable **ppvObject
);
riid
An interface ID.
ppvObject
When this operation completes, a copy of the current strong reference if the strong reference count is nonzero.
S_OK if this operation is successful and the strong reference count is zero. The ppvObject parameter is set to
nullptr
.S_OK if this operation is successful and the strong reference count is nonzero. The ppvObject parameter is set to the strong reference.
Otherwise, an HRESULT that indicates the reason this operation failed.
Sets the specified pointer to the current strong reference value if the strong reference count is nonzero.
Supports the WRL infrastructure and is not intended to be used directly from your code.
void SetUnknown(
_In_ IUnknown* unk
);
unk
A pointer to the IUnknown
interface of an object.
Sets the strong reference of the current WeakReference
object to the specified interface pointer.
Supports the WRL infrastructure and is not intended to be used directly from your code.
WeakReference();
Initializes a new instance of the WeakReference
class.
The strong reference pointer for the WeakReference
object is initialized to nullptr
, and the strong reference count is initialized to 1.