cocos2d version2.0 singleton invalid -


i use cocos2d version2.0 build game, not use arc, find problem: singleton invalid.

i use 2 singleton macro, problem same.

#ifndef singleton_interface #define singleton_interface(classname)                  \     +(classname*) sharedmanager; #endif  #ifndef singleton_implementation #define singleton_implementation(classname)             \                                                         \ +(classname*) sharedmanager {                           \     static dispatch_once_t pred;                        \     static classname* shared##classname = nil;          \     dispatch_once( &pred, ^{                            \     shared##classname = [[self alloc] init]; });        \     return shared##classname;                           \ }                                                            #endif 

or

#ifndef singleton_interface #define singleton_interface(classname)              \     +(classname*) sharedmanager; #endif  #ifndef singleton_implementation             #define singleton_implementation(classname)         \ \ static classname* shared##classname = nil;          \ \ +(classname*) sharedmanager                         \ {                                                   \     if (shared##classname != nil) {                 \         return shared##classname;                   \     }                                               \ \     @synchronized(self) {                           \         if (shared##classname == nil) {             \             shared##classname = [[self alloc] init];\         }                                           \     }                                               \ \     return shared##classname;                       \ }                                                   \ \ +(id) allocwithzone:(nszone*)zone                   \ {                                                   \     @synchronized(self) {                           \         if (shared##classname == nil) {             \             shared##classname = [super allocwithzone:zone]; \             return shared##classname;               \         }                                           \     }                                               \     nsassert(no, @ "[" #classname " alloc] explicitly called on singleton class.");   \     return nil;                                     \ }                                                   \ \ -(id) copywithzone:(nszone*)zone                    \ {                                                   \     return self;                                    \ }                                                   \ \ -(id) retain                                        \ {                                                   \     return self;                                    \ }                                                   \ \ -(unsigned) retaincount                             \ {                                                   \     return uint_max;                                \ }                                                   \ \ -(oneway void) release                              \ {                                                   \ }                                                   \ \ -(id) autorelease                                   \ {                                                   \     return self;                                    \ } #endif 

use:

[[ccdirector shareddirector] replacescene:[menuscene sharedmanager]]; 

the menuscene show again, buttons(ccmenu) or other elements(ccnode) not have response. mean first call, scene ok, when call again, scene show menu or other elements not have response. in cocos2d v1.0 running good.

//////////////////////////////////////////////////////////////////

i override "cleanup" without "[super cleanup]", this:

-(void) dealloc {        [super cleanup];     [super dealloc]; }  -(void) cleanup  {     // } 

now, scene running singleton ok, safely?

the problem lies ccdirector performing "cleanup" operations on scene's children.

cocos2d v2.0 adds use of blocks in several cases, released during "cleanup" happens regardless of whether singleton stays in-memory.

from v2.0 migration guide:

setting cleanup flag yes might cause side-effects in application's logic still making implicit references freed resources. ccmenuitems, use blocks internally launch actions, might giving problems , not launching action, assuming have removed item parent node cleanup flag activated , added back, 1 of telling signs. changing flag no should enough.

migration guide: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:migrate_to_v2.0#ccsprite


offhand, suggest either:

  • find way safely prevent "cleanup" being called on singleton's children.
  • or: keep in memory children of cclayer show/hide instead of using scene mechanism.
  • or: not use singleton , recreate scene each time.

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -