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: "Marcus S. Zarra" <email@hidden>
- Date: Fri, 29 Jul 2005 15:16:34 -0600
The previous reply is correct and he did try and point you in the right
direction. The issue is with your setters and memory management. Basically
you have no memory management in your setters and that is causing your
problems.
In your setters, depending on your situation (there are multiple solutions),
you need to release your previous reference to a variable and then retain
the new one. You do not need to call stringWithString or arrayWithArray.
There is an excellent article over at Stepwise that I would strongly suggest
you review regarding this exact subject:
http://www.stepwise.com/Articles/Technical/HoldMe.html
On 7/29/05, Adam Raney <email@hidden> wrote:
>
> Unfortunately, I have tried learning from books, but find that I
> learn much better by analyzing examples. While I will look for the
> book you've suggested, if anyone could point out what I've done
> wrong, and perhaps provide me with an example of a proper setter, I'd
> greatly appreciate it.
>
> On Jul 28, 2005, at 10:14 PM, Charilaos Skiadas wrote:
>
> > 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
>
_______________________________________________
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