views:

581

answers:

3

Sorry for long description, however the questions aren't so easy...

My project written without GC. Recently I found a memory leak that I can't find. I did use new Xcode Analyzer without a result. I did read my code line by line and verified all alloc/release/copy/autorelease/mutableCopy/retain and pools... - still nothing.

Preamble: Standard Instruments and Omni Leak Checker don't work for me by some reason (Omin Tool rejects my app, Instruments.app (Leaks) eats too many memory and CPU so I have no chance to use it).

So I wanna write and use my own code to hook & track "all" alloc/allocWithZone:/dealloc messages statistics to write some simple own leaks checking library (the main goal is only to mark objects' class names with possible leaks).

The main hooking technique that I use:

  Method originalAllocWithZone = class_getClassMethod([NSObject class],@selector(allocWithZone:));
  if (originalAllocWithZone)
  {
   imp_azo = (t_impAZOriginal)method_getImplementation(originalAllocWithZone);
   if (imp_azo)
   {
    Method hookedAllocWithZone = class_getClassMethod([NSObject class],@selector(hookedAllocWithZone:));
    if (hookedAllocWithZone)
    {
     method_setImplementation(originalAllocWithZone,method_getImplementation(hookedAllocWithZone));
     fprintf(stderr,"Leaks Hook: allocWithZone: ; Installed\n");
    }
   }
  }
  • code like this for hook the alloc method, and dealloc as NSObject category method.


I save IMP for previous methods implementation then register & calculate all alloc/allocWithZone: calls as increment (+1) stat-array NSInteger values, and dealloc calls as decrement (-1).

As end point I call previous implementation and return value.

In concept all works just fine.

If it needs, I can even detect when class are part of class cluster (like NSString, NSPathStore2; NSDate, __NSCFDate)... via some normalize-function (but it doesn't matter for the issues described bellow).

However this technique has some issues:

  • Not all classes can be caught, for example, [NSDate date] doesn't catch in alloc/allocWithZone: at all, however, I can see alloc call in GDB
  • Since I'm trying to use auto singleton detection technique (based on retainCount readind) to auto exclude some objects from final statistics, NSLocale creation freezes on pre-init stage when starting of full Cocoa application (actually, even simple Objective-C command line utility with the Foundation framework included has some additional initialization before main()) - by GDB there is allocWithZone: calls one after other,....


Full Concept-Project draft sources uploaded here: http://unclemif.com/external/DILeak.zip (3.5 Kb)

Run make from Terminal.app to compile it, run ./concept to show it in action.


The 1st Question: Why I can't catch all object allocations by hooking alloc & allocWithZone: methods?

The 2nd Question: Why hooked allocWithZone: freezes in CFGetRetainCount (or [inst retainCount]) for some classes...

A: 

Start from the Xcode templates. Don't try to roll your own main() routine for a cocoa app until you know what you're doing.

NSResponder
There are many reasons to roll your own main(). This isn't one of them, though.
bbum
Yep, the Demo example isn't Cocoa - it's just command line utility that uses Foundation framework for simplify demo.The difference between Cocoa way and this simple App is hidden initialization before main()/NSApplicationMain().Try to uncomment NSLocale line in the concept.m - to see the freeze issue that you'll see if you build DILeak library with original Cocoa app.Look at [NSDate date] to verify that it wasn't logged by alloc-tracker.
UncleMiF
+5  A: 

Holy re-inventing the wheel, batman!

You are making this way harder than it needs to be. There is absolutely no need whatsoever to roll your own object tracking tools (though it is an interesting mental exercise).

Because you are using GC, the tools for tracking allocations and identifying leaks are all very mature.

Under GC, a leak will take one of two forms; either there will be a strong reference to the object that should long ago been destroyed or the object has been CFRetain'd without a balancing CFRelease.

The collector is quite adept at figuring out why any given object is remaining beyond its welcome.

Thus, you need to find some set of objects that are sticking around too long. Any object will do. Once you have the address of said object, you can use the Object Graph instrument in Instruments to figure out why it is sticking around; figure out what is still referring to it or where it was retained.

Or, from gdb, use info gc-roots 0xaddr to find all of the various things that are rooting the object. If you turn on malloc history (see the malloc man page), you can get the allocation histories of the objects that are holding the reference.


Oh, without GC, huh...

You are still left with a plethora of tools and no need to re-invent the wheel.

The leaks command line tool will often give you some good clues. Turn on MallocStackLoggingNoCompact to be able to use malloc_history (another command line tool).

Or use the ObjectAlloc instrument.

In any case, you need to identify an object or two that is being leaked. With that, you can figure out what is hanging on to it. In non-GC, that is entirely a case of figuring out why it there is a retain not balanced by a release.

bbum
He said he's writing it "without" GC. Informative answer for those of us writing WITH GC, however.
jbrennan
Oops. Without GC, huh.
bbum
Nice tips. Thanks. I tried leaks tool.However my background goal is write some library to auto check leaks from code in the DEBUG build, for Unit Tests. So I need control test points from code - activate leaks catching for some threads, read stat... to automatize the procedure...I did find my leaks. There was nothing mysterious - just missed autorelease at one place.However both my questions are still opened.
UncleMiF
+4  A: 

Even without the Leaks instrument, Instruments can still help you.

Start with the Leaks template, then delete the Leaks instrument from it (since you say it uses too much memory). ObjectAlloc alone will tell you all of your objects' allocations and deallocations, and (with an option turned on, which it is by default in the Leaks template) all of their retentions and releases as well.

You can set the ObjectAlloc instrument to only show you objects that still exist; if you bring the application to the point where no objects (or no objects of a certain class) should exist, and such objects do still exist, then you have a leak. You can then drill down to find the cause of the leak.

This video may help.

Peter Hosey
Thanks.This way is preferred, but, as I told before - sometimes Instruments tool eats too many memory and traced App is unreally slowdown...But by limiting some input data and monitoring time - there is possible cooperation with Instruments.
UncleMiF
I use the following solution now for catch memory consuming leaks:Start Instruments, open Leaks template, remove Object Alloc track, run App with Leaks only (also I set auto-detections time to 30-60 seconds value). It gives 10 times memory and time economy while tracing Leaks!
UncleMiF
Cool solution Peter, this Instruments thing is amazingly flexible.
Warren P