Re: Crash when calling custom methods in custom subclass.
Re: Crash when calling custom methods in custom subclass.
- Subject: Re: Crash when calling custom methods in custom subclass.
- From: Charilaos Skiadas <email@hidden>
- Date: Thu, 28 Jul 2005 22:14:01 -0500
On Jul 28, 2005, at 9:34 PM, Adam Raney wrote:
ACRQuizProblemRowElement.h :
#import <Cocoa/Cocoa.h>
@interface ACRQuizProblemRowElement : NSObject
{
NSString * format;
NSString * content;
NSArray * answers; // to be filled with NSStrings
bool isHeader;
}
- (void) setFormat: (NSString *)newFormat;
- (void) setContent: (NSString *)newContent;
- (void) setAnswers: (NSArray *)newAnswers;
- (void) setIsHeader: (bool)newIsHeader;
@end
---------
ACRQuizProblemRowElement.m
...
- (void) setFormat: (NSString *)newFormat
{
format = [NSString stringWithString: newFormat];
}
All your accessors are wrong. Read up on setters and getters, and the
correct way to write them, and also on memory management. In this
particular case, stringWithString returns an autoreleased object,
which will be deallocated in all likelihood soon after the method
exits, and then "format" will point to garbage. Same goes for all
the other methods.
I would suggest, if you haven't done so yet, going through a book on
Cocoa programming, and getting the basics of Obj-C down right. My
personal favorite, and a lot of people will agree on that, is Aaron
Hillegass's book. It will make things immensely easier and more clear.
- (void) setContent: (NSString *)newContent
{
content = [NSString stringWithString: newContent];
}
- (void) setAnswers: (NSArray *)newAnswers
{
answers = [NSArray arrayWithArray: newAnswers];
}
- (void) setIsHeader: (bool)newIsHeader
{
isHeader = newIsHeader;
}
Haris
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden