I read the microsoft documentation about pin_ptr
and it is not clear to me if it is release automatically or not. Can anyone shed some light?
Asked
Active
Viewed 804 times
2

Lion King
- 495
- 5
- 14
-
5Why does this quote from the linked doc not answer your question? _"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 de-referencing one of them could raise an unrecoverable exception."_ – Richard Critten Jan 23 '18 at 14:59
-
1It is automatic. Works the exact same way as the `fixed` keyword in C# does, cheapest way to pin. Talking about "scope block" is fairly misleading, variables in IL have no scope and the garbage collector already knows when a variable can no longer be used, but in practice it works that way. – Hans Passant Jan 23 '18 at 16:42
1 Answers
5
Going off of this bit
The object is no longer pinned when its pinning pointer goes out of scope, or is set to nullptr.
it looks like you don't have to call any release function on a pin_ptr; that it is done automatically.

hegel5000
- 876
- 1
- 7
- 13
-
1What does a smart pointer have to offer if it never automatically releases it's resource? – François Andrieux Jan 23 '18 at 15:05
-
If a smart pointer requires manual deletion, how can it be called a smart pointer? In what way is it smarter than an ordinary pointer? – Richard Critten Jan 23 '18 at 15:05
-
I suggest you remove the irrelevant part on "bad smartpointers". This seems to be what makes people to downvote the acceptable answer. – SergeyA Jan 23 '18 at 15:11
-
-
What I was thinking of is VTK, where every object contains an internal reference count, and that's incremented/decremented with a Register and UnRegister function. Eventually they added a separate vtkSmartPointer which calls those functions for you. So you know, a more typical smart pointer on top of their own very special smart pointer :p – hegel5000 Jan 23 '18 at 15:44
-
1Possibly because VTK is language agnostic https://www.vtk.org/features-language-agnostic/ or the smartpointer was added before C++ had move semantics. – Richard Critten Jan 23 '18 at 15:48
-
@YSC OP said he had already read the document that was quoted. So quoting it again is not going to help. Why it was unclear (to the OP) needs to be asked and answered, then we can help. – Richard Critten Jan 23 '18 at 16:06