I knew I had a small memory leak in my software, so I downloaded
memwatch (http://www.linkdata.se/sourcecode.html). Running the
sample (test.c) in debug mode, a request to malloc for 250 bytes in a
specific context only returns 224 bytes (it may be because of the cast
to a structure type, and the compiler rounding the size). code is
as follows (very chopped):
struct mwData_ {
mwData* prev; /* previous allocation in chain */
mwData* next; /* next allocation in chain */
const char* file; /* file name where allocated */
long count; /* action count */
long check; /* integrity check value */
size_t size; /* size of allocation */
int line; /* line number where allocated */
unsigned flag; /* flag word */
};
mwMalloc(size) {
mwOverflowZoneSize = 4;
mwDataSize = sizeof(mwData_); //32 bytes
needed = mwDataSize + mwOverflowZoneSize*2 + size; //requested size + 40
mw = (mwData*) malloc( needed );
ptr = ((char*)mw) + mwDataSize;
ptr += mwOverflowZoneSize;
memset( ptr, MW_VAL_NEW, size );
calling mwMalloc(210), needed=250, will error out at the memset:
FATAL RUN-TIME ERROR: "memwatch.c", line 935, col 13,
thread id 0x00000950: Array argument too small (188
bytes). Argument must contain at least 210 bytes (210 elements)
for some reason, compiling in debug mode, only 224 bytes total are
being allocated, whereas in release mode, it will allocate the full 250
bytes that are requested. Is this a bug in the compiler? I
have a workaround for this problem, so it works in debug mode, but
seems silly that malloc doesn't return what you ask it for.
Workaround:
replace
mw = (mwData*) malloc( needed );
with
ptr = (char*)malloc( needed );
mw = (mwData*)ptr;
thanks for the help,
~Chip