3

I have the following structure:

//Unmanaged(.h)
class myInterface
{
public:
   virtual bool Send(char* myChar);
}

//Managed (.h)
class myClass;

public ref class Parser
{
   bool Transmit(String^ mString);
}

class myClass : public myInterface
{
public:
   virtual bool Send(char* myChar);
private:
   gcroot<Parser^> pParser;
}

My problem is that somewhere in my unmanaged code, i must call the Send function. It calls the function from the Managed code Send, but, the Send function calls the Transmit Method from the Parser class. The problem is that when I Debug, the pParser instance is empty (even if i already had instantiated it before in the Constructor).

Is this a Garbage Collector Issue or a Virtual Table mislead ? How can i Fix it ? Thanks !

UPDATE: After some further Debugging, i've realized that if i included other instances of gcroot for example:

gcroot<AppDomaion^> pDomain;

and then, in the code, tried to run:

pDomain = AppDomain::CurrentDomain;

The Debugger would show the same empty value as for the pParser. Is there something wrong with what i'm doing ? should I instantiate the class in a different way ?

UPDATE2:

The Managed/Unmanaged goes something like this:

Wrapper:(wrapper.h)

public ref class Wrapper
{
public:
   Send(String^ mSendMessage);
   Parse(String^ mMessageString);
...
private:
   ComLayer* mComm;
   CInferface mInterface;
};

private class CInterface : public IIterface
{
public:
   virtual bool Deliver(CMessage mMessage);
...
private:
   gcroot<Wrapper^> mParent;
};

Wrapper(wrapper.cpp)

Wrapper::Send(String^ mSendMessage)
{
...
mComm->Send(mMessage);
}
Wrapper::Parse(String^ mMessageString)
{
...
}

CInterface::Deliver(CMessage* mMessage)
{
...
//Here, mParent value is empty under Labview, not while Debug/VS/WindowsForm
mParent->Parse(mMessageString)
}

Unmanaged:(commLayer.h)

class CommLayer
{
public:
//Send:
   bool Send(CMessage* mMessage);
...
private:
//instead of CInterface, IInterface.
   IInterface mInterface;
};

Unmanaged:(IInterface.h)

class IInterface
{
public:
//Response:
   virtual bool Deliver(CMessage mMessage);
};

The problem is that when the unmanaged code calls the mInferface->Deliver(mMessage) ; There is no instance for mParent. Then, in the Wrapper, mParent is empty (value = null); Is like it would only access the methods from the Unmanaged IInterface and not the Wrapper^ from the wrapper CInterface.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Jazz.
  • 458
  • 4
  • 12
  • This is not standard C++; please could you tag it with something more specific? – Oliver Charlesworth Nov 26 '10 at 09:19
  • Do you have unmanaged debugging turned on in Visual Studio? In the debug options for the executable project there is a setting for "Enable unmanaged code debugging." – Nick Nov 26 '10 at 09:27
  • @Oli Charlesworth: Thanks icecrime. @Nick. I´m calling the Managed Library from Labview and after 'Attaching the Process' to it, i'm debugging the code. Should i still enable the unmanaged code debugging ? What should i look for ? – Jazz. Nov 26 '10 at 09:39
  • Hmmm, I'm not familiar with LabView, however if you're attaching to a process using VisualStudio you should still be able to enable unmanaged debugging in the Attach Dialog. There is a selection box called "Attach to", ensure that both Native and Managed are selected before you attach. – Nick Nov 26 '10 at 10:59

1 Answers1

0

I believe that you need to turn on both Native and Managed debugging when attaching to the process.

You can do this in the Attach to Process dialog box by clicking the "Select" button next to the "Attach to:" box.

Although generally this is set to "Automatic" which should determine whether a process is running with the CLR and select the correct entries for this dialog.

Nick
  • 25,026
  • 7
  • 51
  • 83
  • I still get an empty instance. I think my problem is more related to AppDomains than with Virtual Tables or Garbage Collector. I'm still trying to figure out how to call everything in one single AppDomain or at least allow the different AppDomains to share a Class. – Jazz. Nov 26 '10 at 11:19
  • While debugging on VisualStudio, the ManagedCode & UnmanagedCode run in a ConsoleTest.vshost.exe AppDomain, while in Labview, the ManagedCode run in a "Labview Domain for Run" while the UnmanagedCode run in a "Default Domain" – Jazz. Nov 26 '10 at 11:21
  • So, in your ConsoleTest application you're not having any problems but then when you run it in LabView you're seeing the empty values? Any chance you could attach more code? – Nick Nov 26 '10 at 11:23
  • Exactly. I copied some of the code which i already had posted in this post: http://stackoverflow.com/questions/4266427/passing-handle-to-a-console-app-or-managed-unmanaged-help Tried doing it with Serialization, couldn't make a Constructor Method that have all the values. Am i missing something really important here ? I'm not a programmer, but a circuit designer with extra tasks ! Thanks for your help. – Jazz. Nov 26 '10 at 11:47