473,586 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pin_ptr global to a class

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?

Mar 8 '06 #1
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"

Mar 8 '06 #2
_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
Mar 8 '06 #3
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.

Mar 9 '06 #4
_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
Mar 9 '06 #5
_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
Mar 10 '06 #6
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
Mar 11 '06 #7
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
Mar 12 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
3018
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...
9
2374
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...
15
2458
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...
2
3205
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; }
11
2714
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.
1
1836
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...
3
1252
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.
1
1512
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; };
7
1980
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...
0
7912
marktang
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...
0
8202
Oralloy
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. ...
0
8338
jinu1996
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...
0
8216
tracyyun
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...
0
5390
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...
0
3837
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...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
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
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.