Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2004

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bern McCarty - 25 May 2004 20:28 GMT
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
Bern McCarty - 28 May 2004 18:32 GMT
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

> I am using MEC++ in VC 7.1.  I had a method on a __gc object that looked
> like this:
[quoted text clipped - 38 lines]
>
> -Bern

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.