0

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)

Ahsan
  • 2,964
  • 11
  • 53
  • 96
  • Where does the error occur? And are you using a NIB? – Deepak Danduprolu Jun 11 '11 at 13:56
  • @Deepak : I am not using a NIB. The error occurs on the line :NSLog(@"2nd VC %@ sdfsdf",details.movieName2); – Ahsan Jun 11 '11 at 14:01
  • @Deepak : you mean the error message ? I am not sure how to get crash logs ! any help on that ? – Ahsan Jun 11 '11 at 14:09
  • Where does movieName get initialised/set ? From the code as given it looks like it's just a dangling pointer which then gets copied to details.movieName2 ? – Paul R Jun 11 '11 at 14:12

3 Answers3

1

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.

bbum
  • 162,346
  • 23
  • 271
  • 359
0

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.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
0

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.

mrueg
  • 8,185
  • 4
  • 44
  • 66
  • Why should you "always copy strings instead of retaining them"? This sounds like horrible advice. – Steven Fisher Jun 11 '11 at 16:43
  • 2
    I can't find a definite statement from Apple right now, but look at all NSString properties of Apple's own classes, e.g the title property on UIViewController: they are copied. See also Google's Objective-C style guide: http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml – mrueg Jun 11 '11 at 16:52
  • Hmm. My suspicion is that they're trying to avoid having the property set to a NSMutableString that may change. But I view it as the responsibility of the caller to specify an immutable string, not the object. – Steven Fisher Jun 11 '11 at 16:55