Ios 在启用ARC的C代码中执行objective-C代码时,运行时内存泄漏警告 ARC启用,缓冲区> /COD>由C++库触发(非弧启动),我确信我在某个地方错过了一个弧。请告知。提前谢谢
代码如下:Ios 在启用ARC的C代码中执行objective-C代码时,运行时内存泄漏警告 ARC启用,缓冲区> /COD>由C++库触发(非弧启动),我确信我在某个地方错过了一个弧。请告知。提前谢谢,ios,memory-leaks,cocos2d-iphone,automatic-ref-counting,Ios,Memory Leaks,Cocos2d Iphone,Automatic Ref Counting,代码如下: @implementation HelloWorldLayer id refToSelf; //reference to self int shakeCounter = 0; void bufferReady() { if (shakeCounter % 100 == 0) { [refToSelf shakes]; } shakeCounter++; } - (void) shakes { CCRotateBy * rota
@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
你不会错过一个“弧形投射”
我猜您的C++会创建一个单独的线程,并在该线程上调用<代码>缓冲区> /COD>。由于它是C++库,所以我认为它对Objto-C或COOMA内存管理一无所知,所以它不创建自动存储池。因此,您应该在
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++;
}
因为主线程管理自己的自动释放池,所以在这种情况下,您不必设置自动释放池。您就是f-ing人!!!我显然没有完全掌握ARC和内存管理。你有什么阅读建议吗?内存管理:从。自动释放池和线程:。弧:。