• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: newbie EXC_BAD_ACCESS
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: newbie EXC_BAD_ACCESS


  • Subject: Re: newbie EXC_BAD_ACCESS
  • From: Daniel Child <email@hidden>
  • Date: Mon, 21 Mar 2005 15:56:58 -1000


On Monday, March 21, 2005, at 02:35 PM, Hamish Allan wrote:

NSString **descPtr = &desc;
while (![scanner isAtEnd) {
sd = [[StrokeDescription alloc] init];
BOOL foundType = [scanner scanInt: typePtr];

BOOL foundDesc = [scanner scanUpToString:@"\n" intoString:descPtr];

if (foundType && foundDesc).

[sd setStrokeType: type];
[sd setStrokeDesc: desc];
[self addStrokeDesc: sd]; // to add to knownStrokes array
// where addStrokeDesc retains the passed parameter sd
[sd release];

Aargh! Why have you changed the autorelease I handed you on a plate, into a release?!

I am trying to accomplish the same thing using the private accessors I originally had. Here's how I think it works....

while looping in progress
allocate memory for sd (sd retainCount = 1)
scan the info
set the instance variables type and desc
add sd to knownStrokes (via the addStrokeDesc method)
(sd retainCount = 2)
release sd (retainCount = 1)

My biggest surprise in your solution was the use of + for a class method. Kochan doesn't seem to do that much in his examples, so it never occurred to me to try. Maybe the real question I need to ask is:

- When should I go for a class method as opposed to instance method? In other words, is there some significant difference between your approach:

+ (int)type desc:(NSString *)desc
..... return [sd autorelease]

and something like

- (id)initStrokeDescriptionWithType:(int)type desc:(NSString *)desc
..... return [self autorelease] // or just return self ???

The calls would look different, but I don't see pluses or minuses either way. When to use one over the other?

I guess this relates to my earlier question: the articles repeatedly explain when to use release/autorelease (when you have used alloc/init/copy), but they don't explain when it is preferable to use alloc/init over some other approach, or in this case, a class vs instance method (when both could work).

In other words, without using a public constructor and sticking with plain old instance accessors, can you create the sd's and then place them in the array? Once sigdev error was solved, I was having troubles because the knownStrokes array was getting the last copy of sd created placed in every single slot of the array.

That's because you changed the autorelease to a release.
 _______________________________________________
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

  • Follow-Ups:
    • Re: newbie EXC_BAD_ACCESS
      • From: Hamish Allan <email@hidden>
References: 
 >Re: newbie EXC_BAD_ACCESS (From: Hamish Allan <email@hidden>)

  • Prev by Date: Re: A 'small' NSMenu
  • Next by Date: Re: newbie EXC_BAD_ACCESS
  • Previous by thread: Re: newbie EXC_BAD_ACCESS
  • Next by thread: Re: newbie EXC_BAD_ACCESS
  • Index(es):
    • Date
    • Thread