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

Declaring and using __gc pointers to native structs [MEC++ - VC 7.1]

I am using MEC++ in VC 7.1. I had a method on a __gc object that looked
like this:

__property System::UInt32 get_MyProperty(void)
{
System::Byte __pin * pinBytes = &m_byteArray[0]; // entire array is
now pinned
NativeStruct const __nogc* nativeStructP = reinterpret_cast<NativeStruct
const __nogc*>(pinBytes);

return nativeStructP->myField;
}

Then I made another version of the property that didn't do any pinning.
Instead this version cast the address the first element of the managed byte
array member to a managed pointer to a native class like so:

__property System::UInt32 get_MyPropertyNoPin(void)
{
NativeStruct const __gc* nativeStructP= reinterpret_cast<NativeStruct
const __gc*>(&m_byteArray[0]);

// what happens if the heap is compacted right here? Am I guaranteed to
be OK when I dereference nativeStructP below since I'm using a __gc pointer?

return nativeStructP->myField;
}

It seems to compile and run just fine. Can I really create tracking
references to native structs that, through pointer casting, really live in
the managed heap? Will they track correctly if the heap is compacted? Or
is this just a crash waiting to happen?

I actually didn't even expect this to compile. I would have thought that
even declaring a __gc pointer to a __nogc type would result in a
compile-time error. That it does compile and seems to run has me wondering
if it will actually work reliably. If so, then it means that I can avoid
pinning much more than I thought that I could.

If this will work, will it still be possible to do this in C++/CLI in VC 8?

-Bern


Nov 17 '05 #1
1 1572

I realized after looking at the ILDASM output of the result of this that the compiler wasn't really
generating managed pointers where I was asking it to. This seems like a bad bug. I thought the
only way to convert a managed pointer to an unmanaged pionter was supposed to be via pinning. This
appears to be a hole in the enforcement of that rule. When I do:

NativeStruct const __gc* nativeStructP= reinterpret_cast<NativeStruct const
__gc*>(&m_byteArray[0]);

....nativeStructP is not REALLY a managed pointer if you look at the resulting IL. Rather it is an
unmanaged pointer. And the compiler does not complain about this code at all. It just merrily
ignores the __gc keyword.

-Bern
"Bern McCarty" <be**********@bentley.com> wrote in message
news:e%****************@tk2msftngp13.phx.gbl...
I am using MEC++ in VC 7.1. I had a method on a __gc object that looked
like this:

__property System::UInt32 get_MyProperty(void)
{
System::Byte __pin * pinBytes = &m_byteArray[0]; // entire array is
now pinned
NativeStruct const __nogc* nativeStructP = reinterpret_cast<NativeStruct
const __nogc*>(pinBytes);

return nativeStructP->myField;
}

Then I made another version of the property that didn't do any pinning.
Instead this version cast the address the first element of the managed byte
array member to a managed pointer to a native class like so:

__property System::UInt32 get_MyPropertyNoPin(void)
{
NativeStruct const __gc* nativeStructP= reinterpret_cast<NativeStruct
const __gc*>(&m_byteArray[0]);

// what happens if the heap is compacted right here? Am I guaranteed to
be OK when I dereference nativeStructP below since I'm using a __gc pointer?

return nativeStructP->myField;
}

It seems to compile and run just fine. Can I really create tracking
references to native structs that, through pointer casting, really live in
the managed heap? Will they track correctly if the heap is compacted? Or
is this just a crash waiting to happen?

I actually didn't even expect this to compile. I would have thought that
even declaring a __gc pointer to a __nogc type would result in a
compile-time error. That it does compile and seems to run has me wondering
if it will actually work reliably. If so, then it means that I can avoid
pinning much more than I thought that I could.

If this will work, will it still be possible to do this in C++/CLI in VC 8?

-Bern

Nov 17 '05 #2

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

Similar topics

73
by: David Scarlett | last post by:
Is the following code safe? SomeType *a, *b; /* Some code.... */ free(a);
1
by: TGF | last post by:
Hello, I am calling a Filestream::Read() (i.e. fs->Read(arg1, arg2, arg3)). The problem is I have a pointer to an unsigned char block of memory and would like to fill it with the call to 'Read'....
8
by: Mas L via DotNetMonster.com | last post by:
Hi, I have a c++ source code which I can compile to be a DLL (in VS.NET 2003). And I need to use it in a C# program. After I compiled/build the C++ code to a DLL, I add it as a Reference in my...
3
by: mirek | last post by:
Hi, I've got problem building managed class library to wrap unmanaged code. I created managed class library using standard patten: #include "../Unmanaged/Class1.h" //Class1 unmanaged ...
2
by: Michael Gunter | last post by:
Good afternoon, I am creating an interface in C++ using the managed extensions. I need to declare a parameter as an out string (as would be represented in C#). Does anyone know how to do this?...
2
by: microsoft | last post by:
Hi All, I am fairly new to Managed C++ extensions. I started trying to implement a interface defined in a C# project, in C++. The problem was with passing value arrays from c# to c++. I found...
3
by: m | last post by:
Hi, I'm reading Microsoft Visual C++ .NET Step by Step, Version 2003 and I found a sentence it says : "you can't have pointers to managed types as members of a __gc class.". Why ? Thanks,...
1
by: Achim Domma (Procoders) | last post by:
Hi, I still try to implement a .Net wrapper to handel AVI Files. I'm unsing managed C++ with VStudio 2003. The structure of my classes looks like this: class AudioStream { ... } class...
64
by: Zytan | last post by:
I know there are no pointers in C#, but if you do: a = b; and a and b are both arrays, they now both point to the same memory (changing one changes the other). So, it makes them seem like...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.