• 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: [Newbie] Proper class releasing
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [Newbie] Proper class releasing


  • Subject: RE: [Newbie] Proper class releasing
  • From: "Jon Keon" <email@hidden>
  • Date: Fri, 6 Jun 2008 13:59:25 -0400
  • Thread-topic: [Newbie] Proper class releasing

Hi Vijay,



Thanks for the response.



The line [newBaddie attachManagerRef:self]; is passing itself (the
baddie manager) to the Baddie instance so that when the Baddie has
expired it can call [managerRef removeMe:self] which tells the manager
to remove the Baddie from its array.



I'm gathering this is the wrong way to do things.



Am I correct is saying that because I passed the manager to the Baddie,
the Baddie now has a pointer to the manager. So when the Baddie
deallocs, it tries to release the manager as well?





Thanks,

Jon



From: Vijay Malhan [mailto:email@hidden]
Sent: Friday, June 06, 2008 1:48 PM
To: Jon Keon
Cc: Cocoa Developer List
Subject: Re: [Newbie] Proper class releasing





On Fri, Jun 6, 2008 at 9:41 PM, Jon Keon <email@hidden>
wrote:


Hi All,

I'm having a problem with creating new instances of classes after other
instances of them have been released.

I have a class called BaddieManager.

In this class I have a NSMutableArray that holds instances of Baddies.

So I create a new instance, place it my array and when the Baddies life
expires, I remove it from the array. This then causes the instance to
dealloc but then the next time I call my populateBaddies function the
whole program crashes with no error message. Any help would be greatly
appreciated.

Thanks,
Jon

//populate the array with baddies
-(void) populateBaddies {

       while (baddieCount < baddieLimit) {


               //Create a new instance of my baddie
               PBaddie *newBaddie = [[PBaddie alloc] init];
               //add him to the view
               [gameView addSubview:newBaddie];
               //give him a reference of the manager
               [newBaddie attachManagerRef:self];



What is the above method doing? I hope it is not retaining or releasing
the reference it is keeping in it's dealloc method. That will cause a
crash when next time this obj is accessed after releasing your *baddie
object*. Normally to maintain a weak reference delegate pattern is used,
where delegate is never retained or released.



If you are doing that right, see if you get a crash log/stack trace or
any logs in sys-console.



- Vijay




	               //animate him
	               [newBaddie animateBaddie:NULL finished:0];
	               //add him to the array
	               [pBaddieArray addObject:newBaddie];
	               //release the temp variable
	               [newBaddie release];
	               //increment the count
	               baddieCount = [pBaddieArray count];

	       }

	}

	//Removes the Baddie from the array and destructs him
	-(void) removeMe:(PBaddie *) pBad {

	       //remove from the game view - we don't want to see it
anymore
	       [pBad removeFromSuperview];

	       //remove the baddie from the array (Causes the Baddies
dealloc
	method to fire)
	       [pBaddieArray removeObject:pBad];
	       //update the count
	       baddieCount = [pBaddieArray count];

	}
	_______________________________________________

	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:

m

	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

  • Follow-Ups:
    • Re: [Newbie] Proper class releasing
      • From: "Vijay Malhan" <email@hidden>
References: 
 >detect URL change (From: "Mauricio Camayo" <email@hidden>)
 >Re: detect URL change (From: Scott Anguish <email@hidden>)
 >Re: detect URL change (From: Adam Leonard <email@hidden>)
 >Re: detect URL change (From: Jens Alfke <email@hidden>)
 >[Newbie] Proper class releasing (From: "Jon Keon" <email@hidden>)
 >Re: [Newbie] Proper class releasing (From: "Vijay Malhan" <email@hidden>)

  • Prev by Date: OK on Enter, not Return
  • Next by Date: Re: [Newbie] Proper class releasing
  • Previous by thread: Re: [Newbie] Proper class releasing
  • Next by thread: Re: [Newbie] Proper class releasing
  • Index(es):
    • Date
    • Thread