Devendra Sahu
Devendra Sahu

Reputation: 15

iphone app : EXC_BAD_ACCESS, message sent to deallocated instance

I am a new iPhone developer. I am working on an application which used two table views. My code was working fine with iOS 6, but when I went to iOS 7, and tried to launch the application I get into below error. Based on some answers present over the net, I Enable Zombie. The initial error message that was returned was EXC_BAD_ACCESS. When I enabled NSZombieEnabled I got the error message "-[CFString retain]: message sent to deallocated instance"

Here is the piece of code, throwing error, The causing the error is,

SystemViewController.h

if(indexPath.section == 0)
{
  //User Info section
  if(indexPath.row == 0)
  {
    cell2.label1.text = @"User";
    cell2.label2.text = [UserInfoBean userName];
  }
  else
  {
   cell2.label1.text = @"SI Host URL";
   cell2.label2.text = [UserInfoBean url];
  }
  cell = cell2;
}

Interface UserInfoBean.h

@implementation UserInfoBean

static NSString *userName;
static NSString *url;
static NSString *presentNode;




+ (NSString *)userName { return userName; }
+ (NSString *)url { return url; }
+ (NSString *)presentNode { return presentNode; }


+ (void)setUserName:(NSString *)newVar { userName = newVar; }
+ (void)setUrl:(NSString *)newVar { url = newVar; }
+ (void)setPresentNode:(NSString *)newVar { presentNode = newVar; }

@end

Thanks in advance!

Upvotes: 0

Views: 260

Answers (1)

Mike
Mike

Reputation: 9835

I would recommend using properties instead of writing your own variable accessor methods, like this:

@property (nonatomic, retain) NSString *userName; 

The compiler takes care of all the setting/getting those variables, and using retain will do the following:

Retaining an object creates a strong reference, and an object cannot be deallocated until all of its strong references are released. If two objects retain each other, neither object ever gets deallocated because the connection between them cannot be broken

Once you create the property, you can remove the getter/setter methods and simply use UserInfoBean.userName = @"something" to set or NSString *something = UserInfoBean.userName to get.

Upvotes: 1

Related Questions