2

I am using WinDbg to see number of heaps in the process by using, dt _PEB @$peb. I get following info,

+0x088 NumberOfHeaps    : 1

Now according to Advanced Windows Debugging book,

Most applications implicitly use components that create their own heaps. A great example is the C runtime, which creates its own heap during initialization.

I add breakpoint at main, still I can see there is only one heap in process.

Secondly, I ran following code, still number of heap is 1.

BYTE* pAlloc1 = NULL;
BYTE* pAlloc2 = NULL;
HANDLE hProcessHeap = GetProcessHeap();
pAlloc1 = (BYTE*)HeapAlloc(hProcessHeap, 0, 16);
pAlloc2 = (BYTE*)HeapAlloc(hProcessHeap, 0, 1500);

Why I am not getting number of heap incremented in process?

Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137
  • 2
    You've made two allocations from the heap, but you haven't created a new heap. I know little of Windows, but presumably you'll only get a second heap if something calls [`HeapCreate`](http://msdn.microsoft.com/en-gb/library/windows/desktop/aa366599(v=vs.85).aspx). – Mike Seymour Sep 01 '14 at 13:33
  • 2
    a guess: with `HeapAlloc` you are allocating memory from process heap, but with `HeapCreate` you create a new private heap. – Nazar554 Sep 01 '14 at 13:33
  • Pure logic dictates that the C runtime has to create the heap (way) before you reach `main`. – Karoly Horvath Sep 01 '14 at 13:43
  • 1
    "A great example"? Why can't they say "A good example"? Or am I just getting old? – TonyK Sep 01 '14 at 13:54
  • @KarolyHorvath Sure. Then count must be at least 2. – Pranit Kothari Sep 01 '14 at 14:19
  • @KarolyHorvath: That would be faulty logic. `HeapCreate` is used to create a second heap; the C runtime doesn't need to create the first heap as that's OS-provided. – MSalters Sep 01 '14 at 16:48
  • @MSalters: it's not faulty. You said yourself, it doesn't do it. But if it would... – Karoly Horvath Sep 01 '14 at 18:29

1 Answers1

2

When a process starts, it has a single heap, the default process heap. That explains the count of 1. You can create new heaps by calling HeapCreate. Should you do so you will see the heap count increase.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490