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
        • ../../**

    2011/06/15

    Readable Terminal Colors for OSX Snow Leopard

    1. edit .bash_profile

    • add "export CLICOLOR=1"
    • unlike Linux, .bashrc is not default bash config file (will not be loaded automatically) -- use .bash_profile instead

    2. install SIMBL

    3. install plugins for SIMBL

    • download plugin
    • extract to ~/Library/Application Support/SIMBL/Plugins

    4. set Terminal.app to 32bit mode

    • goto Application/Utilities
    • Terminal -> right click -> Get Info
    • Check "Open in 32-bit mode"
    • reference

    5. install color theme for Terminal

    2009/04/19

    Workaround for Wireless Disconnection Issue (Linux)



    I wanted to run ubuntu server with wireless connection. But it is disconnected randomly. I have no idea and I couldn’t find any solution online. So, I made cron job, which ping to router every 2 minute, and restart networking if there is 0 response.

    1. prepare script file to run every 2 minutes
    # wireless_check.sh
    # ping check and log time and result
    #!/bin/bash
    a=`ping -q -c 3 192.168.1.1 | grep "0 received"`
    b=`date "+%Y%d%m %T"`
    if $a then 
    echo "$b it's connected" >> /home/sean/wireless_status
    else
    echo "$b it's not connected" >> /home/sean/wireless_status 
    sudo /etc/init.d/networking restart 
    fi

    2. run cron editor
    $ sudo crontab -e


    3. add new cron job
    */2 * * * * /home/sean/wireless_check.sh


    4. New cron job starts automatically and you can check your log file. Here is my log file.
    20091904 01:08:13 it's not connected 
    20091904 01:10:13 it's not connected 
    20091904 01:12:13 it's not connected 
    20091904 01:14:13 it's not connected 
    20091904 01:16:13 it's not connected 
    20091904 01:18:13 it's not connected 
    20091904 01:20:04 it's connected 
    20091904 01:22:13 it's not connected 
    20091904 01:24:13 it's not connected 
    20091904 01:26:03 it's connected 
    20091904 01:28:03 it's connected 
    20091904 01:30:04 it's connected 
    20091904 01:32:03 it's connected 
    20091904 01:34:03 it's connected 
    20091904 01:36:03 it's connected 
    20091904 01:38:03 it's connected 
    20091904 01:40:03 it's connected 
    20091904 01:42:03 it's connected 
    20091904 01:44:13 it's not connected 
    20091904 01:46:04 it's connected 
    20091904 01:48:04 it's connected 
    20091904 01:50:03 it's connected
    

    Labels

    Followers