Why malloc always returns NULL

My dev is VS2008, DX9, Windows XP. I am trying to add protection processing from memory. When malloc returns NULL, I would list the resource to disk and free the resources in memory.

But sometimes malloc always returns NULL, even if I free up most of the resources and use the memory to use, and the size of the VM is only 800 MB in the task manager.

I think that using malloc to allocate 88 bytes should be good when the process memory is only 800 MB. But malloc always returns NULL.

Could this be memory fragmentation? This is not like using process memory is not too much.

alt text http://i.imagehost.org/0267/Snap2.jpg

+6
c ++ c memory-management windows directx
source share
3 answers

You mentioned memory fragmentation, and this will certainly be my first guess. Try downloading this app. It is called the Address Spatial Monitor and should be able to show you if there is a fragmentation problem.

+2
source share

This may be a fragmentation of the virtual address space. One way to check is to call HeapCompact(GetProcessHeap(), 0) . If this frees up enough memory, then this is the likely cause.

Another similar reason will be launched from the debugger; which leads to the fact that Windows uses a bunch of debugging, which in fact really does not work well with memory for a long period of time. To disable this behavior, set _NO_DEBUG_HEAP=1 to the environment and run.

+2
source share

Another possibility is that there may be an error in your program. You think you are asking for 88 bytes, but perhaps you are passing an uninitialized variable and requesting hundreds of megabytes. Or perhaps something you did before triggered a buffer and distorted the heap, calling malloc () forever after that.

0
source share

All Articles