簡體   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