Possible vc++ bug: using c++ references to managed objects in unmanaged code 
Author Message
 Possible vc++ bug: using c++ references to managed objects in unmanaged code

I recently tracked down some mysterious crashes due to a bug in some
unmanaged code using managed objects.  I had instinctively created a C++
reference to the managed object, something like:

class MyClass {
   ManagedClass& m_mc;
public:
   MyClass( ManagedClass& mc ) : m_mc( mc ) {}

Quote:
}

Visual C++ 7 compiled this without any complaints, and this code has
been in the code base for a while without causing any problems.

Since you can't declare a pointer to a managed class in unmanaged code,
I feel you should similarly not be able to declare a reference.

Cheers,

-Mark

ps.  In solving this, we found the gcroot<> template class.  It looks to
be incredibly useful, but are there any issues with using it?  It only
appears twice in the documentation, and isn't in the index.



Mon, 16 May 2005 05:21:02 GMT  
 Possible vc++ bug: using c++ references to managed objects in unmanaged code
Hi Mark,

As you've discovered, you cannot directly hold a reference to a managed
objects in an unmanaged class.  The problem is that all managed objects need
to have their lifetime monitored by the .NET garbage collected heap and the
gc heap has no way of knowing what the unmanaged class does with the
reference.  Since the gc heap isn't monitoring the reference you hold in
your unmanaged class, once all other references to the object go away it
will become a candidate for garbage collection and may be deleted out from
under you.  This is not a bug; it's just the way things work in the managed
world.

As you suspect, the gcroot class can help you out.  It is a templated smart
pointer wrapper over a .NET provided type called a GCHandle.  This type is
specifically designed to hold a managed reference in unmanaged code.  It is
true that there is not too much information on gcroot in  the docs but there
is more about GCHandle if you want to learn the details of what gcroot
wraps.  In addition, there is a mention of how to use gcroot in the
following two MSDN articles:

http://msdn.microsoft.com/msdnmag/issues/01/07/vsnet/default.aspx
http://msdn.microsoft.com/msdnmag/issues/02/02/ManagedC/default.aspx

There is also a sample in the product (UsingMCppGCRoot) that demonstrates
using gcroot to held a .NET string object reference in an unmanaged C++
class.  You might want to have a look at that.

HTH,
--
Ron Pihlgren / Dan Spalding
Visual C++ Team
This posting is provided "AS IS" with no warranties, and confers no rights.


Quote:
> I recently tracked down some mysterious crashes due to a bug in some
> unmanaged code using managed objects.  I had instinctively created a C++
> reference to the managed object, something like:

> class MyClass {
>    ManagedClass& m_mc;
> public:
>    MyClass( ManagedClass& mc ) : m_mc( mc ) {}
> }

> Visual C++ 7 compiled this without any complaints, and this code has
> been in the code base for a while without causing any problems.

> Since you can't declare a pointer to a managed class in unmanaged code,
> I feel you should similarly not be able to declare a reference.

> Cheers,

> -Mark

> ps.  In solving this, we found the gcroot<> template class.  It looks to
> be incredibly useful, but are there any issues with using it?  It only
> appears twice in the documentation, and isn't in the index.



Tue, 24 May 2005 03:57:31 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Referencing data from unmanaged code to managed code in C++ Wrapper class

2. Debugging into managed c++ DLL that is being used by unmanaged code

3. Fatal Error C1010 in Mixing Managed C++ and Unmanaged C++ Code

4. Compiler bug when mixing managed and unmanaged code

5. bug for transition of bools between managed and unmanaged code

6. Managed c++ with unmanaged code Linking problem...

7. C++ Dot net: calling managed method from unmanaged code

8. problem of unmanaged exception in managed code C++

9. Debugging mixed managed/unmanaged c++ code

10. Unmanaged VC++ from Managed C++

11. Crazy mutual referencing object with .h and .cpp in managed c++

12. calling managed code from unmanaged code

 

 
Powered by phpBB® Forum Software