Re: noobie mem management problem
Re: noobie mem management problem
- Subject: Re: noobie mem management problem
- From: James Cicenia <email@hidden>
- Date: Thu, 26 Feb 2009 09:20:42 -0600
Peter -
Thank you that does clear up a lot. And now it works beautifully on my
iphone in simulator mode.
However, when I ad-hoc it to my phone... it doesn't work. I get this
in my log:
11 Foundation 0x306cab10
_NSArrayRaiseInsertNilException + 48
12 Foundation 0x3066f8ec -[NSCFArray
insertObject:atIndex:] + 152
13 Foundation 0x3066f844 -[NSCFArray addObject:]
+ 30
14 WhatsFresh 0x0000c796 -[Sqlite executeQuery:]
(Sqlite.m:122)
15 WhatsFresh 0x00004946 -[WhatsFreshAppDelegate
statesForItemAndMonthAndFresh:month:fresh:]
So is there something "different" that I have to do for the iphone
itself?
Thanks
James
On Feb 26, 2009, at 8:10 AM, Peter N Lewis wrote:
At 6:50 -0600 26/2/09, James Cicenia wrote:
In .h :
NSMutableArray *currentStates;
@property (nonatomic, copy) NSMutableArray *currentStates;
Now in my appdelegate I have:
@synthesize currentStates;
then in a method:
while (sqlite3_step(statement) == SQLITE_ROW) {
char *str = (char *)sqlite3_column_text(statement, 0);
[self.currentStates addObject:(str) ? [NSString
stringWithUTF8String:str] : @""];
}
However currentStates never gets populated and always has zero
objects.
What am I doing wrong. And, yes str has data.
Where is currentStates actually allocated?
Initially all ivars are set to nil (or 0 or whatever). So unless
you've set it somewhere, self.currentStates will return nil, and in
Objective C, you can send a message to nil and it is simply a NoOp
(ie, you wont get any error or exception).
currentStates needs to be allocated.
The next issue (and this is almost certainly where your problem is)
is that your @property is set to copy, so:
[self.currentStates addObject:@"hello"];
is equivalent:
[[self currentStates] addObject:@"hello"];
And [self currentStates] returns a copy of the NSMutableArray. And
so you create a copy, then you add the string, then it is
autoreleased.
So the issue is that the property specified "copy", which I don't
think actually makes sense for a NSMutableArray (besides
[NSMutableArray copy] actually returns an immutable NSArray (is
there any way to specify mutableCopy in an @property?)).
So you probably need to change the @property from copy to retain.
Alternatively, use the property without the setter/getter reference,
ie:
[currentStates addObject:@"hello"];
I hope that clears up more than it confuses,
Peter.
@interface TestObject : NSObject {
NSMutableArray *currentStates;
}
@property (nonatomic, retain) NSMutableArray *currentStates;
@end
@implementation TestObject
@synthesize currentStates;
- (id) init
{
self = [super init];
if (self != nil) {
self.currentStates = [NSMutableArray array];
}
return self;
}
- (void) Test;
{
[self.currentStates addObject:[NSString
stringWithUTF8String:"Hello"]];
}
- (void) dealloc
{
[currentStates release];
[super dealloc];
}
@end
int main(int argc, char *argv[])
{
NSLog( @"main" );
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
TestObject* obj = [[TestObject alloc] init];
[obj Test];
NSLog( @"%@", obj.currentStates );
[pool release];
// return NSApplicationMain(argc, (const char **) argv);
}
--
Run macros from your iPhone with Keyboard Maestro Control!
or take a break with Aragom Space War for your iPhone
Keyboard Maestro <http://www.keyboardmaestro.com/> Macros for your Mac
Aragom Space War <http://www.stairways.com/iphone/aragom> Don't get
killed!
<http://www.stairways.com/> <http://download.stairways.com/>
_______________________________________________
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
_______________________________________________
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