Re: EXC_BAD_ACCESS on returning an int?
Re: EXC_BAD_ACCESS on returning an int?
- Subject: Re: EXC_BAD_ACCESS on returning an int?
- From: Andrew Merenbach <email@hidden>
- Date: Sat, 4 Jul 2009 23:39:30 -0700
Hi, Ian,
From a cursory glance, the issue would likely be that you're using %@
in your NSLog statements, rather than %i or %d. %@ is used for
NSObjects, generally NSStrings. You'll want to replace that with %i
or %d since your -numberOfSides and other methods are returning int
values. That should clear up the problems.
Cheers,
Andrew
On Jul 4, 2009, at 5:39 PM, Ian Havelock wrote:
Hi All,
I've just started to learn my way around Cocoa but I've come across
an error message that I don't understand and googling hasn't really
shed any light on the subject.
I have defined a simple polygon class with 3 int's, called
PolygonShape. When debugging I can see that I am setting the
properties correctly, but when I try to output the object's
variables using NSLog I receive an EXC_BAD_ACCESS error?
When it hits the NSLog line in 'PolygonShape Testing.m', it goes
through (from the right) returning maximumNumberOfSides,
minimumNumberOfSides and numberOfSides, but when it hits the
closing } after return numberOfSides I get the EXC_BAD_ACCESS error?
As I'm using int's and not string Objects such as NSString I don't
see how it could be memory management related unless there is
something I'm missing?
Running…
Program received signal: “EXC_BAD_ACCESS”.
(gdb)
I apologise for posting such a lengthy email.
// PolygonShape.h
#import <Cocoa/Cocoa.h>
@interface PolygonShape : NSObject
{
int numberOfSides;
int minimumNumberOfSides;
int maximumNumberOfSides;
}
@property int numberOfSides;
@property int minimumNumberOfSides;
@property int maximumNumberOfSides;
- (int)numberOfSides;
- (void)setNumberOfSides:(int)numberOfSides;
- (int)minimumNumberOfSides;
- (void)setMinimumNumberOfSides:(int)minimumNumberOfSides;
- (int)maximumNumberOfSides;
- (void)setMaximumNumberOfSides:(int)maximumNumberOfSides;
@end
// PolygonShape.m
#import "PolygonShape.h"
@implementation PolygonShape
@synthesize numberOfSides;
@synthesize minimumNumberOfSides;
@synthesize maximumNumberOfSides;
- (int)numberOfSides {
return numberOfSides;
}
- (void)setNumberOfSides:(int)value {
numberOfSides = value;
}
- (int)minimumNumberOfSides {
return minimumNumberOfSides;
}
- (void)setMinimumNumberOfSides:(int)value {
minimumNumberOfSides = value;
}
- (int)maximumNumberOfSides {
return maximumNumberOfSides;
}
- (void)setMaximumNumberOfSides:(int)value {
maximumNumberOfSides = value;
}
@end
// PolygonShape Testing.m
#import <Foundation/Foundation.h>
#import "PolygonShape.h"
void PrintPolygonInfo()
{
PolygonShape *poly1 = [[PolygonShape alloc] init];
[poly1 setMinimumNumberOfSides:3];
[poly1 setMaximumNumberOfSides:12];
[poly1 setNumberOfSides:6];
NSLog(@"Number of Sides: %@, Min: %@, Max: %@", [poly1
numberOfSides], [poly1 minimumNumberOfSides], [poly1
maximumNumberOfSides]);
}
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
PrintPolygonInfo();
[pool drain];
return 0;
}
Many Thanks,
Ian_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden