Re: Weird Fix for EXC_BAD_ACCESS
Re: Weird Fix for EXC_BAD_ACCESS
- Subject: Re: Weird Fix for EXC_BAD_ACCESS
- From: matt neuburg <email@hidden>
- Date: Fri, 25 Jul 2003 10:11:23 -0700
On 7/25/03 at roughly 11:41 AM, thus spake Duncan Oliver <email@hidden>:
>
> On Thu, 24 Jul 2003 12:56:24 -0500, Duncan Oliver
>
> <email@hidden>
>
> said:
>
>> I have a bit of code that tries to find the "owner" dictionary of an
>
>> object. Each item is a dictionary that consist of a NSString under key
>
>> "name" and an array of similar objects under key "contents" the code
>
>> is
>
>> suppose to look for the parent, starting from the top array,
>
>> collection:
>
>>
>
>> - (NSMutableDictionary *)searchDeepArrayOfItem:(NSMutableDictionary
>
>> *)scope forObject:(NSMutableDictionary *)query;
>
>> {
>
>> NSEnumerator *enumerator;
>
>> NSMutableDictionary *object, *result;
>
>> NSMutableArray *array;
>
>>
>
>> if (scope == nil)
>
>> array = collection;
>
>> else
>
>> array = [scope objectForKey:@"contents"];
>
>>
>
>> if ([array indexOfObject:query] == NSNotFound)
>
>> {
>
>> enumerator = [array objectEnumerator];
>
>> while (object = [enumerator nextObject])
>
>> {
>
>> result = [self searchDeepArrayOfItem:object
>
>> forObject:query];
>
>> if (result != nil)
>
>> break;
>
>> }
>
>> }
>
>> else
>
>> result = scope;
>
>>
>
>> return result;
>
>> }
>
>>
>
>> The problem is that I always get a "EXC_BAD_ACCESS", unless I change
>
>> this:
>
>>
>
>> result = [self searchDeepArrayOfItem:object forObject:query];
>
>> if (result != nil)
>
>> break;
>
>>
>
>> to this:
>
>>
>
>> NSLog(//any text will do here);
>
>> result = [self searchDeepArrayOfItem:object forObject:query];
>
>> if (result != nil)
>
>> break;
>
>>
>
>>
>
>> I can't figure out why adding an NSLog would allow the code to execute
>
>> properly. Any ideas?
>
>
>
>
On Thursday, July 24, 2003, at 09:54 PM, matt neuburg wrote:
>
>
> What happens if you try initializing result to nil at the start?
>
>
I took your advice and initialize result to nil, and lo and behold, it
>
didn't crash. If you know why that worked, I'd like to hear the reason.
>
Anything to help me learn what I was doing wrong.
I read your algorithm (above). I didn't have time to try it out myself, but just
reading it, it looked faulty to me; it appeared to me that either you didn't understand
how to recurse (it appeared to me that you had not covered every situation, so that
there were situations where "result" could be returned without ever being given a value
at all) or you didn't understand how initialization works in Objective-C (in
Objective-C, non-initialized local variables are not nil; they have an undefined value,
and since they are pointers this means they could be pointing anywhere). Thus I
reasoned that when setting result = [self searchDeepArrayOfItem:object forObject:query]
it might be the case that you were returning one of these undefined values. This would
fail the nil test in the next line, because it was NOT nil (nil is zero, not a random
undefined number). Therefore I made a suggestion whereby you would always at least be
returning nil, and not an undefined value. m.
--------
matt neuburg, phd = email@hidden,
http://www.tidbits.com/matt
pantes anthropoi tou eidenai oregontai phusei
Subscribe to TidBITS! It's free and smart.
http://www.tidbits.com/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.