Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSMutableArray crashes when adding after proper initialization

I have an NSMutableArray defined as a property, synthesized and I have assigned a newly created instance of an NSMutableArray. But after this my application always crashes whenever I try adding an object to the NSMutableArray.

Page.h

@interface Page : NSObject  
{  
    NSString *name;  
    UIImage *image;  
    NSMutableArray *questions;  
}
@property (nonatomic, copy) NSString *name;  
@property (nonatomic, retain) UIImage *image;  
@property (nonatomic, copy) NSMutableArray *questions;  
@end

Page.m

@implementation Page  
@synthesize name, image, questions;  
@end  

Relevant code

Page *testPage = [[Page alloc] init];  
testPage.image = [UIImage imageNamed:@"Cooperatief  leren Veenman-11.jpg"];  
testPage.name = [NSString stringWithString:@"Cooperatief  leren Veenman-11.jpg"];  
testPage.questions = [[NSMutableArray alloc] init];  
[testPage.questions addObject:[NSNumber numberWithFloat:arc4random()]];  

The debugger reveals that the moment I use testPage.questions = [[NSMutableArray alloc] init]; the type of testPage.questions changes from NSMutableArray* to __NSArrayL* (or __NSArrayI*, not sure). I suspect this to be the problem, but I find it extremely odd. Anyone know what's happening here?

like image 283
SpacyRicochet Avatar asked May 23 '26 18:05

SpacyRicochet


2 Answers

The problem is that you've declared the property as copy. This means your setter is going to be implemented something like this:

- (void) setQuestions:(NSMutableArray *)array {
  if (array != questions) {
    [questions release];
    questions = [array copy];
  }
}

The kicker here is that if you -copy an array (whether immutable or mutable), you will always get an immutable NSArray.

So to fix this, change the property to be retain instead of copy, and also fix this memory leak:

testPage.questions = [[NSMutableArray alloc] init];

It should be:

testPage.questions = [NSMutableArray array];
like image 81
Dave DeLong Avatar answered May 25 '26 06:05

Dave DeLong


@property (nonatomic, copy) This setter declaration "copy" probably cast to NSArray why not retain or assign? I would retain anyway

like image 38
Michal Avatar answered May 25 '26 06:05

Michal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!