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
I'm late to the party but here I go.
I received this error code from my own program, which brought me to this post. I was setting an array position out of range, causing the next allocation to crash the program, in Windows 7. I found the error by compiling with the -g flag with gcc from MinGW and then running the program with gdb. Somewhere here you read or write an invalid location and the next allocation picks up on the heap corruption. I solved my problem by bounds checking my iterators, but that doesn't seem to be the problem here.
Main Problems with the C Program:
FILE_SIZE % 4 == 1
then there are 3 bytes which are not part of your allocated data, but are accessed when you look at the final element of the u32 array. Solutions:
That version of software may have been doing some extra work when casting so that it wrote to a location out of range, or C# made some extra allocation calls that would do the same (I am not familiar with how C# compiles and what instruction changes it may impose).
Some code for finding the next multiple of 4:
size_t diff, rfSize = fSize; /* size_t is preferable for array sizes and indexes,
* but matching to fSize's data type will work and
* ensures no truncation occurs. */
/* Only if this is not already a multiple of 4 */
if (diff = fSize % 4)
/* Mod gives the remainder by division by 4, which is also the difference between
* fSize and the next multiple of 4. */
rfSize= fSize + diff;
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.
You are allocating the output data 2 times.
Once in C# as new IntPtr
and then in C++ as GlobalAlloc
and then return the Pointer Returned by GlobalAlloc
. So the Pointer returned by new intPtr
has been lost.