Mem I assume is a handle in a CreateHeap call, we need to see that and the ulsize and ulsize1 values. Break down your code into a simple console application and you will most likely find very quick your error. There is no problem with calling HeapCreate(...) and HeapAlloc(...)/HeapFree(...) etc, unless you set dwMaximumSize to be less than you are allocating, but that makes no sense in the context your describing, that one succedes when you don't Free....which brings me to memory corruption, but I can assume again your running under a debug build and if you have corruptied the heap it should raise a breakpoint...