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: Adam Raney <email@hidden>
- Date: Sat, 30 Jul 2005 18:51:16 -0500
On 7/30/05, Adam Raney <email@hidden> wrote:
> Alright, we've got some crossed wires here. Let's re-analyze the situation:
>
> I have an object of ACRQuizBuilderController, this was created in
> Interface Builder and instantiated. It has a method called
> addHeaderRow:, which is called when a button is pressed.
>
> addHeaderRow is designed like this:
>
> (IBAction)addHeaderRow:(id)sender
> {
> ACRQuizProblemRowElement * newElement = [[ACRQuizProblemRowElement
> alloc] init];
>
> [newElement setContent: [headerEntry stringValue]]; //header entry is
> an NSTextField IBOutlet
> [newElement setFormat: @"text"];
> [newElement setAnswers: [NSArray array]];
> [newElement setIsHeader: YES];
> }
>
>
> ACRQuizProblemRowElement is my custom class that contains a few
> strings, a BOOL, and an NSArray. The array is immutable, designed to
> be set through a setter method, and not have items added over time.
>
> Here's the code for this class:
>
> ACRQuizProblemRowElement.h
>
> #import <Cocoa/Cocoa.h>
>
>
> @interface ACRQuizProblemRowElement : NSObject
> {
> NSString * _format;
> NSString * _content;
> NSArray * _answers;
> BOOL _isHeader;
> }
>
> - (NSString *) format;
> - (NSString *) content;
> - (NSArray *) answers;
> - (BOOL) isHeader;
>
> - (void) setFormat: (NSString *)newFormat;
> - (void) setContent: (NSString *)newContent;
> - (void) setAnswers: (NSArray *)newAnswers;
> - (void) setIsHeader: (BOOL)newIsHeader;
>
> @end
>
> --------------------
>
> ACRQuizProblemRowElement.m
>
>
> #import "ACRQuizProblemRowElement.h"
>
>
> @implementation ACRQuizProblemRowElement
>
> #pragma mark -
> #pragma mark Creation and Destruction
>
> - (id) init
> {
> [super init];
> if (self) {
> _format = [NSString string];
> _content = [NSString string];
> _answers = [NSMutableArray array];
> _isHeader = NO;
> }
> return self;
> }
>
> - (void) dealloc
> {
> [_format release];
> [_content release];
> [_answers release];
>
> [super dealloc];
> }
>
>
> #pragma mark -
> #pragma mark Accessors
> #pragma mark Getters
>
> - (NSString *) format
> {
> return _format;
> }
>
> - (NSString *) content
> {
> return _content;
> }
>
> - (NSArray *) answers;
> {
> return _answers;
> }
>
> - (BOOL) isHeader;
> {
> return _isHeader;
> }
>
> #pragma mark Setters
>
> - (void) setFormat: (NSString *)newFormat
> {
> [_format release];
> _format = [newFormat copy];
> }
>
> - (void) setContent: (NSString *)newContent
> {
> [_content release];
> _content = [newContent copy];
> }
>
> - (void) setAnswers: (NSArray *)newAnswers
> {
> if (newAnswers != _answers) {
> [_answers autorelease];
> _answers = [newAnswers copy];
> }
> }
>
> - (void) setIsHeader: (BOOL)newIsHeader
> {
> _isHeader = newIsHeader;
> }
>
> @end
>
>
> --------------------------------------
>
> I do nothing else with newElement because I have yet to make this
> section work. I am building it in stages.
>
> When I remove the setAnswers: method call, the addHeaderRow: method
> completes with no problem. When the setAnswers: call included, the
> entire addHeaderRow: method completes (the setAnswers: works as
> expected), then I get the "EXC_BAD_ACCESS" error and the program
> crashes.
>
> As for the underscores, I was taking Carlos's advice:
>
> > 1. Point of style and clarity, prefix instance variables with an underscore.
>
> I can get AROUND this problem by making the array mutable, and using
> addObject: calls, but I have read that Mutable Arrays create more
> memory overhead and are less effecient.
>
_______________________________________________
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