自OSX 10.8以来在ScreenSaver中崩溃

时间:2012-07-26 12:27:53

标签: objective-c macos cocoa screensaver

自OSX 10.8以来,我的屏幕保护程序崩溃了:

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libobjc.A.dylib                 0x00007fff94749790 objc_msgSend_vtable13 + 16
1   com.apple.Foundation            0x00007fff9508941f -[NSObject(NSThreadPerformAdditions) performSelector:onThread:withObject:waitUntilDone:modes:] + 163
2   com.apple.Foundation            0x00007fff950892f8 -[NSObject(NSThreadPerformAdditions) performSelectorOnMainThread:withObject:waitUntilDone:] + 131
3   albertzeyer.PictureSlider       0x000000010f6e64c7 -[PictureSliderView nextFileName] + 71 (PictureSliderView.m:69)
4   albertzeyer.PictureSlider       0x000000010f6e6675 -[PictureSliderView loadNext] + 53 (PictureSliderView.m:86)
5   albertzeyer.PictureSlider       0x000000010f6e695f -[PictureSliderView initWithFrame:isPreview:] + 447 (PictureSliderView.m:116)
6   com.apple.ScreenSaver           0x00007fff968c7cb5 -[ScreenSaverModules loadModule:frame:isPreview:] + 968

相关代码:

- (void) queuedFileNamesPop:(NSString**)fn {
    if([queuedFileNames count] > 0) {
        *fn = [queuedFileNames objectAtIndex:0];
        [queuedFileNames removeObjectAtIndex:0];
    }
}

- (NSString*) nextFileName
{
    NSString* fn = nil;
    [self performSelectorOnMainThread:@selector(queuedFileNamesPop:) withObject:(id)&fn waitUntilDone:YES];
    if(!fn) {
        [nextFileNameLock lock];
        fn = [[NSString alloc] initWithUTF8String:FileQueue_getNextFile()];
        [nextFileNameLock unlock];
    }
    return fn;
}

似乎在performSelectorOnMainThread崩溃,但我不明白为什么。有什么明显的东西我做错了吗?

此外,它只会在从ScreenSaverEngine运行时崩溃。我还有一个小的虚拟应用程序,它使用相同的视图来测试屏幕保护程序,它不会在那里崩溃。


编辑:我刚才询问了一些非常相关(相同代码)的内容here。我想知道为什么我最终没有采用其中一种解决方案......


编辑:在Jonathan建议的代码更改之后,此崩溃不再发生。但是,在其他事件中,我正在通过此回溯获得其他崩溃:

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   com.apple.CoreFoundation        0x00007fff8f9aefe8 CFRelease + 248
1   com.apple.CoreFoundation        0x00007fff8f9d6770 -[__NSArrayM removeObjectAtIndex:] + 400
2   albertzeyer.PictureSlider       0x0000000108c54425 -[PictureSliderView queuedFileNamesPop:] + 197 (PictureSliderView.m:64)
3   com.apple.Foundation            0x00007fff95089450 -[NSObject(NSThreadPerformAdditions) performSelector:onThread:withObject:waitUntilDone:modes:] + 212
4   com.apple.Foundation            0x00007fff950892f8 -[NSObject(NSThreadPerformAdditions) performSelectorOnMainThread:withObject:waitUntilDone:] + 131
5   albertzeyer.PictureSlider       0x0000000108c544b0 -[PictureSliderView nextFileName] + 128 (PictureSliderView.m:71)
6   albertzeyer.PictureSlider       0x0000000108c54665 -[PictureSliderView loadNext] + 53 (PictureSliderView.m:88)
7   albertzeyer.PictureSlider       0x0000000108c54cce -[PictureSliderView keyDown:] + 382 (PictureSliderView.m:178)
8   com.apple.AppKit                0x00007fff924ad8e0 -[NSWindow sendEvent:] + 9687

或者这个:

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libobjc.A.dylib                 0x00007fff947497d0 objc_msgSend_vtable14 + 16
1   com.apple.CoreFoundation        0x00007fff8f9aef9a CFRelease + 170
2   com.apple.CoreFoundation        0x00007fff8f9d6770 -[__NSArrayM removeObjectAtIndex:] + 400
3   albertzeyer.PictureSlider       0x00000001023fac51 -[PictureSliderView keyDown:] + 257 (PictureSliderView.m:172)
4   com.apple.AppKit                0x00007fff924ad8e0 -[NSWindow sendEvent:] + 9687

我不确定他们是相关的还是独立的。

编辑:他们没有关系。他们是因为在其他地方又增加了一个[fn release]

1 个答案:

答案 0 :(得分:2)

您正在向对象投射非对象。这可能与Objective-C运行时或Foundation中的内存管理更改相冲突(看起来他们可能已经在启用ARC的情况下开始构建NSObject。)

代替(id)&fn,试试这个:

NSValue *fnHandle = [NSValue valueWithPointer: &fn];
[self performSelectorOnMainThread: @selector(queuedFileNamesPop:) withObject: fnHandle waitUntilDone: YES];

这会传递包含在对象中的fn的地址。然后,在被调用的方法中:

- (void)queuedFileNamesPop: (NSValue *)fnHandle {
    NSString **fn = [fnHandle pointerValue];
    if (fn) {
        // existing code here
    }
}

这可确保始终遵循正确的内存管理。