Re: newbie EXC_BAD_ACCESS
Re: newbie EXC_BAD_ACCESS
- Subject: Re: newbie EXC_BAD_ACCESS
- From: Hamish Allan <email@hidden>
- Date: Tue, 22 Mar 2005 00:35:12 +0000
1. I think the line
if ([scanner scanInt:&type] && [scanner scanUpToString:@"\n" intoString:&desc])
[self addStrokeDesc:[StrokeDescription strokeDescriptionWithType:type
desc: desc];
can be paraphrased as
if (you find an int AND you find a string in the first line)
Not just the first line, because of the while loop.
// i.e. if two YES values returned
place those values into whatever it is the type and desc pointers point to, and
use those values to initialize a StrokeDescription.
This means you are passing pointers without ever assigning names to those pointers. Aiming for less dense code (reflecting novice stage), would this work?
int *typePtr = &type;
NSString **descPtr = &desc;
if ([scanner scanInt: typePtr] && [scanner scanUpToString:@"\n" intoString:descPtr])
... the rest.
Yes, although I can't really conceive of how you could find this easier to understand (you still have to think about what &type and &desc mean when you're reading it, but you also have to think about two extra variable names and types).
2. You created a class method. Could I have pulled this off without doing that? e.g. something along the lines of....
int *typePtr = &type;
NSString **descPtr = &desc;
while (![scanner isAtEnd) {
sd = [[StrokeDescription alloc] init];
[scanner scanInt: typePtr]; // OBVIOUS FLAW OF NOT KNOWING BOOL RTN VAL
It takes more time to write "// OBVIOUS FLAW OF NOT KNOWING BOOL RTN VAL" at the end of this line than it would to write "BOOL foundType = " in front of it.
[scanner scanUpToString:@"\n" intoString:descPtr]; // SAME HERE
// IS THERE A WORKAROUND WITHOUT THE COMPLEX IF STATEMENT?
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?!
}
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.
I could explain why this caused the behaviour you describe, but I think it would be much more useful for you to try to explain it to me as an exercise in consolidating what we have already discussed. Hint: freed heap memory is available for reallocation.
Best wishes,
Hamish
_______________________________________________
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