首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未使用指针或数组时C++损坏了堆

未使用指针或数组时C++损坏了堆
EN

Stack Overflow用户
提问于 2014-10-15 18:12:42
回答 1查看 207关注 0票数 1

我正在开发一个动态加载动态链接库的C++控制台应用程序。

应用程序可以成功地调用DLL的一个函数。但是,在执行结束时,会抛出损坏的堆异常。具体值为:

代码语言:javascript
复制
Stack cookie instrumentation code detected a stack-based buffer overrun.

我想知道为什么..。下面是代码。

代码语言:javascript
复制
#include <iostream>
#include <string>
#include "windows.h"

using namespace std;

typedef DOUBLE(CALLBACK* DllFunc)(DOUBLE, DOUBLE);

int _tmain(int argc, _TCHAR* argv[])
{    
    HINSTANCE hDLL;               // Handle to DLL
    DllFunc dllFunc1;
    DOUBLE p1 = 2.0, p2 = 4.0, r;
    wstring dllName;
    string functionName;

    cout << "Insert the dll name: " << endl;
    getline(wcin, dllName);
    cout << "Insert the function name:" << endl;
    getline(cin, functionName);

    cout << "Insert the first value: " << endl;
    cin >> p1;
    cout << "Insert the second value" << endl;
    cin >> p2;

    hDLL = LoadLibrary(dllName.c_str());
    if (hDLL != NULL)
    {
        cout <<  "DLL loaded: " << hDLL << endl;
        functionName = "?" + functionName + "@MyMathFuncs@MathFuncs@@SANNN@Z";
        dllFunc1 = (DllFunc)GetProcAddress(hDLL, functionName.c_str());
        if (!dllFunc1)
        {
            // handle the error
            FreeLibrary(hDLL);
            cout << "Function not found!" << endl;
        }
        else
        {
            // call the function
            r = dllFunc1(p1, p2);
            cout << "The result is: " << r << endl;
            FreeLibrary(hDLL);
        }               
    }
    else {
        cout << "Dll not found" << endl;
    }
    cout << "Press any key to exit." << endl;
    int i;
    cin >> i;
    return 0;
}

我不知道问题出在哪里:如果库被正确加载,我就释放它;没有指针,也没有使用任何缓冲区……

有什么想法吗?只有当执行到达最后一个关闭的大括号时,才会发生异常...

EN

回答 1

Stack Overflow用户

发布于 2014-10-15 20:24:54

这看起来更像是堆栈问题(可能会影响错误报告)。您的调用约定正确吗?当装饰似乎指示__cdecl时,回调可能是__stdcall

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26379942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档