C++虚拟表错误?

发布于 2024-10-04 16:50:56 字数 2054 浏览 8 评论 0原文

我有以下结构:

//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;
}

我的问题是在我的非托管代码中的某个地方,我必须调用 Send 函数。它从托管代码 Send 调用该函数,但是 Send 函数从 Parser 类调用 Transmit 方法。问题是,当我调试时,pParser 实例是空的(即使我之前已经在构造函数中实例化了它)。

这是垃圾收集器问题还是虚拟表误导?我该如何修复它? 谢谢 !

更新: 经过进一步调试后,我意识到如果我包含 gcroot 的其他实例,例如:

gcrootpDomain;

然后,在代码中尝试运行:

pDomain = AppDomain::CurrentDomain;

调试器将显示与 pParser 相同的空值。我正在做的事情有什么问题吗?我应该以不同的方式实例化该类吗?

UPDATE2:

托管/非托管是这样的:

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)
}

非托管:(commLayer.h)

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

非托管:(IInterface.h)

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

问题是,当非托管代码调用 mInferface->Deliver(mMessage) ; mParent 没有实例。然后,在Wrapper中,mParent为空(value = null);就像它只能访问非托管 IInterface 中的方法,而不是包装器 CInterface 中的 Wrapper^ 一样。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随风而去 2024-10-11 16:50:56

我相信在附加到进程时您需要打开本机调试和托管调试。

您可以在“附加到进程”对话框中通过单击“附加到:”框旁边的“选择”按钮来执行此操作。

尽管通常将其设置为“自动”,但它应该确定进程是否正在使用 CLR 运行,并为此对话框选择正确的条目。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文