Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

man2manno

macrumors member
Original poster
Mar 21, 2009
55
0
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!
 
RootViewController.m
Code:
    latestTxt = [NSString stringWithCString:contentTxt encoding:NSUTF8StringEncoding];

If you define a property for latestTxt, you should reference it using self, as in:
Code:
self.latestTxt = [NSString stringWithCString:contentTxt encoding:NSUTF8StringEncoding];

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];
}

You should have this dealloc in your RootViewController's class.

Have I properly defined/declared a global NSString?

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?
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.