Re: NSArray bad access?
Re: NSArray bad access?
- Subject: Re: NSArray bad access?
- From: Christophe Dore <email@hidden>
- Date: Mon, 17 Jun 2002 09:50:21 +0200
The problem may be :
s = [NSMutableArray arrayWithObject:@"test"];
You don't own (retain) the array . It will disappear without warning you.
You should either retain it (use the retain method), either use an
init... method.
See all the stuff about retain.release/autorelease things
Philippe Hausler wrote:
ive got an object that on init it creates an array and stores it, then
later on ive got a function in that object to grab the count from the
array...
heres some code
-------------MyObject.h------------------------------------------------------------------------------------------------------------------------------------------
#import <Foundation/Foundation.h>
@interface MyObject : NSObject {
NSMutableArray *s;
}
- (unsigned)count;
@end
-------------MyObject.m------------------------------------------------------------------------------------------------------------------------------------------
#import "MyObject.h"
@implementation MyObject
- (id)init
{
s = [NSMutableArray arrayWithObject:@"test"];
return self;
}
- (unsigned)count
{
return [s count];
}
@end
-------------MyMainObject.h------------------------------------------------------------------------------------------------------------------------------------------
#import <Cocoa/Cocoa.h>
#import "MyObject.h"
MyObject *o; /*for the real application im going to use this
methodology in it has to be here, there is no other way*/
@interface MyMainObject : NSObject
{
}
- (IBAction)countIt:(id)sender;
@end
-------------MyMainObject.m------------------------------------------------------------------------------------------------------------------------------------------
@implementation MyMainObject
- (id)init
{
o = [[MyObject alloc] init];
return self;
}
- (IBAction)countIt:(id)sender
{
printf("%d", [o count]);
}
@end
------------------------------------------------------------------------------------------------------------------------------------------
What am I doing wrong, its more than likely a stupid human error, but
the application returns EXC_BAD_ACCESS, implying the variable hasnt
been initialized.... but it has in the init of its owning object...
_______________________________________________
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.
_______________________________________________
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.