• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSArray bad access?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

References: 
 >NSArray bad access? (From: Philippe Hausler <email@hidden>)

  • Prev by Date: NSArray bad access?
  • Next by Date: Re: NSArray bad access?
  • Previous by thread: NSArray bad access?
  • Next by thread: Re: NSArray bad access?
  • Index(es):
    • Date
    • Thread