473,326 Members | 2,010 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,326 software developers and data experts.

How easy is it to accidentally not use pin_ptr where you ought to?

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 gotchas where you can have code that
compiles cleanly (and works in all tests) that's not using pin_ptr
where it ought to?

Dave
May 10 '07 #1
7 1967
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 gotchas where you can have code that
compiles cleanly (and works in all tests) that's not using pin_ptr
where it ought to?
I know that there were a number of cases in MC++ that gave you enough rope
to hang yourself.
From what I understood, C++/CLI was designed to prevent you from messing up
like that.
I have not yet encountered a situation where I messed up that part.
Of course, that still doesn't really prove that it is impossible.

--
Kind regards,
Bruno van Dooren MVP - VC++
http://msmvps.com/blogs/vanDooren
br**********************@hotmail.com
May 11 '07 #2
>I know that there were a number of cases in MC++ that gave you enough rope
>to hang yourself.
From what I understood, C++/CLI was designed to prevent you from messing up
like that.
I have not yet encountered a situation where I messed up that part.
Of course, that still doesn't really prove that it is impossible.
Hi Bruno,

That's the sort of response I was hoping for :)

In the cases where I tried to do it wrong, I got a compiler error - so
I was hoping that short of something wildly underhand that it was
unlikely to be an issue that could be missed.

Cheers
Dave
May 11 '07 #3
I know that there were a number of cases in MC++ that gave you enough
rope
to hang yourself.
From what I understood, C++/CLI was designed to prevent you from messing
up
like that.
I have not yet encountered a situation where I messed up that part.
Of course, that still doesn't really prove that it is impossible.

Hi Bruno,

That's the sort of response I was hoping for :)

In the cases where I tried to do it wrong, I got a compiler error - so
I was hoping that short of something wildly underhand that it was
unlikely to be an issue that could be missed.
Hi David,
Short of purposely doing something very ugly in a multithreaded environment,
I don't think you can mess up. And even then, the runtime and compiler
should prevent you from doing something that bad.

--
Kind regards,
Bruno van Dooren MVP - VC++
http://msmvps.com/blogs/vanDooren
br**********************@hotmail.com
May 11 '07 #4

"David Lowndes" <Da****@example.invalidwrote in message
news:gd********************************@4ax.com...
I know that there were a number of cases in MC++ that gave you enough
rope
to hang yourself.
From what I understood, C++/CLI was designed to prevent you from messing
up
like that.
I have not yet encountered a situation where I messed up that part.
Of course, that still doesn't really prove that it is impossible.

Hi Bruno,

That's the sort of response I was hoping for :)

In the cases where I tried to do it wrong, I got a compiler error - so
I was hoping that short of something wildly underhand that it was
unlikely to be an issue that could be missed.
As long as you are using safe_cast and static_cast, not reinterpret_cast or
old-style casts, the compiler should protect you.
>
Cheers
Dave

May 11 '07 #5
>As long as you are using safe_cast and static_cast, not reinterpret_cast or
>old-style casts, the compiler should protect you.
In the attempt I had, I couldn't even fool it with those - though I
didn't try too hard. Let me rephrase that - I could get something to
compile with the heavy handed casts - but not something that worked.

What I couldn't do was get something to compile that worked (and would
have been susceptible to the managed heap movements that pin_ptr
prevents) - it was those specific situations I was worried about since
they'd pass casual tests but would likely fail in weird unpredictable
ways in the real world.

Dave
May 11 '07 #6
Hi David,

"David Lowndes" <Da****@example.invalidwrote in message
news:hq********************************@4ax.com...
As long as you are using safe_cast and static_cast, not reinterpret_cast
or
old-style casts, the compiler should protect you.

In the attempt I had, I couldn't even fool it with those - though I
didn't try too hard. Let me rephrase that - I could get something to
compile with the heavy handed casts - but not something that worked.

What I couldn't do was get something to compile that worked (and would
have been susceptible to the managed heap movements that pin_ptr
prevents) - it was those specific situations I was worried about since
they'd pass casual tests but would likely fail in weird unpredictable
ways in the real world.

Dave
I do not necessarily agree with the other statements. IMO there *is* a need
for special care with pin pointers:

Let's start with a dumb scenario:

unsigned char* NeverDoThis(array<unsigned char>^ arr)
{
pin_ptr<unsigned charpp(&arr[0]);
return pp;
}

In this case, you pin a pointer to a managed array, use the standard
conversion to get a native pointer, and return the native pointer to the
caller. After the method has returned, the array is no longer pinned and
could be relocated, but the native pointer would still refer to the old
location. Let's have a look at a less dumb scenario:

Assume you have a managed array:
array<unsinged char>^ arr = GetArrayFromSomeWhere();

Further assume you want to pass a native pointer to the array to a native
function like this one:

void DoSth(unsigned char*);

To achieve this, you could write:
{
pin_ptr<unsigned charpp(&arr[0]);
DoSth(pp);
}

Is this code safe? Well that depends on the implementation of DoSth. If
DoSth stores the native pointer to access the array after the call, this
code as a problem, because the pinnng time ends after the pointer left
scope.

Marcus Heege

May 13 '07 #7
Hi Markus,

Thanks for the comments.
>Let's start with a dumb scenario:
I sincerely hope we don't have any of those :) I'm pretty sure we
don't in fact.
>Assume you have a managed array:
...
{
pin_ptr<unsigned charpp(&arr[0]);
DoSth(pp);
}

Is this code safe? Well that depends on the implementation of DoSth. If
DoSth stores the native pointer to access the array after the call, this
code as a problem, because the pinnng time ends after the pointer left
scope.
Yeah, they're both examples of the same situation really - using the
pinned native pointer outside the pinning scope.

I was really more concerned with any more direct situations where you
could fail to use pin_ptr where it was really necessary.

The first example is fairly easy to check for, but as you say the
second one isn't (as we're using a 3'rd party library that we don't
have the source to).

Are there any diagnostic tools that would help flush out problems
where the managed heap moves? Something that explicitly causes lots of
managed heap movements?

Dave
May 13 '07 #8

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

Similar topics

10
by: Brian | last post by:
Hello all, I have an form to Enter a Name with letters from A to Z in buttons. When you click a button, it should append the text associated with that button to a text box. Instead of...
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...
3
by: Terry Hancock | last post by:
Is there an *easy* way to make an object immutable in python? Or perhaps I should say "one obvious way to do it"? Oughtn't there to be one? I've found a thread on how to do this, which...
7
by: _iycrd | last post by:
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...
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...
6
by: Peter Duniho | last post by:
So, I'm trying to learn how the Regex class works, and I've been trying to use it to do what I think ought to be simple things. Except I can't figure out how to do everything I want. :( If I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.