Note that there are some explanatory texts on larger screens.

plurals
  1. POTrack all Objective-C's alloc/allocWithZone/dealloc
    primarykey
    data
    text
    <p>Sorry for long description, however the questions aren't so easy...</p> <p>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.</p> <p>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).</p> <p>So I wanna write and use my own code to hook &amp; 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).</p> <p>The main hooking technique that I use:</p> <pre><code> 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></pre> <ul> <li>code like this for hook the alloc method, and dealloc as NSObject category method.</li> </ul> <hr> <p>I save IMP for previous methods implementation then register &amp; calculate all alloc/allocWithZone: calls as increment (+1) stat-array NSInteger values, and dealloc calls as decrement (-1).</p> <p>As end point I call previous implementation and return value.</p> <p>In concept all works just fine.</p> <p>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).</p> <p>However this technique has some issues:</p> <ul> <li>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</li> <li>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,....</li> </ul> <hr> <p>Full Concept-Project draft sources uploaded here: <a href="http://unclemif.com/external/DILeak.zip" rel="nofollow noreferrer">http://unclemif.com/external/DILeak.zip</a> (3.5 Kb)</p> <p>Run <strong>make</strong> from <strong>Terminal.app</strong> to compile it, run <strong>./concept</strong> to show it in action. </p> <hr> <p>The <strong>1st Question</strong>: Why I can't catch all object allocations by hooking alloc &amp; allocWithZone: methods?</p> <p>The <strong>2nd Question</strong>: Why hooked allocWithZone: freezes in CFGetRetainCount (or [inst retainCount]) for some classes... </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload