Yet another EXC_BAD_ACCESS - However with zombie (instruments), never exe_bad_access - objective-c

I have spent hours on this and read everybit on the web about memory management, zombies. leaks (tried instruments). But I cannot figure this one out. Does someone has a clue? I am getting a EXC_BAD_ACCESS at the following like of code on popping the ChildViewController.
[profileVO release]; profileVO = nil;
I strongly feel I have followed all memory management best practices!
Details:
I have a model file. (CachedProfileVO)
CachedProfileVO.h
#interface CachedProfileVO : NSObject {
NSString *name;
NSString *email;
}
#property (nonatomic, retain) NSString *name;
#property (nonatomic, retain) NSString *email;
#end
CachedProfileVO.m
#synthesize name, email;
- (void) dealloc
{
[super dealloc];
[name release]; name= nil;
[email release]; email = nil;
}
Now I have a UINavigationController. The ParentViewController and the ChildViewController.
I invoke the ChildViewController as follows:
[self.navigationCntroller pushViewcontroller:childViewcontroller animated:YES];
In the ChildViewController, I basically use the model CachedProfileVO. However when this view controller is popped (back button on UI), it gives a EXC_BAD_ACCESS
ChildViewController.h
#interface ChildViewcontroller : UITableViewController {
CachedProfileVO *profileVO;
}
#property (nonatomic, retain) CachedProfileVO *profileVO;
#end
ChildViewController.m
#synthesize profileVO;
- (void) dealloc
{
[super dealloc];
[profileVO release]; profileVO = nil; ****** GETTING EXE_BAD_ACCESS here
}
- (void) viewDidLoad
{
CachedProfileVO *vo = [CachedProfileVO alloc] init];
self.profileVO = vo;
[vo release];
}
//responseString looks like this: [Murdoch, murdoch#email.com][other data][more data]
- (void) populateProfile:(NSString *) responseString
{
NSMutableString *str = [[NSMutableString alloc] initWithCapacity:20];
[str setString:responseString];
[str deleteCharactersInRange: NSMakeRange(0,1)];
[str deleteCharactersInRange: NSMakeRange([str length]-1,1)];
NSArray *tempArray = [str componentsSeparatedByString: #"]["];
NSString *tempStr = (NSString*)[tempArray objectAtIndex:0];
NSArray *bioArray = [tempStr componentsSeparatedByString:#","];
self.profileVO.name = (NSString*)[bioArray objectAtIndex:0];
self.profileVO.email= (NSString*)[bioArray objectAtIndex:1];
[str release]; str = nil;
}
Note that the function populateProfile is called after some event. I know it is called. And dealloc then causes the problem. Also this does not happen in every pop. I have to try several times to reproduce. It is never reproduced using zombies in instruments!!!

You are calling [super dealloc]; first in your examples. That should always be the last call otherwise you are accessing instance variables that belong to a now deallocated class. The following should work fine if you followed memory management rules elsewhere.
- (void) dealloc
{
[profileVO release];
[super dealloc];
}

Related

Parameters to thread Objective-C

Trying to use class variables in thread and getting EXC_BAS_ACCESS.
Code snippet:
#interface ViewController : UIViewController {
NSString* accountLoginName;
NSString* accountPassword;
}
in implementation:
accountLoginName = [NSString stringWithString:textFieldLoginName.text];
accountPassword = [NSString stringWithString:textFieldPassword.text];
[self performSelectorInBackground:#selector(loginAtBackgroundSelector:) withObject:nil];
-(void)loginAtBackgroundSelector:(UIAlertView*)alert
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(#"%#\n%#", accountLoginName, accountPassword);
[self login];
[self dismissAlert:alert];
[pool release];
}
just trying to write to console and getting error in this part of code, but error in the loginAtBackgroundSelector appears from time to time.
-(AlertType)login
{
NSLog(#"%#\n%#", accountLoginName, accountPassword);
}
Try this in interface:
#interface ViewController : UIViewController {
NSString* accountLoginName;
NSString* accountPassword;
}
#property(nonatomic, retain) NSString* accountLoginName;
#property(nonatomic, retain) NSString* accountPassword;
And this in the implementation (in the place where you assign the values):
self.accountLoginName = [NSString stringWithString:textFieldLoginName.text];
self.accountPassword = [NSString stringWithString:textFieldPassword.text];
[self performSelectorInBackground:#selector(loginAtBackgroundSelector:) withObject:nil];
Additionaly in the dealloc:
-(void)dealloc {
[accountLoginName release];
[accountPassword release];
[super dealloc];
}
Let me know if it helps.

objective-c threading with NSobject subclass

Im using a threading class (.h/.m below) where the subclass is UIViewcontroller works without any issues.
#interface myFirstClass : UIViewController <MyOperationDelegate>{
However when I use it where the subclass is a NSobject to call a reachability class checking for internet connection, the App crashes when calling performSelectorOnMainThread? I dont understand why, there are no error when I build the App and when it crashes all i get is EXC_BAS_ACCESS. Is it not possible to do this when dealing with an NSObject? Any suggestion will be helpful for me.
#interface AppController : NSObject <MyOperationDelegate>{
myThreading.h
#protocol MyOperationDelegate
#required
-(void) updatedStatus:(NSArray*)items;
-(void) failedStatusWithError:(NSError*)error;
#end
#interface MyOperation : NSObject {
NSObject<MyOperationDelegate> * delegate;
NSOperationQueue *queue;
}
#property (retain) NSObject<MyOperationDelegate> *delegate;
-(void)load: (NSString *)stringUrlPath:(NSString *)functionAction;
#end
myThreading.m
#interface MyOperation (NSObject)
-(void)dispatchLoadingOperation:(NSDictionary *)aParameters;
#end
#implementation MyOperation
#synthesize delegate;
-(id)init
{
if ([super init]!=nil) {
queue = [NSOperationQueue new];
[queue setMaxConcurrentOperationCount:1];
}
return self;
}
-(void)load: (NSString *)stringUrlPath: (NSString *)functionAction {
[self dispatchLoadingOperation:[NSDictionary dictionaryWithObjectsAndKeys:
stringUrlPath, #"urlString", functionAction, #"action", nil]];
}
-(void)dealloc {
[queue cancelAllOperations];
self.delegate = nil;
[super dealloc];
}
-(void)dispatchLoadingOperation:(NSDictionary *)aParameters {
if([aParameters objectForKey:#"action"] == #"getStatus"){
#synchronized(self) {
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:#selector(fetchCheckStatus:)
object:aParameters];
[queue addOperation:operation];
[operation release];
}
}
}
-(void) fetchCheckStatus:(NSDictionary *)aParameters
{
NSData* data = [[NSMutableData alloc] initWithContentsOfURL:[NSURL URLWithString:[aParameters objectForKey:#"urlString"]] ];
NSError *error;
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (responseString != nil) {
NSMutableArray *rssItems;
[self.delegate performSelectorOnMainThread:#selector(updatedStatus:) withObject:[NSArray arrayWithObjects:rssItems, nil] waitUntilDone:NO];
} else {
[queue cancelAllOperations];
[self.delegate performSelectorOnMainThread:#selector(failedStatusWithError:) withObject:error waitUntilDone:NO];
}
[responseString autorelease];
[data release];
}
#end
The problem are these lines:
NSMutableArray *rssItems;
[self.delegate performSelectorOnMainThread:#selector(updatedStatus:) withObject:[NSArray arrayWithObjects:rssItems, nil] waitUntilDone:NO];
You declare a variable rssItems but don't set it. It will contain random garbage from the stack which will then be interpreted as a pointer. Maybe sometimes you're lucky and the value is actually a pointer to a living object, but more likely dereferencing it causes your crash.
You need to actually initialize the variable, e.g.:
NSMutableArray *rssItems = nil;
but I guess you really want:
NSMutableArray *rssItems = [NSMutableArray array];

Objective-c addObject in loop causes memory leak

I have found a similar issue:
NSMutableArray addObject in for loop - memory leak
But none of those suggestions seem to fix my problem.
I have a simple loop where I'm creating an object and adding it to an array. When I try to release the object at the end of each loop the app crashes with "EXC_BAD_ACCESS". If I don't release the object I get leaked memory:
In .h
NSMutableArray *mainlist;
...
#property (nonatomic, retain) NSMutableArray *mainList;
In .m
#synthesize mainlist;
...
for (int i = 0; i < [self.objects count]; i++) {
MyObj *myObj = [[MyObj alloc] init];
myObj.title = [[self.objects objectAtIndex: i] valueForKey: #"title"];
[self.mainlist addObject:myObj];
[myObj release]; // crashes with release
}
MyObj just has some properties:
#property (nonatomic, retain) NSString *title;
#property (nonatomic, retain) NSString *date_text;
...
#synthesize title;
#synthesize date_text;
- (void)dealloc
{
[super dealloc];
[title release];
[date_text release];
}
#end
Any help would be much appreciated.
Thanks.
Crashes cause you first call dealloc of superclass and then try to release attributes. Change this to:
- (void)dealloc
{
[title release];
[date_text release];
[super dealloc];
}
And also: I'm almost certain that your self.mainlist is nil, when you're adding objects there. Creating a property doesn't mean that the attribute would be initialized automatically.

incompatible Objective-c types warning

I have a user class that I use through the iPhone application, this is the init and initWithUser functions from my user class (A SUBCLASS OF NSobject), when I use the initWithUser function I get the warning described after the code. please advise.
// serialize.h
#import <Foundation/Foundation.h>
#protocol Serialize
// serialize the object to an xml string
-(NSString*)ToXML;
#end
// user.h
#import <Foundation/Foundation.h>
#import "Serialize.h"
#import "Contact.h"
#interface User : NSObject <Serialize> {
NSString *email;
NSString *firstName;
NSString *lastName;
NSString *userId;
NSString *userName;
NSString *password;
NSMutableArray *contactList;
}
#property (nonatomic,copy) NSString *email;
#property (nonatomic,copy) NSString *firstName;
#property (nonatomic,copy) NSString *lastName;
#property (nonatomic,copy) NSString *userId;
#property (nonatomic,copy) NSString *userName;
#property (nonatomic,copy) NSString *password;
#property (nonatomic, retain) NSMutableArray *contactList;
//-(id)init;
-(id)initWithUser:(User *)copyUser;
#end
// user.m
#import "user.h"
#implementation User
#synthesize email;
#synthesize firstName;
#synthesize lastName;
#synthesize userId;
#synthesize userName;
#synthesize password;
#synthesize contactList;
-(id)init
{
// call init in parent and assign to self
if( (self = [super init]) )
{
// do something specific
contactList = [[NSMutableArray alloc] init];
}
return self;
}
-(id)initWithUser:(User *)copyUser
{
if( (self = [self init]) ) {
email = copyUser.email;
firstName = copyUser.firstName;
lastName = copyUser.lastName;
userId = copyUser.userId;
userName = copyUser.userName;
password = copyUser.password;
// release contactList initialized in the init
[contactList release];
contactList = [copyUser.contactList mutableCopy];
}
return self;
}
- (void)dealloc
{
// TODO:
[contactList removeAllObjects];
[contactList release];
[super dealloc];
}
// implementation of serialize protocol
-(NSString*)ToXML
{
return #"";
}
and I use it in the main controller this way
- (void) registerNewUser {
RegistrationViewController *regController = [[RegistrationViewController alloc] init] ;
regController.newUser = [[User alloc] initWithUser:self.user];
[self.navigationController pushViewController:regController animated:YES];
[regController release];
}
the line
regController.newUser = [[User alloc] initWithUser:self.user];
gives me the following error, and its been driving me nuts for a couple of days:
incompatible Objective-c types 'struct User*', expected 'struct NSString *' when passing argument 1 of 'initWithUser:' from distinct Objective-c type
any help and guidance is appreciated
The problem is you have an ambiguous selector. Because alloc returns id, the call to initWithUser: has become ambiguous. NSUserDefaults also has an initWithUser: function which takes a string. The compiler thinks you're trying to use that one. Change the line to
regController.newUser = [(User*)[User alloc] initWithUser:self.user];
and everything should work as expected.
As mentioned in the comments, there are other problems with your implementation. In your initializer, reusing the -init is redundant and the assignments to ivars like email should be taking ownership of the data using -copy or -retain:
-(id)initWithUser:(User *)copyUser {
if((self = [super init])) {
// take ownership of the users data by copying or retaining:
email = [copyUser.email copy];
// ...
contactList = [copyUser.contactList mutableCopy];
}
return self;
}
In -dealloc, -removeAllObjects can be removed and the member data has to be released:
- (void)dealloc {
[email release];
// ...
[contactList release];
[super dealloc];
}
Note that you are also leaking the new User instance if newUser is a copy or retain property as there is a release missing:
User *user = [[User alloc] initWithUser:self.user];
regController.newUser = user;
[user release];

Why does this crash?

I made a class. This is the h file.
// MyClass.h
#import <Foundation/Foundation.h>
#interface MyClass : NSObject <NSCoding> {
NSString *string1;
NSString *string2;
}
#property (nonatomic, retain) NSString *string1;
#property (nonatomic, retain) NSString *string2;
#end
This is the m file.
// MyClass.m
#import "MyClass.h"
#implementation MyClass
#synthesize string1, string2;
- (void)encodeWithCoder:(NSCoder *)coder;
{
if (self = [super init]){
[coder encodeObject:string1 forKey:#"string1"];
[coder encodeObject:string2 forKey:#"string2"];
}
}
- (id)initWithCoder:(NSCoder *)coder;
{
self = [[MyClass alloc] init];
if (self != nil)
{
string1 = [coder decodeObjectForKey:#"string1"];
string2 = [coder decodeObjectForKey:#"string2"];
}
return self;
}
- (void)viewDidUnload {
self.string1 = nil;
self.string2 = nil;
}
- (void)dealloc {
[super dealloc];
[string1 release];
[string2 release];
}
#end
I created an array of these objects like this:
MyClass *object1 = [[MyClass alloc] init];
object1.string1 = #"object1 string1";
object1.string2 = #"string1 string2";
MyClass *object2 = [[MyClass alloc] init];
object2.string1 = #"object2 string1";
object2.string2 = #"object2 string2";
theArray = [[NSMutableArray alloc] initWithObjects:object1, object2, nil];
Then I saved the array like this:
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:theArray] forKey:#"savedArray"];
Then I loaded the array from disk like this.
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:#"savedArray"];
if (dataRepresentingSavedArray != nil)
{
NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
if (oldSavedArray != nil)
{
theArray = [[NSMutableArray alloc] initWithArray:oldSavedArray];
}
else {
theArray = [[NSMutableArray alloc] init];
}
}
The program crashes when it gets to this line in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
cell.textLabel.text = [[theArray objectAtIndex:indexPath.row] string1];
Why does it crash there? It doesn't crash if I don't load the array from NSUserDefaults. However, I don't see anything I did wrong with saving or loading the array.
Edit:
I can also make it crash with this line of code:
NSLog(#"%#", [[theArray objectAtIndex:0] string1]) ;
In addition to cobbal's excellent points, your initWithCoder: method isn't using setters and therefore the strings aren't being retained. When you try to access string1 at your crash line, that string has probably already been released. Do this in initWithCoder: instead:
self.string1 = [coder decodeObjectForKey:#"string1"];
self.string2 = [coder decodeObjectForKey:#"string2"];
To address cobbal's first point, don't do any init at all in encodeWithCoder:. Just encode your object. Check out Apple's Archives and Serializations Programming Guide for Cocoa for more details about encoding and decoding objects.
A few things jump out at me
you're calling init in encodeWithCoder:. since you're not initializing anything here you should not be changing self
you're calling alloc in initWithCoder:. Once you're in an init method you shouldn't have to call alloc, just call self = [super init].
you have a viewDidUnload method. Even if this were called in an NSObject subclass, you probably don't want to get rid of your data when the view is unloaded.
you're calling [super dealloc] at the beginning of your dealloc method. It should go at the end.
I didn't read all of your code, so I am not sure if this is the problem, but in dealloc you should call [super dealoc] only after releasing the instance variables.
If it crashes due to being out of bounds, check that your UITableViewController correctly implements
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
and
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
to reflect the NSArray you're using as data source.