繁体   English   中英

运行时检查失败#2-围绕变量''的堆栈已损坏

[英]Run-Time Check Failure #2 - Stack around the variable '' was corrupted

我已经开始尝试dll并遇到了这个问题。 我有2个解决方案(VS 2012)1.在生成dll的地方(包含:templatedll.h,templatedll.cpp,templatedllshort.h)2.在哪里进行测试(因此我使用templatedllshort.h)

这是我第一个(dll)解决方案的代码

templatedll.h

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

templatedllshort.h(这是一个短标题,它隐藏了我班级的所有私人部分)

class __declspec(dllimport) Echo
{
public:
    Echo();
    Echo(int output_);
    ~Echo();
    void echo_public();
};

我测试的第二个解决方案

#include "templatedllshort.h"

int main()
{
    Echo e(1);  
    e.echo_public();
    return 0;
}

一切都正确链接,并且两个解决方案都可以编译并运行。 返回0后将出现运行时检查失败; 声明。 这是预期的输出:

Echo(int)
this is output: 1
~Echo()

谁能看到问题所在? 谢谢

问题来自#include "templatedllshort.h" 您不能像这样“隐藏”私人信息。 您可以使用#include "templatedll.h"并检查您是否不再遇到此问题?

(这是一个简短的标题,隐藏了我班级的所有私人部分)

致命的 DLL的客户端代码会将错误的大小传递给分配器以创建对象。 并创建一个太小的对象。 在这种特殊情况下,它将不会为对象保留足够的堆栈空间。 DLL本身现在会写到未分配的内存。 / RTC警告旨在使您摆脱此类麻烦。

不要为类撒谎。

使用界面和工厂功能来隐藏实现细节。

我认为您需要为DLL和驱动程序应用程序使用相同的标头。 另外,我看不到您在驱动程序应用程序中将DLL导入到的位置。

在每个源文件中,类的定义必须相同,否则它是未定义的行为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM