Deallocate nonatomic copy setter

Discussion in 'iOS Programming' started by igorland, Mar 4, 2014.

  1. igorland macrumors newbie

    Joined:
    Mar 4, 2014
    #1
    Hello, World!

    I have some experience in Java and C++ and this is my first experience with Objective C and iOS development. So, please bear with me if my question is silly.

    I am trying to create non-atomic copy accessors, and I read everywhere that the object should be released at the end. So, if you could help me understand whether I am doing it properly, I would appreciate it. Will the following be correct?

    Code:
    
        @interface ClassA: NSObject
        {
           NSString* stringA;
        }
    
    
        @property (nonatomic, copy) NSString* stringA;
    
        @implementation ClassA
    
        @synthesize stringA;
    
        -(void) setStringA: (NSString*) stringInput {
           if(stringA != stringInput) {
    	      [stringA release];
    	      stringA = [stringInput copy];
           }
        }
    
        -(void) dealloc {
            [stringA release];
            [super dealloc];
        }
    
    
    I am looking for a confirmation whether I need to deallocate stringA in the dealloc method at the end and whether I did it correctly.

    Many thanks.
     
  2. hollersoft macrumors member

    Joined:
    Feb 10, 2013
    #2
    Can't help much except to suggest you ditch the manual memory management and use ARC. You could just call -copy on the input string, set your property and you're done.

    You don't need @synthesize these days either. And I would not recommend having the property and instance variable with the exact same name - that's confusing. Stick with the standard convention where @property stringA is auto-synthesized using an instance variable _stringA.
     
  3. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #3
    You are correct. In manual memory management you need to do that release in the dealloc. If you don't, you'll leak memory.
     
  4. igorland thread starter macrumors newbie

    Joined:
    Mar 4, 2014
    #4
    Thanks a lot, guys!

    hollersoft. I am working on a project for iPad/iPhone. Unless something has been changed recently, as far as I understand, it does not have ARC, therefore I need to release it manually.

    Will the following be better?

    Code:
    
    @synthesize stringA = _stringA;
    
        -(void) setStringA: (NSString*) stringInput {
           if(_stringA != stringInput) {
    	      [_stringA release];
    	      _stringA = [stringInput copy];
                  _stringA = [_stringA uppercaseString]; // reason for customized       setter
           }
        }
    
    
    Thank you so much!
     
  5. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #5
    iOS does support ARC. Actually it depends on the Xcode version you're using. If you are going to post your app to the App Store, then you'll have to use Xcode 5 and at least SDK7.
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    Transitioning to ARC Release Notes:
    ARC is supported in Xcode 4.2 for OS X v10.6 and v10.7 (64-bit applications) and for iOS 4 and iOS 5. Weak references are not supported in OS X v10.6 and iOS 4.​

    Which version of Xcode are you using?
    Which version of iOS are you targeting?
     
  7. igorland thread starter macrumors newbie

    Joined:
    Mar 4, 2014
    #7
    Xcode 5.0

    I guess, the latest, or at least 6. Not sure if someone is still using anything lower than that.

    Thanks for pointing to the statement. I guess I need to be careful when googling for this kind of things.

    In light of the change, would you recommend synthesizing the setter (since I need to capitalize the ivar) and deleting the dealloc method in the end?

    Thank you!
     
  8. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #8
    If you're calling uppercaseString, then I don't see why you need to call copy. uppercaseString is going to give you an uppercase copy anyway.
     

Share This Page