B
Bern McCarty
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
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