NSString causing memory leak

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

  1. man2manno macrumors member

    Mar 21, 2009
    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?

    @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;
    @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
    It is also important to note that in the app delegate I am calling the dealloc method...

    - (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?

  2. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    If you define a property for latestTxt, you should reference it using self, as in:
    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

    Mar 21, 2009
    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