Home > other >  AutoRelease object memory release problem
AutoRelease object memory release problem

Time:09-23

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- a delicious line -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
[cocos2dx] - I create a Sprite object aSprite (Sprite created is a autoRelease objects), and use it as the arguments passed to a function. In this function I retain aSprite first and then assigned to a class member variable bSprite, after bSprite addChild again to the a panel inside. I want to do now bSprite release?
1. The autoRelease objects in where? It is when and how to release (manual call release will affect it?)
2. This function after the operation, aSprite count of memory? When was it released recovery?
3. BSprite assigned aSprite addChild again after the count should be 3, after the function should be to 2 that this class destructor of time I need to take bSprite release twice can release bSprite real? But it is not the autoRelease? . Dizzy

CodePudding user response:

1, override dealloc function, object release automatically calls the function, the inside print a log to see
2, int I=[bSprite retainCount]; I=0 that had already released

CodePudding user response:

Another, if I retain an autoRelease object at a time, but I will release the 2 times this object, it will happen?

CodePudding user response:

refer to the second floor sinat_20998879 response:
one, if I retain an autoRelease object at a time, but I will release the two this object, it will happen?

Has released, object is nil Release again nothing happens

CodePudding user response:

reference 1st floor yuelengdihai response:
1, override dealloc function, object to release automatically calls the function, the inside print a log to see
2, int I=[bSprite retainCount]; I=0 that had already released
but it is not for 0..

CodePudding user response:

reference yuelengdihai reply: 3/f
Quote: refer to the second floor sinat_20998879 response:

Another, if I retain an autoRelease object at a time, but I will release the 2 times this object, it will happen?

Has released, object is nil Release again nothing happens the
is autoRelease objects in addition to retain or Release manually, other such as creating, added to the memory of the other parent panel count is automatically add and subtract?

CodePudding user response:

Sinat_20998879
reference 4 floor response:
Quote: refer to 1st floor yuelengdihai response:

1, override dealloc function, object release automatically calls the function, the inside print a log to see
2, int I=[bSprite retainCount]; I=0 that had already released
but it is not for 0..

Not 0 is you didn't release, or is a class member variables without bSprite didn't release, or a class member variable die didn't release

CodePudding user response:

refer to 6th floor yuelengdihai response:
Quote: refer to 4th floor sinat_20998879 response:

Quote: refer to 1st floor yuelengdihai response:

1, override dealloc function, object release automatically calls the function, the inside print a log to see
2, int I=[bSprite retainCount]; I=0 that had already released
but it is not for 0..

Not 0 is you didn't release, or is a class member variables without bSprite didn't release, or a class member variable die didn't release
but I am very confused, this is an autoRelease object,
 
Void TestAutoReleaseClass: : setBSprite (Sprite * aSprite)
{
ASprite - & gt; Retain ();
M_bSprite=aSprite;
If (aSprite) {
This - & gt; AddChild (m_bSprite, 0);

This - & gt; SetContentSize (m_bSprite - & gt; GetContentSize ());
}
}

When I was in the class destructor I release a m_bSprite, but release after it count is 1. Do I need to release again, or automatic memory pool will help me to empty, if it is, when the memory pool is clean?

CodePudding user response:

 
Void TestAutoReleaseClass: : setBSprite (Sprite * aSprite)
{
If (m_bSprite!=aSprite) {//write more stringent
M_bSprite=[aSprite retain];
}
If (aSprite) {
This - & gt; AddChild (m_bSprite, 0);

This - & gt; SetContentSize (m_bSprite - & gt; GetContentSize ());
}
}

CodePudding user response:


 
Void TestAutoReleaseClass: : setBSprite (Sprite * aSprite)
{
If (m_bSprite!=aSprite) {//write more stringent
[m_bSprite release];
M_bSprite=[aSprite retain];
}
If (aSprite) {
This - & gt; AddChild (m_bSprite, 0);

This - & gt; SetContentSize (m_bSprite - & gt; GetContentSize ());
}
}

CodePudding user response:

 
//the reference count
//retain ();//+ 1
//release ();//- 1
//autorelease ();//+ 1 and add objects to the reference counting pool (AutoreleasePool), the pool at the end of the current message loop, it will be a release of all objects call ();

Click on the button//assume that a certain function

CXXX: : onBtn (Ref * sender) {
Sprite * s=Sprite: : create (a. "pg");//the Node and its subclasses of the create function usually call autorelease ();//the _referenceCount=1;

//works as if what all don't, leave a message loop, the pool will call release ();//_referenceCount=0 and release the object at this time...
}


Click on the button//assume that another function

CXXX: : onBtn2 (Ref * sender) {
Sprite * s=Sprite: : create (a. "pg");//the Node and its subclasses of the create function usually call autorelease ();//the _referenceCount=1;

The parent - & gt; AddChild (s);//the Node and its subclasses of addChild () function arrowheads will call retain ();//the _referenceCount=2;

//leave a message loop, pool will call release ();//_referenceCount=1 and release the object at this time...
//no release objects. At this time unless you call
//parent - & gt; The removeChild (s);//the Node and its subclasses of removeChild () function arrowheads will call release ();//_referenceCount=0 and release the object at this time...
}

CodePudding user response:

Who who retain (), release ();
Autorelease by AutoreleasePool to release () ();

CodePudding user response:

//leave a message loop, pool will call release ();//_referenceCount=1 and release the object at this time...
This sentence should be
//leave a message loop, pool will call release (); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related