Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG - Was mutated while being enumerated exception while adding a child. #908

Open
vladubogdan opened this issue Jul 30, 2014 · 3 comments
Labels

Comments

@vladubogdan
Copy link

If you execute the following code

-(void)mouseDown:(NSEvent *)theEvent{

    NSPoint location = [theEvent locationInNode:self];

    CCSprite* parentSpr = [CCSprite spriteWithImageNamed:@"test.png"];
    if(parentSpr){
        NSLog(@"ADDING PARENT SPR %p", parentSpr);
        [parentSpr setPosition:location];

        CCSprite* newChildSpr = [CCSprite spriteWithImageNamed:@"test.png"];
        [newChildSpr setPosition:CGPointMake(30, 0)];
        NSLog(@"ADD CHILD %p", newChildSpr);
        [parentSpr addChild:newChildSpr];

        [self addChild:parentSpr];
    }

    [super mouseDown:theEvent];
}

Eventually you will get this exception

An uncaught exception was raised
2014-07-30 15:13:11.503 Cocos2d31ExceptionTest[19573:8403] *** Collection <__NSArrayM: 0x610000046cc0> was mutated while being enumerated.
2014-07-30 15:13:11.503 Cocos2d31ExceptionTest[19573:8403] (
    0   CoreFoundation                      0x00007fff8a12925c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff8d41ee75 objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff8a128b64 __NSFastEnumerationMutationHandler + 164
    3   Cocos2d31ExceptionTest              0x000000010008c377 -[CCNode visit:parentTransform:] + 407
    4   Cocos2d31ExceptionTest              0x000000010008c8cc -[CCNode visit] + 508
    5   Cocos2d31ExceptionTest              0x0000000100054ef3 -[CCDirectorDisplayLink drawScene] + 755
    6   Cocos2d31ExceptionTest              0x00000001000546bc -[CCDirectorDisplayLink getFrameForTime:] + 188
    7   Cocos2d31ExceptionTest              0x00000001000549a7 MyDisplayLinkCallback + 55
    8   CoreVideo                           0x00007fff88ee75cc _ZN13CVDisplayLink9performIOEP11CVTimeStamp + 206
    9   CoreVideo                           0x00007fff88ee6a08 _ZN13CVDisplayLink11runIOThreadEv + 608
    10  CoreVideo                           0x00007fff88ee678f _ZL13startIOThreadPv + 147
    11  libsystem_pthread.dylib             0x00007fff88e67899 _pthread_body + 138
    12  libsystem_pthread.dylib             0x00007fff88e6772a _pthread_struct_init + 0
    13  libsystem_pthread.dylib             0x00007fff88e6bfc9 thread_start + 13
)

I've uploaded a test project on dropbox here

https://dl.dropboxusercontent.com/u/27229440/Temporary/Cocos2d31ExceptionTest.zip

To test it successfully just run the project and then click really fast in the scene.

@ypmits
Copy link

ypmits commented Aug 10, 2014

I hope someone can look at this. This particular bug is making sure LevelHelper is stuck in 3.0 modus and cannot move on to 3.1 or higher.

@vladubogdan
Copy link
Author

Its not just LevelHelper that needs this fix. Its every game out there. Im surprised nobody catch it until now. At least acknowledge or dismiss the bug.

@slembcke
Copy link
Contributor

Oh my... This has to be an absolutely ancient bug if I'm correct. I remember bringing it up a few years ago actually.

The default Mac/threading configuration is still CC_MAC_USE_DISPLAY_LINK_THREAD. For some reason I had thought it had changed. The mouse input methods happen on the main thread while rendering happens on a different thread. I really have no idea how to fix this without deprecating the Mac threading options. What it's doing is inherently unsafe and it's not really possible to fix without adding enough locks to destroy any threading performance advantage.

Try using CC_MAC_USE_MAIN_THREAD instead. I was wondering how this bug hasn't come up in SpriteBuilder, but I checked and it uses the main thread and not the default.

Should probably figure out what to do about this immediately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants