tags:

views:

88

answers:

3

Hi,

I am a newbie, writing a simple program and while there are no warnings/errors during compilation. I am getting a "EXC_BAD_ACCESS" error. Will appreciate any help with this:

    int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

        moviedatabase *movie1=[[moviedatabase alloc] init];

    [movie1 addMovieWithName:@"DDLJ" andyear:1789 andlength:360 andGenre:Horror];
    [movie1 printAll];
    [movie1 release];

    [pool drain];

    return 0;
}

Here is the moviesdatabase class and movies class which it is inheriting:

@interface moviedatabase :  movies{
//no variables in the class
}

-(void) addMovieWithName: (NSString *)mname andyear: (int) myear andlength: (int) mlength andGenre: (enum Genre) mgenre; 
-(void) printAll;

@end

@interface movies : NSObject {
NSString    *name;
int year;
int length;
enum Genre {Comedy,Drama,Horror,Action} genre;
}

@property (nonatomic) NSString *name;
@property (nonatomic) int year;
@property (nonatomic) int length;
@property (nonatomic) enum Genre genre;

-(id) initWithName: (NSString *)name andyear: (int) year andlength: (int) length andGenre: (enum Genre) genre; 

@end

Including the implementation of moviedatabase:

 #import "moviedatabase.h"


 @implementation moviedatabase

 -(void) addMovieWithName: (NSString *) mname andyear: (int) myear andlength: (int) mlength andGenre: (enum Genre) mgenre
   {
         name=mname;
  year=myear;
  length=mlength;
  genre=mgenre;

  }

-(void) printAll;
 {
  NSLog(@"name=%@, year=%@, length=%@, genre=%@",name,year,length,genre);
 }

 @end
+1  A: 

Have you looked at: http://stackoverflow.com/questions/327082/exc-bad-access-signal-received ?

This document was particularly helpful for me:

http://www.cocoadev.com/index.pl?DebuggingAutorelease

smtlaissezfaire
yeah, i have searched and looked for it but still can't figure out :-(
Vatsal Bhardwaj
I'm assuming you are doing this in xcode? If so, I'd say you should put it in debug mode, let the thing crash, and then get a stack trace from GDB. (hit "bt" for backtrace).
smtlaissezfaire
A: 

It's working now. Thanks to JAcob for helping isolate. The error was in printAll function :)

Vatsal Bhardwaj
You should accept the correct answer
JeremyP
+4  A: 

Your printAll method uses %@ instead of %i for int variables.

This:

- (void)printAll
{
    NSLog(@"name=%@, year=%@, length=%@, genre=%@", name, year, length, genre);
}

Should look like this:

- (void)printAll
{
    NSLog(@"name=%@, year=%i, length=%i, genre=%i", name, year, length, genre);
}

In NSLog(), %@ is used for NSObjects, and standard printf format strings are used for other C primitives, e.g. %i for int, %u for unsigned int, %f for float.

Nick Forge
Thanks everyone!
Vatsal Bhardwaj