Critical error detected c0000374 - C++ dll returns pointer off allocated memory to C#

2i3r

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).

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:

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# Application:

        [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() {}

}

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.

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.

Freya301

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Critical error -> c0000374

From Dev

How to free allocated memory in C++ DLL

From Dev

C "pointer being freed was not allocated" error

From Dev

C "pointer being freed was not allocated" error

From Dev

c++ pointer being freed was not allocated error

From Dev

C++: delete[] error, pointer not allocated

From Dev

Free allocated memory in C

From Dev

Trie in C: Pointer detected as null. Causes Memory leak

From Dev

FILE pointer in C, dynamically allocated?

From Dev

pointer being freed was not allocated in C

From Dev

(C++ Error: pointer being freed was not allocated) for linked lists

From Dev

C: malloc error-pointer being freed was not allocated

From Dev

error: Pointer being freed was not allocated in vector template class c++

From Dev

How memory is allocated to macros in c?

From Dev

C++ Local object goes out of scope returning a pointer (memory allocated using new). Memory leak because of this

From Dev

C pointer and memory leak

From Dev

Pointer layout in memory in C

From Dev

How to properly use memory dynamically allocated in C++ dll within Matlab

From Dev

How to clear memory allocated for an array that's pointed to by a pointer correctly in C++?

From Dev

How to clear memory allocated for an array that's pointed to by a pointer correctly in C++?

From Dev

Calling function in C dll from C# which returns somekind of pointer to function pointer

From Dev

C++: pointer being freed was not allocated

From Dev

C dynamic allocated pointer to main function

From Dev

C Program Issue "pointer being freed was not allocated"

From Dev

C free(): invalid pointer allocated in other function

From Dev

C++ return pointer to dynamically allocated array

From Dev

C++ delete pointer to dynamic allocated object

From Dev

When does memory gets allocated for a variable in c?

From Java

Does c keep track of allocated heap memory?

Related Related

  1. 1

    Critical error -> c0000374

  2. 2

    How to free allocated memory in C++ DLL

  3. 3

    C "pointer being freed was not allocated" error

  4. 4

    C "pointer being freed was not allocated" error

  5. 5

    c++ pointer being freed was not allocated error

  6. 6

    C++: delete[] error, pointer not allocated

  7. 7

    Free allocated memory in C

  8. 8

    Trie in C: Pointer detected as null. Causes Memory leak

  9. 9

    FILE pointer in C, dynamically allocated?

  10. 10

    pointer being freed was not allocated in C

  11. 11

    (C++ Error: pointer being freed was not allocated) for linked lists

  12. 12

    C: malloc error-pointer being freed was not allocated

  13. 13

    error: Pointer being freed was not allocated in vector template class c++

  14. 14

    How memory is allocated to macros in c?

  15. 15

    C++ Local object goes out of scope returning a pointer (memory allocated using new). Memory leak because of this

  16. 16

    C pointer and memory leak

  17. 17

    Pointer layout in memory in C

  18. 18

    How to properly use memory dynamically allocated in C++ dll within Matlab

  19. 19

    How to clear memory allocated for an array that's pointed to by a pointer correctly in C++?

  20. 20

    How to clear memory allocated for an array that's pointed to by a pointer correctly in C++?

  21. 21

    Calling function in C dll from C# which returns somekind of pointer to function pointer

  22. 22

    C++: pointer being freed was not allocated

  23. 23

    C dynamic allocated pointer to main function

  24. 24

    C Program Issue "pointer being freed was not allocated"

  25. 25

    C free(): invalid pointer allocated in other function

  26. 26

    C++ return pointer to dynamically allocated array

  27. 27

    C++ delete pointer to dynamic allocated object

  28. 28

    When does memory gets allocated for a variable in c?

  29. 29

    Does c keep track of allocated heap memory?

HotTag

Archive