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
您不会错过“ARCcast”。
我猜你的 C++ 创建了一个单独的线程并调用bufferReady
在那条线上。由于它是一个 C++ 库,我认为它对 Objective-C 或 Cocoa 内存管理一无所知,因此它不会创建自动释放池。所以you应该创建一个自动释放池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++;
}
由于主线程管理自己的自动释放池,因此在这种情况下您不必设置自动释放池。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)