Search

HeapValidate does not work by kp30

Closed
as By Design Help for as By Design

1
0
Sign in
to vote
Type: Bug
ID: 109434
Opened: 8/21/2005 3:46:39 PM
Access Restriction: Public
0
Workaround(s)
0
User(s) can reproduce this bug
HeapValidate should return "true" if the heap reference is valid,
according to the documentation.

This is a serious problem because when I port my working app to
Windows, "delete" operations are unexpectedly crashing and I can't
debug the heap problems.
Details (expand)
Product Language
English
Version
Visual Studio 2005 Beta 1
Category
Visual C++ Development
Operating System
Windows XP Professional
Operating System Language
English
Steps to Reproduce
compile this program and run it in Windows XP pro:

#include <windows.h>
#include <assert.h>

int main(int argc, char* argv[])
{

int* out = new int[2048];
bool res = HeapValidate(GetProcessHeap(), HEAP_NO_SERIALIZE, out);
assert(res);
return 0;
}
Actual Results
assertion failed
Expected Results
no assertion failure
File Attachments
0 attachments
Sign in to post a comment.
Posted by Microsoft on 8/22/2005 at 11:42 AM
Your example does not indicate a bug. The CRT does not use the default process heap, so your HeapValidate call should return false. The following example illustrates that:

#include <windows.h>
#include <malloc.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
    int* out = new int[2048];
    bool res1 = HeapValidate(GetProcessHeap(), HEAP_NO_SERIALIZE, out);
    bool res2 = HeapValidate((HANDLE)_get_heap_handle(), 0, out);
    printf("%d %d\n", res1, res2);
    return 0;
}

When you compile this "cl test.cpp", the output will be "0 1", indicating the 2nd HeapValidate passed. Note that this is not true always - compiling /MTd or /MDd, so the debug CRT is used, will output "0 0". That's because the debug heap adds sentinel bytes around both sides of memory allocations, so the pointer returned from, say, "new", is not a pointer that can be passed to HeapValidate. The same is true in other circumstances, for instance using new to allocate an array of destructable objects. In general, it's a bad idea to assume you can pass any pointer allocated from the CRT heap directly to HeapValidate. That makes assumptions on internal implementation details of the CRT heap which are either invalid, or might be invalid in some builds, or in the future. It's better to use the debug CRT instead to uncover heap problems of this sort.

Phil Lucido
VC++ developer
Sign in to post a workaround.