memoryobjectnsmutablearraynscopyingcopywithzone

Problems with getting and changing NSMutableArray objects


I have a NSMutableArray called organisations which is an array of custom objects of a class I subclassed from NSObject, this subclass is called NSXOrganisation. This subclass DOES implement NSCopy like so:

@interface NSXOrganisation : NSObject <NSCopying>

...

- (id)copyWithZone:(NSZone *)zone
{
    id copy = [[[self class] alloc] init];

    if (copy) 
    {
        [copy setPropertyOne:[[[self getPropertyOne] copyWithZone:zone] autorelease]];           
    }

    return copy;
}

Now, I alloc & init the array like so:

organisations = [[NSMutableArray alloc] initWithArray:organisationsArray copyItems:YES];

This all works fine. For the sake of testing the array has 2 objects in it.

Next, when a method is called, I update an object in the array like so:

- (void)updateOrganisation
{
    [[organisations objectAtIndex:0] setConsumerKey:[tfPropertyOne stringValue]];
}

Where tfPropertyOne* is a NSTextField on the display (binded properly etc). This also works fine, as I have tested with NSLog and it outputs the correct result. I then call a display method on a different index in the array to view its relevant details:

- (void)displayOrganisation:
{
    [tfPropertyOne setStringValue:[[organisations objectAtIndex:index] getPropertyOne]];
}

index is a local NSInteger and is not the problem. I then call the same update method on this object, which apparently works fine also (using NSLog to test).

Finally, when I make a call to go back and display the first object in the array [self displayOrganisation:0] the call works successfully, but my app crashes when calling getPropertyOne.

This is the error in the Mac Console:

...: Invalid memory access of location 0x3bcfd069 eip=0x90c1cd5c

And this is from the crash report:

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libobjc.A.dylib                 0x90c1cd4b objc_msgSend + 27
1   com.apple.Foundation            0x943da945 _NSDescriptionWithLocaleFunc + 57
2   com.apple.CoreFoundation        0x906503ae __CFStringAppendFormatCore + 12238
3   com.apple.CoreFoundation        0x906a70dc _CFStringCreateWithFormatAndArgumentsAux + 108
4   com.apple.CoreFoundation        0x9072821c _CFLogvEx + 124
5   com.apple.Foundation            0x9445d221 NSLogv + 136
6   com.apple.Foundation            0x9445d194 NSLog + 27
7   co.nz.fdl.XeroConnect           0x1b5b4872 -[PrefsController displayOrganisation:] + 194 (PrefsController.mm:229)

Can anyone help? Am I accessing / creating / storing the objects into the array incorrectly?


Solution

  • I managed to fix this problem by changing my header file for the custom object.

    Original:

    @property (nonatomic, retain) NSString *propertyOne;
    

    Working:

    @property (copy) NSString *propertyOne;
    

    This whole issue was generally down to my lack of Objective-C knowledge (hence why I'm still learning!). Thanks guys