2011/12/11

Avoid Excessive Retaining Mistake

When I ran "Analyze" on my project, I got a lot of "potential leak" warnings. I thought I was carefully managing object retaining but I did stupid mistakes everywhere.

part of the sample header file
@property (nonatomic, retain) ClassA objA;

part of the sample source file
@synthesize objA;
...
self.objA = [[ClassA alloc] init];
...
// done using objA instance
[self.objA release];

Above example seems good and there is no places for mistake. But actually retaining is happened twice.

  • objA has "retain" attribute
  • it will retain anything with assigning operator ("=")
  • "alloc" method will increase retain count too

    Corrected code
    Class *tmpA = [[ClassA alloc] init];
    self.objA = tmpA;
    [tmpA release];
    ...
    // done using objA instance
    [self.objA release];
    

    Objective-C Singleton Class

    Singleton class is really awesome when I need to share some data throughout many places. If I use normal way which is passing object pointer around, I have to care about a lot of object assigning tasks. With singleton class, it is really simple to use shared class object.

    • no instanciation
    • one class method do the all necessary job
    • first time of the class method call > create single instance
    • other class method calls > return already created instance
    • how to use?
      • include singleton class's header file
      • use class method to obtain class instance

    Sample Class's header file
    @interface SampleClass : NSObject
    
    + (SampleClass*)sharedInstance;
    
    @end
    

    Sample Class's source file
    @implementation SampleClass
    
    static SampleClass *sharedInstance = nil;   // shared instance
    
    + (SampleClass*)sharedInstance
    {
        @synchronized(self)
        {
            if (sharedInstance == nil)
                sharedInstance = [[super allocWithZone:nil] init];
    
            return sharedInstance;
        }
    }
    
    + (id)allocWithZone:(NSZone*)zone
    {
        return [[self sharedInstance] retain];
    }
    
    - (id)copyWithZone:(NSZone*)zone
    {
        return self;
    }
    
    - (id)retain
    {
        return self;
    }
    
    - (NSUInteger)retainCount
    {
        return NSUIntegerMax;  // object cannot be released
    }
    
    - (oneway void)release
    {
        // do nothing
    }
    
    - (id)autorelease
    {
        return self;
    }
    

    Use Code Block in Blogspot

    I got all of these informations form here
    1. go to "Template Designer" of your blog
    2. select Advanced > Add CSS
    3. add css code

    pre.code
    {
    font-size:12px;
    border:1px dashed #CCCCCC;
    width:99%;
    height:auto;
    overflow:auto;
    color:#000000;
    background:#f0f0f0;
    background-image:URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif);
    padding:0px;
    padding-left:2px;
    text-align:left;
    line-height:20px;
    word-wrap:normal;
    }
    

    You can use them with <pre class="code"> tag

    2011/12/08

    Working with Static Libraries in Xcode

    Layout
    • single workspace
    • app project AProj
    • static library project BProj

    Configurations
    • In BProj
      • Target > Build Phase > Copy Headers
        • add header files
      • Project > Build Settings > Public Headers Folder Path
        • set to header folder name (ex. LIBA for )
      • Project > Build Settings > Skip Install
        • Yes
    • In AProj
      • Target > Build Phase > Link Binary With Libraries
        • add library file (ex. lib.a)
      • Project > Build Settings > Header Search Paths
        • ../../**

    Labels

    Followers