简体   繁体   English

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

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

I have started to experiment with dlls and came across this problem. 我已经开始尝试dll并遇到了这个问题。 I have 2 solutions (VS 2012) 1. Where I generate the dll (contains: templatedll.h, templatedll.cpp, templatedllshort.h) 2. Where I test it (I use therefore templatedllshort.h) 我有2个解决方案(VS 2012)1.在生成dll的地方(包含:templatedll.h,templatedll.cpp,templatedllshort.h)2.在哪里进行测试(因此我使用templatedllshort.h)

So this is the code of my first (dll) solution 这是我第一个(dll)解决方案的代码

templatedll.h templatedll.h

class __declspec(dllexport) Echo
{
private:
    int output;
    void echo_private();

public:
    Echo();
    Echo(int output_);
    ~Echo();
    void echo_public();
};

templatedll.cpp 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 (this is a short header that hides all the private parts of my class) templatedllshort.h(这是一个短标题,它隐藏了我班级的所有私人部分)

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

The second solution where I test it 我测试的第二个解决方案

#include "templatedllshort.h"

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

Everything is properly linked and both solutions compile and run. 一切都正确链接,并且两个解决方案都可以编译并运行。 The Run-Time Check Failure comes after return 0; 返回0后将出现运行时检查失败; statement. 声明。 This is the expected output: 这是预期的输出:

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

Can any one see where the problem is? 谁能看到问题所在? Thanks 谢谢

The problem comes from #include "templatedllshort.h" . 问题来自#include "templatedllshort.h" You can not "hide" private information like this. 您不能像这样“隐藏”私人信息。 Can you use #include "templatedll.h" and check that you don t face anymore this problem? 您可以使用#include "templatedll.h"并检查您是否不再遇到此问题?

(this is a short header that hides all the private parts of my class) (这是一个简短的标题,隐藏了我班级的所有私人部分)

That's fatal. 致命的 The client code of your DLL will pass the wrong size to the allocator to create the object. DLL的客户端代码会将错误的大小传递给分配器以创建对象。 And create an object that's too small. 并创建一个太小的对象。 In this particular case, it will not reserve enough stack space for the object. 在这种特殊情况下,它将不会为对象保留足够的堆栈空间。 The DLL itself will now scribble to memory that wasn't allocated. DLL本身现在会写到未分配的内存。 The /RTC warning was designed to keep you out of this kind of trouble. / RTC警告旨在使您摆脱此类麻烦。

Don't lie about classes. 不要为类撒谎。

Use an interface and a factory function to hide the implementation details. 使用界面和工厂功能来隐藏实现细节。

I think you need to use the same header for both the DLL and the driver application. 我认为您需要为DLL和驱动程序应用程序使用相同的标头。 Also, I don't see where you're importing the DLL in the driver app. 另外,我看不到您在驱动程序应用程序中将DLL导入到的位置。

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

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

相关问题 运行时检查失败 #2 - 变量“IDNumber”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'IDNumber' was corrupted 运行时检查失败#2-变量&#39;indices&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'indices' was corrupted 运行时检查失败#2-变量&#39;result&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'result' was corrupted 运行时检查失败#2-变量&#39;numberchoices&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'numberchoices' was corrupted 运行时检查失败#2-变量&#39;ex&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'ex' was corrupted 运行时检查失败#2-变量&#39;NearID&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'NearID' was corrupted 运行时检查失败-变量周围的堆栈已损坏 - Run-Time Check Failure - Stack around variable was corrupted 运行时检查失败#2-变量&#39;s&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 's' was corrupted 运行时检查失败#2 - 变量&#39;B&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'B' was corrupted 运行时检查失败#2-变量周围的堆栈-已损坏 - Run-Time Check Failure #2 - Stack around the variable — was corrupted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM