NSString causing memory leak

Discussion in 'iOS Programming' started by man2manno, May 23, 2014.

  1. man2manno macrumors member

    Joined:
    Mar 21, 2009
    #1
    Hey guys,

    I built part of my app and sometimes when I run it, it works fine other times when I push a button that retrieves a global NSString value makes the app crash. The only thing I can think is that this caused by a memory leak from the NSString. I believe I have it initialized correctly and I am releasing it in the dealloc method.

    Any ideas?

    RootViewController.h
    Code:
    @interface RootViewController: UIViewController {
    	
    	// Declare the text views for viewing the messages on the app
    	IBOutlet UILabel *latestTxtView;
    	IBOutlet UILabel *unreadTxtView;
    	
    	// Declare buttons for retrieving the messages
    	IBOutlet UIButton *latestTxtButton;
    	IBOutlet UIButton *sendButton;
    	
    	// Declare string to hold values to be sent out
    	NSString *latestTxt;
    }
    
    @property(nonatomic,retain)IBOutlet UITextView *latestTxtView;
    @property(nonatomic,retain)IBOutlet UITextView *unreadTxtView;
    @property(nonatomic,retain)IBOutlet UIButton *latestTxtButton;
    
    @property(nonatomic,retain)IBOutlet UIButton *sendButton;
    
    @property(nonatomic,retain)NSString *latestTxt;
    
    // Action Items for the buttons
    - (IBAction)getLatestTxt:(id)sender;
    - (IBAction)pushSendButton:(id)sender;
    @end
    
    
    RootViewController.m
    Code:
    @implementation RootViewController
    @synthesize latestTxtView, unreadTxtView, latestTxtButton, sendButton, latestTxt;
    
    - (IBAction)getLatestTxt:(id)sender {
        ....
        latestTxt = [NSString stringWithCString:contentTxt encoding:NSUTF8StringEncoding];
        ....
    }
    
    - (IBAction)pushSendButton:(id)sender {
       ....
       unreadTxtView.text = latestTxt; // I believe this is the leak
    }
    @end
    
    It is also important to note that in the app delegate I am calling the dealloc method...

    Code:
    - (void)dealloc {
    	
        [_latestTxt release];
        
        [_latestTxtView release];
        [_latestTxtButton release];
        
        [_unreadTxtView release];
        
    	[_viewController release];
    	[_window release];
    	[super dealloc];
    }
    
    Any ideas where this leak may be coming from? Have I properly defined/declared a global NSString?

    Thanks!
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    If you define a property for latestTxt, you should reference it using self, as in:
    Code:
    self.latestTxt = [NSString stringWithCString:contentTxt encoding:NSUTF8StringEncoding];
    You should have this dealloc in your RootViewController's class.

    Which NSString do you think you've define as global? I don't see one.

    P.S. I see you're not using ARC. Any particular reason why?
     
  3. man2manno thread starter macrumors member

    Joined:
    Mar 21, 2009
    #3
    Thanks for the help.

    Well I thought by declaring latestTxt in the header I was making it global, is that not correct?

    I didn't think the ARC was needed for this program seeing that I am really only intending the change the value of the string one time. Maybe my understanding of the auto reference counting is not accurate.
     

Share This Page