Updating C library, getting heap corruption?

When you allocate a block, you allocate a little more space, store your header at the beginning of the block, then return a pointer to an offset within the block (not the start of the block). For example "return alloc + sizeof(MY_HEADER)".

Up vote 0 down vote favorite share g+ share fb share tw.

This is a cheap hack but I am trying to change the allocation method of a C library I am working on. For some reason it used GlobalLock, perhaps because it used to be multiple DLLs. I have changed it to alloc: HANDLE BmiDibAlloc(size_t uBytes) { HANDLE alloc = malloc(uBytes + sizeof (size_t)); if (alloc!

= NULL) { memcpy_s(alloc, sizeof (alloc), &uBytes, sizeof (size_t)); } return BmiDibAttach(alloc); //just tracks the number of memory allocs for logging } BOOL BmiDibFree(HANDLE hdib) { if (!hdib) { return TRUE; } free(hdib); // Forget this handle: return BmiDibDetach(hdib); } Since I cannot use GlobalSize anymore I tack on the size of the allocation on the first sizeof (size_t) bytes... When The bitmap writes fine after allocated with the first method - however, when I get to Free it throws a heap corruption. Granted it may be somewhere inbetween these calls, does anyone see something wrong with this with the information given? C memory memory-management malloc free link|improve this question asked Jan 20 at 22:58Tom Fobear8291314 99% accept rate.

2 Your code looks fine, probably the problem is somewhere in the rest of the code; the only mistake I see is that sizeof(alloc) is wrong, it should be sizeof(size_t) (although, I think that on every Windows platform sizeof(HANDLE)==sizeof(void *)==sizeof(size_t)). – Matteo Italia Jan 20 at 23:04 @MatteoItalia you see why I need to do this though, right? Certain parts of the library would formerly use GlobalSize() on the allocation.

Perhaps I could have replaced that with sizeof (*alloc) had it not been a void *. – Tom Fobear Jan 20 at 23:23 if you prepend the size, do you make sure everything that uses this memory does not mess with it? And account for the memory block actually being uBytes + sizeof (size_t) if you e.g. Resize it?

– nos Jan 21 at 3:15 @Matteo: Actually on Win64 void * is 64-bit but size_t is 32-bit... – R.. Jan 21 at 6:06 @R. : well, that's interesting, it means that even on 64 bit platforms an object (thus even a dynamically allocated array) cannot be larger than 4 GB... sounds like a compatibility tradeoff. – Matteo Italia Jan 21 at 12:19.

When you allocate a block, you allocate a little more space, store your header at the beginning of the block, then return a pointer to an offset within the block (not the start of the block). For example "return alloc + sizeof(MY_HEADER)". When you free a block you have to do the reverse.

For example: BOOL BmiDibFree(HANDLE callerPointer) { actualPointer = callerPointer - sizeof(MY_HEADER); free(actualPointer); Note 1: For performance, you should make sure that "sizeof(MY_HEADER)" is a multiple of the minimum alignment provided by "malloc()"; so you don't cause mis-alignment problems for the caller. Note 2: You can add "canaries" (magic numbers) at the real start of the block and at the real end of the block, and check these (during free and realloc) to increase the chance of detecting heap corruption. I do this and set a "heap was corrupted" flag, and test this flag before any malloc/free/realloc (if the heap was corrupted, all subsequent operations fail immediately to avoid making the mess larger).

Note 3: You can use conditional compiling (e.g. "#ifdef DEBUGGING") to enable/disable various features of your wrapper. I do this too - one to enable extra checking (the canaries) and one to enable the gathering/reporting of statistics (total number of blocks allocated, max. Number of blocks allocated at any time, total number of bytes allocated, max. Number of bytes allocated at any time).

This is typically just 4 bytes. The same with "sizeof(alloc)" - it's probably just "4".

The sizeof (size_t) is intended, I want to allocate an extra 4 bytes to store the size of the allocation. The sizeof (alloc) I didn't even think about, should be the number of (elements? ) in alloc... but seeing as how alloc is a void * (HANDLE) that would be the size of a pointer?

(4 bytes as well on my 32 bit os, not breaking but also technically not correct) – Tom Fobear Jan 20 at 23:06.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions