473,387 Members | 1,529 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 4879
> 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**********************@hotmail.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_object=new unmanagedobject();
}
~wrapper()
{
m_unmanaged_object=new unmanagedobject();
}

private:
unmanagedobject* m_unmanaged_object;
};

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_object=new unmanagedobject();
}
~wrapper()
{
m_unmanaged_object=new unmanagedobject();


I'm sure you meant:

delete m_unmanaged_object;

-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_object=new unmanagedobject();
}
~wrapper()
{
m_unmanaged_object=new unmanagedobject();


I'm sure you meant:

delete m_unmanaged_object;


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
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...
9
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...
15
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...
2
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
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
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...
3
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...
1
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...
7
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. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.