C++ 运行时检查失败#2-围绕变量'';腐败
我开始尝试DLL,遇到了这个问题。我有两个解决方案(VS 2012) 1.我在其中生成dll(包含:templateddll.h、templateddll.cpp、templateddllshort.h) 2.我测试它的地方(因此我使用templatedllshort.h) 这是我的第一个(dll)解决方案的代码 templatedll.hC++ 运行时检查失败#2-围绕变量'';腐败,c++,visual-studio-2012,dll,C++,Visual Studio 2012,Dll,我开始尝试DLL,遇到了这个问题。我有两个解决方案(VS 2012) 1.我在其中生成dll(包含:templateddll.h、templateddll.cpp、templateddllshort.h) 2.我测试它的地方(因此我使用templatedllshort.h) 这是我的第一个(dll)解决方案的代码 templatedll.h class __declspec(dllexport) Echo { private: int output; void echo_priv
class __declspec(dllexport) Echo
{
private:
int output;
void echo_private();
public:
Echo();
Echo(int output_);
~Echo();
void echo_public();
};
templatedll.cpp
#include "templatedll.h"
#include <iostream>
Echo::Echo()
{
output = 0;
std::cout << "Echo()\n";
}
Echo::Echo(int output_)
{
this->output = output_;
std::cout << "Echo(int)\n";
}
Echo::~Echo()
{
std::cout << "~Echo()\n";
}
void Echo::echo_private()
{
std::cout << "this is output: " << this->output << std::endl;
}
void Echo::echo_public()
{
echo_private();
}
第二个解决方案是我测试它
#include "templatedllshort.h"
int main()
{
Echo e(1);
e.echo_public();
return 0;
}
所有内容都已正确链接,两个解决方案都已编译并运行。运行时检查失败发生在返回0之后;陈述
这是预期输出:
Echo(int)
this is output: 1
~Echo()
有人能看出问题出在哪里吗?
谢谢我认为DLL和驱动程序应用程序都需要使用相同的头。此外,我看不到您在驱动程序应用程序中导入DLL的位置。问题来自于
#包括“templatedllshort.h”
。你不能像这样“隐藏”私人信息。您能否使用#include“templateddll.h”
并检查您是否不再面临此问题
(这是一个隐藏类的所有私有部分的短标题)
那是致命的。DLL的客户端代码将向分配器传递错误的大小以创建对象。然后创建一个太小的对象。在这种特殊情况下,它不会为对象保留足够的堆栈空间。DLL本身现在将涂鸦到未分配的内存中。/RTC警告旨在避免您遇到此类问题
不要在课堂上撒谎
使用接口和工厂函数隐藏实现细节。在每个源文件中,类的定义必须相同,否则它是未定义的行为。我认为您不能这样重新定义类并使其工作。我的意思是,你需要在公共和私人标题中的类大小相同。谢谢,我没有考虑到这一点。这就解释了!我在这里发帖之前就试过了,我想知道为什么它有效,而另一种方法无效。现在我知道了
Echo(int)
this is output: 1
~Echo()