Ensuring unmanaged pointers held by managed classes

L

Lev

Hi,

I have an unmanaged pointer to a class that I want to hold in a managed
class. I pass the pointer (from unmanaged code) in the constructor of the
managed class, and at that point it has value. It is stored in a member
variable in my managed class declared as MyObj __nogc* pMyObj. When I try to
retrieve it later, using a function declared as MyObj __nogc* GetObj, the
pointer has been corrupted and shows up as <undefined value> when inspecting
it from the debugger. How do I prevent that?

Thanks,

Lev
 
S

Steve McLellan

Does the object definitely still exist? If the object the pointer points to
has ceased to be, then the pointer you're looking at will be invalid.
Posting a bit more code might help - a cut-down test case or something. I
don't pass in unmanaged pointers to functions - wherever I've had to mix
managed/unmanaged, it's always been possible for the managed class to own
the unmanaged. Let me know if you figure out what the problem is.

Steve
 
L

Lev

Hi Steve,

Yes, I am positive that the object pointed to still exists. The object
pointed to is created even before the managed class is created and deleted
long after the managed class has ceased to exist. I am now trying further
investigations into the modification of the pointer... Any ideas are much
appreciated :)
 
S

Steve McLellan

Thought I'd check :)

When are you looking at it in the debugger? Are you sure the constructor's
been entered when you're viewing it? None of the stuff passed in to a
function is defined immediately when the function is entered; the first
debugger 'step' takes it into the function. If you try and use the pointer
inside the managed class, do you get an error?

Steve
 
L

Lev

Yes, I am quite confident that the constructor has been entered. I assign
the pointer to a member variable as the last step in the constructor.
However, when I tried to execute a method on the pointer (it is an interface
pointer), I got a System.OutOfMemoryException... That leaves me at a loss...
 
L

Lev

Ok, did some further investigations. If I call a method on the passed
interface pointer from the constructor, all is well. This leads me to
believe that the pointer stored internally in the managed class gets
corrupted or gc'ed eventhough it is declared as __nogc...
 
S

Steve McLellan

Yes, although I'm sure it shouldn't. Try removing the __nogc bit, see if
that makes a difference, and try writing a managed wrapper for it - store it
in a simple managed class and store that in your existing managed class. It
really shouldn't be affected by the garbage collector. What's the
constructor argument? Is it exactly the same as the stored pointer def (ie
not a base class or anything)?

Steve
 
L

Lev

Putting the pointer in a wrapper does not seem to help at all. Still getting
the NullReferenceException due to the pointer going into the undefined
state...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top