• 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: What's wrong with my NSMutableArray?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: What's wrong with my NSMutableArray?


  • Subject: Re: What's wrong with my NSMutableArray?
  • From: Dave DeLong <email@hidden>
  • Date: Fri, 12 Jun 2009 07:43:10 -0700

You're not initing the NSMutableArray. You're using a convenience constructor, and it's returning an autoreleased object. What's probably happening is that the autoreleased object is getting deallocated via a popped autorelease pool. Try changing the line to be:

boxes = [[NSMutableArray alloc] initWithCapacity:NUM_BOXES];

This is a really important topic that will give you a lot of grief if you don't understand what's going on.

Also, you're leaking all your Box objects inside the for() loop, because you init them (retain count of +1), and put them into an array (retain count of +2), but you're not releasing the Box after adding it to the array (which is what you'd want to do 99.9% of the time).

Given that both the problems seem to be memory related, I suggest that you take the time to study this document until you find yourself reciting memory management rules in you sleep. =) http://developer.apple.com/documentation/cocoa/conceptual/memorymgmt/MemoryMgmt.html

There are also a *ton* of websites out there that will explain this:  http://lmgtfy.com/?q=cocoa+memory+management

Cheers!

Dave DeLong

On Jun 12, 2009, at 7:17 AM, Allan Greenier wrote:

This is so simple I know the answer will embarrass me.
This is an iPhone app.
I've got an NSMutable array declared in my EAGLview.h file

NSMutableArray *boxes;

I init and fill it in the EAGLview's initWithCoder method

		boxes = [NSMutableArray arrayWithCapacity:NUM_BOXES];

		for(i = 0; i < NUM_BOXES; i++)
	{
		Box *abox = [[Box alloc] init];
		[boxes addObject: abox];
	}				

In EAGLview dealloc I

	boxes = nil;

Later on in program flow, in EAGLview, if I try to access boxes in anyway, I crash into the debugger

[boxes objectAtIndex: 0];

or even

NSLog(@"mode5 boxes  %@",  boxes);

will crash

What am I missing? What's wrong with my NSMutableArray?
My only guess is it's in my for loop, I need to release abox?


Thanks, Allan



_______________________________________________

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


References: 
 >What's wrong with my NSMutableArray? (From: Allan Greenier <email@hidden>)

  • Prev by Date: What's wrong with my NSMutableArray
  • Next by Date: Re: What's wrong with my NSMutableArray?
  • Previous by thread: What's wrong with my NSMutableArray?
  • Next by thread: Re: What's wrong with my NSMutableArray?
  • Index(es):
    • Date
    • Thread