Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guard Malloc found EXC_BAD_ACCESS error instantly. Why not use all the time?

I have been debugging the infamous EXC_BAD_ACCESS error for a few days now. NSZombieEnabled = YES did not offer anything. The call stack was different everytime I received the error, which was once every 5 or 6 runs.

I saw a tip for enabling guard malloc (which is in the scheme editor now for Xcode 4) on Lou Franco's website: Understanding EXC_BAD_ACCESS. As soon as I did this, my program halted on the exact line that was causing this elusive error.

According to its description, guard malloc creates separate pages for every malloc and deletes the whole page when the memory is freed, thus crashing the program when the freed memory is accessed. For general development, why wouldn't I just keep guard malloc on all the time? It seems to catch certain types of memory errors easily. If I'm not testing memory management or performance specifically, is there some downside to using it?

like image 530
brodney Avatar asked Mar 18 '11 23:03

brodney


3 Answers

Not only does it waste address space, but it will significantly slow down your program (potentially to the point where it's unusable, even on the simulator). I suppose for an iOS programme when you're running it on the simulator it's a bit moot (memory isn't a problem, and the performance hit isn't terrible either), but perhaps in the name of best practice you shouldn't run it constantly.

like image 52
lxt Avatar answered Nov 14 '22 23:11

lxt


Allocating a whole 4K page for a couple bytes per malloc() wastes address space very quickly.

like image 31
geekosaur Avatar answered Nov 15 '22 00:11

geekosaur


GuardMalloc does make an app run much slower, especially if you have a large number of allocations during the normal course of execution. I keep it turned off most of the time.

I turn GuardMalloc on to debug a crash that mangles the stack. Often, these have objc_msgSend at the top of whatever is left of the stack.

With GuardMalloc, the random effects of dangling pointers are prevented. The address in the pointer cannot be re-used and its memory location is made invalid. The crash will happen almost immediately, well before the stack is corrupted. This is great for C++ legacy code as well as new Objective-C.

I do leave the other memory debugging aids on full-time.

like image 27
Walt Sellers Avatar answered Nov 15 '22 00:11

Walt Sellers