Ahsan
Ahsan

Reputation: 2982

EXC_BAD_ACCESS error on very simple code

I am using the nav controller and pushing another view into the stack and setting a variable too. While trying to access the variable I get EXEC_BAD_ACCESS :(

Here's the code (I am not using any NIB) :

#import <UIKit/UIKit.h>


@interface detailedView : UIViewController {

NSString *movieName2;
}

@property (nonatomic, retain) NSString *movieName2;

@end

and

#import "detailedView.h"


@implementation detailedView

@synthesize movieName2;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

-(void) viewDidAppear:(BOOL)animated
{

self.view=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.frame=CGRectMake(213, 300, 355, 315);
self.view.backgroundColor=[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"black.png"]];



self.title=self.movieName2;
NSLog(@"%@",movieName2);
}   

Relevant code in the caller function :

detailedView *details;


@property (nonatomic, retain) detailedView *details;

//properly synthesized and released

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

NSLog(@"Did Reach inside...");

status=1;

self.title=@"Back";
details.movieName2=self.movieName;

NSLog(@"```");
NSLog(@"2nd VC %@ sdfsdf",details.movieName2); //Getting the ERROR here
NSLog(@"1st VC %@ wrewrw",self.movieName);
//viewerSearch *viewerSearchController=[[viewerSearch alloc] init];



[[self navigationController] pushViewController:details animated:NO];    

}

init:

 details=[[detailedView alloc] init];

//movieName is a NSString and has @property(nonatomic,copy)

Upvotes: 0

Views: 568

Answers (3)

bbum
bbum

Reputation: 162722

When an app crashes, there will be a crash log or you'll be able to get a backtrace from the debugger. Always post that.

Also -- class names should always start with a capital letter.

Try Build and Analyze; make sure your memory management is correct.

Then turn on Zombie detection once you fix any build and analyze found problems.

Upvotes: 1

mrueg
mrueg

Reputation: 8225

You should always copy strings instead of retaining them. So changing

@property (nonatomic, retain) NSString *movieName2;

to

@property (nonatomic, copy) NSString *movieName2;

will probably fix your problem.

Also, if you create the value in self.movieName with @"something", do not release the value.

Upvotes: 0

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

Based on a test by calling a retain on a alloc-init-released NSString and then following it up by logging it, I think your problem is that self.movieName is deallocated. Please check whether the memory management rules have been properly followed with regards to self.movieName.

On a side note, if you are not using a NIB for detailedView then you should create the view in loadView and not viewDidAppear: as you seem to be doing. viewWillAppear: only if the view exists, right? I suggest you move the related code from viewDidAppear: to loadView.

Upvotes: 0

Related Questions