Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual C++
Reference
C/C++ Languages
pin_ptr
 How to: Define the Scope of Pinning...
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual C++ Language Reference
How to: Define the Scope of Pinning Pointers

An object is pinned only while a pin_ptr points to it. The object is no longer pinned when its pinning pointer goes out of scope, or is set to nullptr. After the pin_ptr goes out of scope, the object that was pinned can be moved in the heap by the garbage collector. Any native pointers that still point to the object will not be updated, and dereferencing one of them could raise an unrecoverable exception.

Example

// pin_ptr_scope.cpp
// compile with: /clr
ref class G {
public:
   int i;
};

int * f(G ^ hG) {
   pin_ptr<int> ppi = &(hG->i);
   int * i = ppi;
   return i;
}

int main() {
   G ^ MyG = gcnew G;
   int * i;
   i = f(MyG);   // bad!  object that i points to is no longer pinned
}

See Also

Reference

pin_ptr

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker