检测到严重错误c0000374-C ++ dll将指针从分配的内存返回到C# [英] Critical error detected c0000374 - C++ dll returns pointer off allocated memory to C#

查看:697
本文介绍了检测到严重错误c0000374-C ++ dll将指针从分配的内存返回到C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个c ++ dll,可为我的主要c#应用程序提供某些功能。
在这里,我尝试读取文件,将其加载到内存中,然后将一些信息(例如,指向已加载数据的指针和返回给c#的内存块数)返回。 Dll成功将文件读取到内存,但是在返回主应用程序时,由于堆损坏而导致程序崩溃(检测到严重错误c0000374)。

I have a c++ dll which serving some functionality to my main c# application. Here i try to read a file, load it to memory and then return some information such as the Pointer to loaded data and count of memory blocks to c#. The Dll reads file to memory successfully but on the return to the main application, program crashes due to Heap Corruption(Critical error detected c0000374).

代码非常简单,简单明了,我之前也做过类似的事情,没有问题,但是我无法弄清楚是什么原因造成的,我尝试使用 new,malloc和GlobalAlloc分配内存,但都没有帮助。代码如下:

The code is quite simple and straightforward and I have done some similar things before with no problem, However i could not figure out what makes the problem here, I tried to allocate memory using "new, malloc and GlobalAlloc" but neither did help. Codes are as follow:

C ++ MyDll:

C++ MyDll:

typedef unsigned long         U32;

extern "C" __declspec(dllexport) int ReadFile(LPSTR Path, U32** DataPtr, U32* Count)
{
   FILE *fp;
   U32 *Data;
   CString tempStr(Path);
   long fSize;

   if(!(fp = fopen(tempStr, "rb"))) {
    return 0;
   }

   // Obtain File Size;
   fseek(fp, 0, SEEK_END);
   fSize =  ftell(fp);
   rewind(fp);

   Data = (U32 *)GlobalAlloc(0, fSize);
   if(Data == NULL) {
            fclose(fp);
            return -1;
    }

    // Copy file into the buffer.
        if(!(*Count = fread(Data, sizeof(U32), fSize / sizeof(U32), fp))) {
           fclose(fp);
           free(Data);
           return -2;
        }

   *DataPtr = (U32 *)Data;
       return 1;
}

C#应用程序:

        [DllImport(@"MyDll.dll", CallingConvention= CallingConvention.Cdecl)]
    private static extern int ReadFile([MarshalAs(UnmanagedType.LPStr)]string Path, out IntPtr dataPtr, out uint Count);

private void readDump(string Path)
{
    uint count = 0;
    IntPtr Data = new IntPtr();

   try{
       if(ReadFile(Path, out Data, out count) == 1) //The Program crashes just right after this statement
       {
           //Do Something ...
       }
    }
    catch() {}

}

程序在调试和释放模式下均崩溃。除非我在加载文件后在调试模式下暂停程序,然后在 Visual Studio的即时窗口中调用某些内存块。
要加载的文件大小约为64MB,并且PC上有超过2GB的未使用内存。

The program crashes on both debug and release mode. Unless I pause the program in debug mode after loading the file and call some blocks of memory in the "Visual Studio's Immediate window". The size of files to be loaded are around 64MB and we have more than 2GB unused ram on the PC.

更新:我注意到,他们之前使用的某些第三方程序崩溃,并显示异常代码:c0000005,而Windows 7(主机)中发生了其他一些奇怪的事情。所以我在另一个Windows安装中测试了代码,一切似乎都能正常工作。大概和Windows 7有关。现在我该如何解决该问题? sfc / scannow找不到任何问题。

UPDATE: I noticed that, some third party programs which they working before, crash with "Exception Code: c0000005", and some other weird things happens in Windows 7 (the Host). so I tested the code in another installation of windows and everything seems to work as they should. So probably it's related be the Windows 7. Now how could I fix the problem? "sfc /scannow" failed to find any issue.

推荐答案

如果您的所有代码确实如上所示,那么我不会没有看到问题。但是,当我遇到此问题时,有时是因为malloc / new /无论检测到堆损坏是什么,通常在程序中以前已经发生了这种损坏,但是崩溃已被延迟到下一次调用new / malloc为止。

If all your code is indeed what is shown above, then I don't see the problem. However, when I get this issue, sometimes its because malloc/new/whatever detects heap corruption, often this corruption has already occurred previously in the program, but the crash has been delayed until the next call to new/malloc.

如果在执行上述操作并崩溃之前读取其他文件,或分配或释放其他缓冲区,我会在那儿查找问题。也许在您写到缓冲区的任何地方都抛出一堆断言,并检查界限以及为溢出而写的内容。
抱歉,这不是一个具体答案,我没有足够的代表将此建议作为评论。

If you read other files, or allocate or free other buffers before the above is executed and crashes, I would look there for problems. Perhaps throw a bunch of asserts anywhere you write to buffers and check the bounds and what you are writing for overruns. Sorry this isn't a concrete answer, I do not have enough rep to leave this advice as a comment.

这篇关于检测到严重错误c0000374-C ++ dll将指针从分配的内存返回到C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆