Runtime memory leaked warnings when executing obje

2019-02-20 10:17发布

问题:

ARC is enabled and bufferReady is being triggered by a C++ library(non-ARC enabled), and I'm sure I'm missing an ARC cast somewhere. Please advise. Thanks in advance.

With the code below:

@implementation HelloWorldLayer

id refToSelf;  //reference to self
int shakeCounter = 0;

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

    shakeCounter++;
}

- (void) shakes {
    CCRotateBy * rotate = [CCRotateBy actionWithDuration:0.1 angle:2];
    CCActionInterval * rotateReverse = [rotate reverse];
    CCSequence * seq1 = [CCSequence actions:rotate, rotateReverse, nil];

    CCMoveBy * shake = [CCMoveBy actionWithDuration:0.1 position:ccp(5, 0)];
    CCActionInterval * shakeReverse = [shake reverse];
    CCSequence * seq2 = [CCSequence actions:shake, shakeReverse, nil];

    CCSpawn * spawner = [CCSpawn actions:seq1, seq2, nil];
    CCSequence * lastSequence = [CCSequence actions:spawner, spawner, spawner, spawner, nil];

    [self runAction:lastSequence];
}

- (id) init {
    if((self = [super init])) {
        ...
    }
    refToSelf = self;
    return self;
}

During runtime I'm receiving memory leaked warnings every time shakes is executed.

objc[10060]: Object 0x466830 of class CCRotateBy autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
objc[10060]: Object 0x44cb70 of class CCRotateBy autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
objc[10060]: Object 0x46b260 of class CCSequence autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
objc[10060]: Object 0x45a790 of class CCMoveBy autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
objc[10060]: Object 0x469150 of class CCMoveBy autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
objc[10060]: Object 0x469190 of class CCSequence autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
objc[10060]: Object 0x46b350 of class CCSpawn autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
objc[10060]: Object 0x46b380 of class CCSequence autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
objc[10060]: Object 0x46b3b0 of class CCSequence autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
objc[10060]: Object 0x46bc00 of class CCSequence autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug

回答1:

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.