Is there an easy way to pin a pointer that is a member of a class, and
leave that pointer pinned for the duration of the class's existence?
The pointer would presumably be pinned inside the class's constructor,
but maintain the pin after exiting the constructor.
The syntax, at least under C++/CLI, seems to require simultaneous
declaration and pinning. Any way around that? 7 4898
> Is there an easy way to pin a pointer that is a member of a class, and leave that pointer pinned for the duration of the class's existence? The pointer would presumably be pinned inside the class's constructor, but maintain the pin after exiting the constructor.
The syntax, at least under C++/CLI, seems to require simultaneous declaration and pinning. Any way around that?
if you try to make a pinnend pointer a data member of a class, you get
compiler error C3831: pinnend pointer cannot be a data member or a return
value.
I think this is impossible. If I am wrong someone please correct me, but I
think the reason is this:
If you could do this and pin a member in the constructor, your object would
be be collectible or movable by the GC.
pinning any class member prevents the object from being moved by the GC.
this means that all instances of your class would be fxed in the managed
heap, leading to serious heap fragmentation, which is not the intention.
I am also not sure what would happen when you would try to delete the object.
the GC cannot manage you object as long as the member is pinned. since the
finalizer is called by the GC, it would never be able to finalize your
object??
Anyway, you should only pin data for temporary internal or external use, and
for as short a time as possible to prevent heap fragmentation.
--
Kind regards,
Bruno. br************* *********@hotma il.com
Remove only "_nos_pam"
_iycrd wrote: Is there an easy way to pin a pointer that is a member of a class, and leave that pointer pinned for the duration of the class's existence? The pointer would presumably be pinned inside the class's constructor, but maintain the pin after exiting the constructor.
There are a few (very rare) situations when pin_ptr doesn't work,
because it uses the stack syntax, and it auto-releases the pin when your
function exits (or when the pinned pointer goes out of scope). Read
Ronald Laeremans' first answer in this thread to learn how to pin
pointers manually and keep them pinned using a pinning handle: http://tinyurl.com/o9qkp
With this techinque, you can manually pin an object, and you decide when
you wish to release the pin.
Whether this makes sense in your case is a different story. I would
personally avoid doing it. The big problem with it is that pinned
objects can't be moved around in the memory, and therefore your managed
heap will get fragmented if you keep a lot of objects pinned for a long
time. Keep this in mind, and consider pinning the pointer for only a
brief period of time while an unmanaged function is being executed.
There is a reason why the pin_ptr uses a stack syntax. If you need
something pinned indefinitely, you should rethink your design. You could
just copy your managed data into an unmanaged buffer, which doesn't
require pinning.
Tom
On Wed, 08 Mar 2006 09:59:46 -0800, Tamas Demjen <td*****@yahoo. com>
wrote: _iycrd wrote: Is there an easy way to pin a pointer that is a member of a class, and leave that pointer pinned for the duration of the class's existence?
[pinning handle]http://tinyurl.com/o9qkp The big problem with it is that pinned objects can't be moved around in the memory, and therefore your managed heap will get fragmented if you keep a lot of objects pinned for a long time. Keep this in mind, and consider pinning the pointer for only a brief period of time while an unmanaged function is being executed. There is a reason why the pin_ptr uses a stack syntax. If you need something pinned indefinitely, you should rethink your design. You could just copy your managed data into an unmanaged buffer, which doesn't require pinning.
Thanks for the info, Tom. I was not familiear with pinning handles.
Is this still relevant with C++/CLI?
In this case I am trying to come up with a good design for wrapping a
native DLL in a C++ class for access by C#. There will be one object,
and it will be alloc'd at the start of the program, freed at the end,
so I don't anticipate much fragmentation.
I'm having trouble with the approach I was trying to take (weird
runtime error, I'll post about that separately). PInvoke seems too
complex. I can't wrap the unmanaged code directly via C++/CLI (can't
embed unmanaged data in the ref class).
So I've ended up with an C++/CLI #pragma unmanaged shell class talking
to the native DLL, with a managed C++/CLI wrapping that. Ugly. I'm
looking for alternatives.
_iycrd wrote: Thanks for the info, Tom. I was not familiear with pinning handles. Is this still relevant with C++/CLI?
Yes, the C++/CLI pin_ptr<> is stack-based too. I haven't used pinning
handles, only pin_ptr.
I'm having trouble with the approach I was trying to take (weird runtime error, I'll post about that separately). PInvoke seems too complex. I can't wrap the unmanaged code directly via C++/CLI (can't embed unmanaged data in the ref class).
You can use IJW instead of PInvoke. A ref class can hold unmanaged
pointers, so you can do this:
class UC
{
};
ref class MC
{
public:
MC() : unmanaged(new UC) { }
~MC() { delete unmanaged; }
private:
UC * unmanaged;
};
You just have to create "unmanaged" with new, and delete it from the
destructor, and ensure that the C# program is calling Dispose on it:
MC mc = new MC();
[...]
mc.Dispose();
Tom
_iycrd wrote:
<snip> In this case I am trying to come up with a good design for wrapping a native DLL in a C++ class for access by C#. There will be one object, and it will be alloc'd at the start of the program, freed at the end, so I don't anticipate much fragmentation.
I'm having trouble with the approach I was trying to take (weird runtime error, I'll post about that separately). PInvoke seems too complex. I can't wrap the unmanaged code directly via C++/CLI (can't embed unmanaged data in the ref class).
The usual idiom is :
ref class wrapper
{
public:
wrapper()
{
m_unmanaged_obj ect=new unmanagedobject ();
}
~wrapper()
{
m_unmanaged_obj ect=new unmanagedobject ();
}
private:
unmanagedobject * m_unmanaged_obj ect;
};
This way, the native object is created by the native runtime and is created
outside of the GC heap. Therefore:
- the object is "pinned" (it doesn't move around in memory)
- it doesn't disturb the GC by fragmenting the GC heap.
Arnaud
MVP - VC
Arnaud Debaene wrote: _iycrd wrote: <snip> In this case I am trying to come up with a good design for wrapping a native DLL in a C++ class for access by C#. There will be one object, and it will be alloc'd at the start of the program, freed at the end, so I don't anticipate much fragmentation.
I'm having trouble with the approach I was trying to take (weird runtime error, I'll post about that separately). PInvoke seems too complex. I can't wrap the unmanaged code directly via C++/CLI (can't embed unmanaged data in the ref class).
The usual idiom is :
ref class wrapper { public: wrapper() { m_unmanaged_obj ect=new unmanagedobject (); } ~wrapper() { m_unmanaged_obj ect=new unmanagedobject ();
I'm sure you meant:
delete m_unmanaged_obj ect;
-cd
Carl Daniel [VC++ MVP] wrote: Arnaud Debaene wrote: _iycrd wrote: <snip> In this case I am trying to come up with a good design for wrapping a native DLL in a C++ class for access by C#. There will be one object, and it will be alloc'd at the start of the program, freed at the end, so I don't anticipate much fragmentation.
I'm having trouble with the approach I was trying to take (weird runtime error, I'll post about that separately). PInvoke seems too complex. I can't wrap the unmanaged code directly via C++/CLI (can't embed unmanaged data in the ref class).
The usual idiom is :
ref class wrapper { public: wrapper() { m_unmanaged_obj ect=new unmanagedobject (); } ~wrapper() { m_unmanaged_obj ect=new unmanagedobject ();
I'm sure you meant:
delete m_unmanaged_obj ect;
Urgh! Of course, sorry :-(
Arnaud
MVP - VC This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: MLH |
last post by:
I've read some posts indicating that having tons of GV's in
an Access app is a bad idea. Personally, I love GVs and I
use them (possibly abuse them) all the time for everything
imaginable - have been for years. If the machine has memory
to spare and windows can use it - I'm thinking "Why not?"
I was wondering what some of you have to say...
|
by: tshad |
last post by:
I have an example I copied from "programming asp.net" (o'reilly) and can't
seem to get the Sub (writefile) to execute. It displays all the
response.write lines that are called directly, but not any of the
response.write lines from inside the sub.
*******************************************
<%@ Application Language="VB" %>
<script...
|
by: randyr |
last post by:
I am developing an asp.net app based on a previous asp application.
in the asp applications global.asa file I had several
<object id="id" runat="server" scope="scope" class="comclass"> tags for
objects that the app used to speed up some global level data access and
functionality.
I have recoded the class libraries in .net and would like...
|
by: T Ray Humphrey |
last post by:
I could not get the following code to compile:
public ref class wrapper
{
void Read(int^ mpLen)
{
pin_ptr<int> pLen = mpLen;
unmanaged* punman = new unmanaged();
punman->Read(pLen);
return;
}
|
by: Tao.Young |
last post by:
when i doing this:
pin_ptr<A> pA = gcnew A; // assume A is a ref class
i'll get many compiler errors.
does anyone know how to do it correctly?
Thanks in advance.
| |
by: _DS |
last post by:
My managed code needs to shuttle an unmanaged buffer between a couple
unmanaged functions. I know this could be done via pin_ptr, and I
understand the implications re managed heap fragmentation (I'll set it
up at the start of the program and leave it).
I believe that this could also be done by old-fashioned #include
<malloc.h> with...
|
by: Nobody |
last post by:
I posted this bug to MS, but was wondering if someone else could try
inserting the following into a C++/cli project and seeing if it causes the
compiler to barf:
array<float,2>^ fMatrix = gcnew array<float,2>(4,4);
pin_ptr<float> pf = &(fMatrix);
It seems to work for int types but not for float and double.
|
by: Bruce |
last post by:
public ref class GpsDevice abstract
{
/* Please note GarXface4::GpsDevice* is in a different namespace
from this one and GarXface4::GpsDevice is an unmanaged class */
virtual GarXface4::GpsDevice* GetDevice() = 0;
};
|
by: David Lowndes |
last post by:
I've got a project with a fair bit of mixed C++/CLI calling native
code, often passing raw pointers - and I'm worrying that we may have
occasions where we don't use pin_ptr when we need to.
However, after a few experiments I was unable to compile code (VS2005)
that casually tried to do the wrong thing.
So, is anyone aware of any common...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |