Runtime memory leaked warnings when executing objective-C code within C code with ARC enabled

久未见 提交于 2019-12-01 12:27:51

You're not missing an “ARC cast”.

I guess your C++ creates a separate thread and calls bufferReady on that thread. Since it's a C++ library, I presume it doesn't know anything about Objective-C or Cocoa memory management, so it doesn't create an autorelease pool. Therefore you should create an autorelease pool in bufferReady:

void bufferReady() {
    if (shakeCounter % 100 == 0) {
        @autoreleasepool {
            [refToSelf shakes];
        }
    }

    shakeCounter++;
}

But also I see that in shakes, you're creating Cocos2D objects and sending runAction: to yourself, presumably to run the action objects you created. Are you sure it's safe to do this on a random thread? Maybe you should send yourself shakes on the main thread. Here's an easy way to do that:

void bufferReady() {
    if (shakeCounter % 100 == 0) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [refToSelf shakes];
        });
    }

    shakeCounter++;
}

Since the main thread manages its own autorelease pool, you don't have to set one up in this case.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!