Wednesday, September 03, 2008

QuietXcode

Have you ever noticed the Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0x319a380, has non-zero refcount = 1 messages in the console ? Chris Espinosa replied about these warnings to Matt Neuburg:


The second one is an error deep in some system framework when running under Garbage Collection that we have not been able to track down yet. It simply means that somebody has neglected to do a final release on a memory block that nobody has kept a pointer to (making the final release technically impossible). The Garbage Collector knows the block is inaccessible and is freeing it, but is warning us that somebody forgot to formally release it before the pointer to it went out of scope. Bad form, but no actual harm.

I think it actually harms. These messages are filling the console so much it becomes unusable. I'm a big fan of GeekTool and I always have the tail of the console on the desktop. Now, it looks always the same and interesting messages from various applications are lost in the mass of free_garbage messages:

Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0x319a380, has non-zero refcount = 1
Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0x31bf6e0, has non-zero refcount = 1
Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0x3263da0, has non-zero refcount = 1
Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0x3274eb0, has non-zero refcount = 1
Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0x319a380, has non-zero refcount = 1
Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0x31bf6e0, has non-zero refcount = 1
Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0x3263da0, has non-zero refcount = 1
Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0x3274eb0, has non-zero refcount = 1
Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0x201c8b0, has non-zero refcount = 1
Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0x3256110, has non-zero refcount = 1
Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0x201c8b0, has non-zero refcount = 1
Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0x3256110, has non-zero refcount = 1
Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0x2045540, has non-zero refcount = 1
Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0x208ca10, has non-zero refcount = 1
Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0x2045540, has non-zero refcount = 1
Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0x208ca10, has non-zero refcount = 1

So I decided to tackle the problem and here is my Solution: QuietXcode 1.1.4 (5 KB). This is an Xcode plugin that patches the culpable call to malloc_printf("free_garbage: garbage ptr = %p, has non-zero refcount = %d", ...).

You can build the plugin either by typing xcodebuild in a terminal or by building it (⌘ + B) in Xcode. Building the plugin will also automatically install it into your ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins folder.

Once it's installed, you have to relaunch Xcode. You should see the message <QuietXcode> loaded successfully in the console and no more free_garbage messages.

The plugin performs a safe patch, that is, if it does not find the expected call to malloc_printf and the expected Xcode version (greater than or equal to 3.1/1099), it logs a more or less comprehensive error to the console. Have fun browsing the source code, it demonstrates how to use the dyld and mach-o apis to locate non exported symbols and the mach api to dynamically patch code.

Note that the plugin is for i386 only, porting it to ppc and/or 64 bits is left as an exercise to the reader.

17 comments:

J.P. said...

Thank you so very much! That message was annoying the hell out of me.

Lance Drake said...

OMG! - Thank you SO much! - My system.log file was completely unusable prior ot implementing your QuietXcode plug-in. The project built and the plug-in was installed without error. YES - IT WORKS LIKE A CHARM!

Jamie said...

This is great, thanks!

Luca said...

Good Job Cédric.
Apple should hire you to work on XCode that is actually their worst piece of software...
Codewariorr !! Where are you ??

Daniel Jalkut said...

Cool stuff. Hadn't seen it before and this just started making the rounds again (via Twitter/IRC).

Ben Lachman said...

Wondering if there are any useful error messages that are sent to the console via dyld_stub_malloc_printf from Xcode. Thoughts?

@Luca I think you must have used some version of CodeWarrior that was much better than any I ever used. Anyway, I'd say there are a lot of other developer tools that are less polished than Xcode both from Apple and other companies. Don't even try using the Qt toolset.

Marc Liyanage said...

Thanks a lot for this. My MkConsole output is usable again...

DaveKeck said...

fucking christ THANK YOU

Tony said...

"Note that the plugin is for i386 only, porting it to ppc and/or 64 bits is left as an exercise to the reader."

Unless of course the reader has nowhere near your level of skill, dear Cédric! Any plans to add 64-bit support, or are you running on a 32-bit mac?

menumachine said...

This is just great. I used the code to suppress the messages in the debug build of my own GC-enabled app, which is an enormous relief. The messages have been driving me insane, it's astonishing that this hasn't been fixed in any of the Leopard updates.

sjlinwarsaw said...

This is great. I'm looking forward to learning a little something from the code as well.

Thanks!
Scott

Anonymous said...

Hi - very nicely done - thanks for taking the time to do it it's excellent - one question for all who see this - do any of you get duplicate message in the console - I get them all the time it's also very annoying

Thanks!
Jack

Anonymous said...

Thank you. As said above, the console is now useful again. Really appreciate you making this available. Why doesn't Apple buy this from you and ship it with Xcode? -fb

veck said...

That's a great entry! Thank you!!

Anonymous said...

This is the error for me :
"QuietXcode failed to load: unexpected auto_collect_internal function"
any ideas ?

Anonymous said...

Nice work, thanks!

Here's the patch for PPC

/Michael

Ant- said...

Cool, thanks a lot for this message was really messing with my nerves :)