Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
740 views
in Technique[技术] by (71.8m points)

winapi - Why does _get_heap_handle equal to GetProcessHeap?

According to this article, CRT uses separate heap (is it private heap?), but this little example shows that CRT heap and Default heap are the same:

HANDLE heaps[64];
DWORD heapCount = GetProcessHeaps(64, heaps);    
for (int i = 0; i<heapCount; i++)
    printf("heap %d : [0x%x]
", i, heaps[i]);
printf("crt heap[0x%x], default heap[0x%x]
", _get_heap_handle(), GetProcessHeap());

In what cases GetProcessHeap and _get_heap_handle return different handles?

// Compiled with VS2012 (Platform toolset v110)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is new for VS2012, the CRT now uses the default process heap to allocate from. Previous versions have always created their own heap.

A significant advantage of using the default heap is that interop with code in a DLL will be a lot easier, it can significantly reduce the trouble of having to use a DLL that has its own copy of the CRT linked-in. Assuming that copy is also 2012+ vintage of course.

A potential disadvantage is that it is more difficult to generate a meaningful diagnostic or cleanly shutdown when the process heap becomes corrupted, Windows also uses that heap. And memory corruption in your code can destabilize OS calls, the kind that doesn't involve kernel calls, anything is possible there. I can imagine a security risk as well, I assume that this choice was made once they felt comfortable with the secure CRT enhancements.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...