在 Visual Studio 2010 中从 dll 崩溃中释放主程序内存

发布于 2024-11-08 15:47:44 字数 1415 浏览 0 评论 0原文

我在使用 gcc 编译器用 C 语言编写 .dll 文件时遇到了一个奇怪的问题,然后尝试通过 MSVC 和 Visual Studio 使用它。 .dll 可以正确编译,并且可以被使用 gcc 编译和链接的测试程序正确使用。 但是当我尝试在 Visual Studio 中使用 .dll 时,我遇到了奇怪的运行时崩溃。我已经发现了这个问题,希望您能提供有关为什么会发生这种情况的意见。

在测试程序中,我们有一个函数分配缓冲区,以某种方式填充它(取决于用户),然后返回缓冲区。用户必须自己实现。如下所示:

float* decode(void* data, int dataSize, int par1, int par2,int par3)
{ 

    //allocate the memory for the data
    float* d =  (float*)malloc(sizeof(float)*dataSize);

    //do something to populate the data here

    //return data
     return d;
}

该 dll 有两个函数,它们实际上获取指向该 use-rmade 函数的函数指针,并使用它来获取该 float* 缓冲区。然后 .dll 使用此函数创建和使用数据,并在使用后释放它们。

如下所示:

void DLL_Func(void* data,unsigned int inputPN,float* (*decode)(void*,int,int,int,int))
{

    //use the user-made decode function
    float* nData = (*decode)(data,inputPN,par1,par2,par3);

    //do stuff here

    //before returning free the data buffer since it is no longer needed
    free(nData) //<--- Crushes the program only in Visual Studio
 }

嗯,我在 free() 中隔离了问题。如果我使用 gcc 编译程序中的 dll,则工作正常,但 MSVC 编译程序此时会崩溃并给出以下内容:

 Unhandled exception at 0x7714e1fe in vstudiotest.exe: 0xC0000005: Access violation reading location 0x1e6dca5e.

我猜这意味着对于 MSVC 编译器来说,我的 dll 所做的事情是被禁止的。这只是 MSVC 的一些怪癖还是 GCC 也应该粉碎但不会并且将来会回来并用这个问题折磨我?

当然,解决方案是以某种方式更改它,以便用户也释放缓冲区而不是 .dll,但我想首先了解发生这种情况的原因。

I encountered a strange issue while coding a .dll file in C with gcc compiler and then tried to use it through MSVC and visual studio. The .dll compiles correctly and is correctly used by test programs compiled and linked with gcc.
But when I try to do use the .dll in Visual studio I get a strange runtime crush. I have identified the issue and would appreciate your input as to why is this happening.

In the test program we have a function which allocates a buffer, populates it in some way (depending on the user) and then returns the buffer. The user has to implement it on his own. Something like below:

float* decode(void* data, int dataSize, int par1, int par2,int par3)
{ 

    //allocate the memory for the data
    float* d =  (float*)malloc(sizeof(float)*dataSize);

    //do something to populate the data here

    //return data
     return d;
}

The dll has two functions which actually take a function pointer to that use-rmade function and use it to get that float* buffer. Then the .dll uses this function to create and use the data and after the usage it frees them.

Something like below:

void DLL_Func(void* data,unsigned int inputPN,float* (*decode)(void*,int,int,int,int))
{

    //use the user-made decode function
    float* nData = (*decode)(data,inputPN,par1,par2,par3);

    //do stuff here

    //before returning free the data buffer since it is no longer needed
    free(nData) //<--- Crushes the program only in Visual Studio
 }

Well I isolated the problem in that free(). Works fine if I use my dll from a gcc compiled program but MSVC compiled program crushes at that point and gives this:

 Unhandled exception at 0x7714e1fe in vstudiotest.exe: 0xC0000005: Access violation reading location 0x1e6dca5e.

Which I guess means that for the MSVC compiler what my dll does is forbidden. Is this just some MSVC quirk or should GCC also crush but does not and will come back in the future and torture me with this problem?

Of course the solution is to change it somehow so that the buffer is also freed by the user and not the .dll but I would like to understand the reason this is happening first.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

未央 2024-11-15 15:47:44

必须使用相同的 CRT 分配和释放内存。这意味着通常您应该尝试释放分配内存的同一模块中的内存,以避免出现此类问题。如果这不可能,您必须链接到相同版本的 CRT。

如果您尝试链接到不同版本的 MS CRT,MSVC 会抱怨,但我猜测您正在链接到 MSVC 不知道的其他版本。

memory must be allocated and freed with the same CRT. This means in general that you should try to free memory in the same module it was allocated in to avoid these types of issues. If this isn't possible you must link to the same version of the CRT.

MSVC will complain if you try to link to different versions of the MS CRT, but I'm guessing that you are linking to some other version that MSVC doesn't know about.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文