Re: Debugging memory corruption? (C++)
Re: Debugging memory corruption? (C++)
- Subject: Re: Debugging memory corruption? (C++)
- From: Jean-Denis Muys <email@hidden>
- Date: Sat, 31 Oct 2009 10:27:01 +0100
Followup on my memory corruption bug:
I got a few suggestions (thanks everybody):
- a memory watch point: I may be able to use that idea, but so far, I
could not clearly identify a fixed location that's corrupted.
- Guard Malloc: I have a machine that I started about 12 hours ago
with Guard Malloc enabled. The host app hasn't finished launching yet.
- Valgrind: I still have 10.5 running on a Mac mini, so I started the
host app under Valgrind. It's veryyyy slow. Still running.
- static analyzer: it doesn't detect anything wrong (but of course, my
code is C++).
I also wanted to report that I made some significant progress on how
to call malloc heap checking facility at will. Use something like the
following hack:
#if qCheckHeap
typedef void (*internal_check_entry_point)(void);
extern "C" int malloc_zone_check(int);
void zoneCheck() {
internal_check_entry_point internal_check = internal_check_entry_point
((char*)&malloc_zone_check+1693);
int c = malloc_zone_check(0);
if (c != 1) {
Debugger();
(*internal_check)();
}
}
#else
void zoneCheck() {}
#endif
Explanation and caveat:
• the "1693" constant in the code above is experimental and may be
different on your machine. Read on.
• int malloc_zone_check(int) runs the heap check itself. It's not in
the headers, but the symbol is exported in the library. It returns 1
it the check is OK. Otherwise it also prints some diagnostic in the
console. Its parameter is the zone to check. If zero, it checks all
zones.
• void internal_check(void) is a routine in malloc.c that malloc calls
every time its malloc_check_counter is triggered. It's the one you
want to call really. It calls malloc_zone_check(0) and the stack crawl
for the last operation that succeeded without corruption, together
with the last malloc_check_counter, and suggestions for new settings
for MallocCheckHeapEach and MallocCheckHeapStart
• Unfortunately, internal_check(void) is defined as static, so the
symbol is not exported to the linker. However, it's at a constant
offset from malloc_zone_check(). On my machine, that offset is 1693.
That's my code above calls it.
• here is how I determined that constant: I set a breakpoint to
malloc_zone_check. I wrote down its address (0x991838b1 on my
machine). Often, malloc_zone_check was called from internal_check.
Clicking on internal_check's stack frame, I wrote down its address
(0x99183f4e on my machine). The difference is 1693, as Calculator told
me...
• I found all this out reading through malloc.c source code on opensource.apple.com
I hope this helps others, and I still welcome suggestions for my bug :-)
Jean-Denis
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden