1

ARC 已启用bufferReady并由 C++ 库(未启用 ARC)触发,我确定我在某处缺少 ARC 转换。请指教。提前致谢。

使用以下代码:

@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;
}

在运行时,每次shakes执行时我都会收到内存泄漏警告。

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
4

1 回答 1

2

你不会错过“ARC cast”。

我猜您的 C++ 创建了一个单独的线程并bufferReady在该线程上调用。由于它是一个 C++ 库,我认为它对 Objective-C 或 Cocoa 内存管理一无所知,因此它不会创建自动释放池。因此,您应该在以下位置创建一个自动释放池bufferReady

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

    shakeCounter++;
}

但我也看到shakes,您正在创建 Cocos2D 对象并发runAction:送给自己,大概是为了运行您创建的操作对象。您确定在随机线程上执行此操作是安全的吗?也许您应该将自己发送shakes到主线程。这是一个简单的方法:

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

    shakeCounter++;
}

由于主线程管理自己的自动释放池,因此在这种情况下您不必设置一个。

于 2012-08-07T07:18:35.170 回答