Re: Accessing a managedObject property from within an accessor of another property
Re: Accessing a managedObject property from within an accessor of another property
- Subject: Re: Accessing a managedObject property from within an accessor of another property
- From: Quincey Morris <email@hidden>
- Date: Tue, 22 Feb 2011 09:44:16 -0800
On Feb 22, 2011, at 06:19, Brad Stone wrote:
> I've been trying for days to determine why I get EXC_BAD_ACCESS when I try to access a managedObject property from within an accessor of another property?
>
> this code in main.m
> [[self note] setValue:@"HELLO WORLD" forKey:@"category"];
> NSNumber *tmpVal = [NSNumber numberWithBool:![[[self note] valueForKey:@"isEncrypted"] boolValue]]; // if I type po [self note] I see the note description
> [[self note] setValue:tmpVal forKey:@"isEncrypted"]; // if I type po [self note] after this I get EXEC_BAD_ACCESS
>
> The if statement in the accessor is causing the problem:
> - (NSString *)category {
> NSString * tmpValue;
>
> [self willAccessValueForKey:@"category"];
> tmpValue = [self primitiveCategory];
> [self didAccessValueForKey:@"category"];
>
> if ([[self valueForKey:@"isEncrypted"] boolValue]) { // THIS IS CAUSING THE PROBLEM
> // code to decrypt tmpValue
> }
>
> return tmpValue;
> }
No answer, but a couple of points:
-- What happens if you write 'note.isEncrypted' (or '[note isEncrypted]') instead of '[note valueForKey: @"isEncrypted"]'?
-- What happens if you code your own custom accessors for "isEncrypted", like you've done for "category"?
-- EXC_BAD_ACCESS is often a symptom of a memory management problem. Is there a problem with the object being returned by '[self note]'? You didn't show code for where this is implemented. It's possible that you failed to retain the object, or, if you're using garbage collection, returned an object with no strong references, or perhaps you returned a zombie object.
What happens in the above code if you set first a local variable to '[[self note] retain]' and use the local variable in place of the other references to '[self note]'?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden