MIDINetworkConnection EXC_BAD_ACCESS 错误

MIDINetworkConnection EXC_BAD_ACCESS error

我正在开发一个控制 daw 软件的应用程序。有时我会收到 EXC_BAD_ACCESS 错误。我相信它与 MIDINetworkConnection 有关,即使该项目启用了 ARC。它似乎试图访问连接数,但访问了错误的内存地址。谁能发现问题?

- (NSString*) describeConnections {

    NSMutableArray* connections = [NSMutableArray arrayWithCapacity:[[[MIDINetworkSession     defaultSession] connections] count]];
    for (MIDINetworkConnection* connection in [[MIDINetworkSession defaultSession] connections]) {
        [connections addObject:[[connection host] name]];
    }
    if ([connections count] > 0) {
        return [connections componentsJoinedByString:@", "];
    }
    else{
        return @"(Not connected)";

    }

}

断点错误停止于

 NSMutableArray* connections = [NSMutableArray arrayWithCapacity:[[[MIDINetworkSession     defaultSession] connections] count]];

消息:线程 1:EXC_BAD_ACCESS(代码=1,地址=0x2033b00c)

在调试器中,我看到 connections = (NSMutableArray*)0x839ab1e0 与上述错误不匹配。这可能是问题的原因吗?

这是线程堆栈跟踪:

* thread #1: tid = 0x9f2b0, 0x01da9e15 libobjc.A.dylib`objc_retain + 21, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x5302f2fb)
frame #0: 0x01da9e15 libobjc.A.dylib`objc_retain + 21
* frame #1: 0x0000d14b Fader`-[MIDIController describeConnections](self=0x7c3c35b0, _cmd=0x00083ee3) + 683 at MIDIController.m:149
frame #2: 0x00049073 Fader`-[MainViewController internalReloadConnections:](self=0x7d0f6000, _cmd=0x000849f0, sender=0x7d0f6000) + 115 at MainViewController.m:1087
frame #3: 0x000494e0 Fader`-[MainViewController updateDisplay](self=0x7d0f6000, _cmd=0x000847c9) + 272 at MainViewController.m:1143
frame #4: 0x014e2119 Foundation`__NSFireTimer + 97
frame #5: 0x020bf8d6 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22
frame #6: 0x020bf25d CoreFoundation`__CFRunLoopDoTimer + 1309
frame #7: 0x0207e6ba CoreFoundation`__CFRunLoopRun + 2090
frame #8: 0x0207dbcb CoreFoundation`CFRunLoopRunSpecific + 443
frame #9: 0x0207d9fb CoreFoundation`CFRunLoopRunInMode + 123
frame #10: 0x038d324f GraphicsServices`GSEventRunModal + 192
frame #11: 0x038d308c GraphicsServices`GSEventRun + 104
frame #12: 0x002b58b6 UIKit`UIApplicationMain + 1526
frame #13: 0x0000b06c Fader`main(argc=1, argv=0xbfff63f0) + 76 at main.m:14
frame #14: 0x02a6bac9 libdyld.dylib`start + 1

也许 [[MIDINetworkSession defaultSession] connections] 集合在您枚举时被更改了? (MIDI 连接 made/drops 输出)

快速枚举一个可变集合是不明智的,尤其是你不拥有的集合。我建议在开始时将它复制到 NSArray 中并枚举它

    - (NSString*) describeConnections {

       // NSArray *tempConnections = [NSArray arrayWithArray: [[MIDINetworkSession defaultSession] connections] ]; 
      //edit. replaced with line below, we are working with a set, not an array, see comments below..
        NSArray *tempConnections = [[[MIDINetworkSession defaultSession] connections]allItems];

        NSMutableArray* connections = [NSMutableArray arrayWithCapacity:[tempConnections count]];
        for (MIDINetworkConnection* connection in tempConnections) {
            [connections addObject:[[connection host] name]];
        }
        if ([connections count] > 0) {
            return [connections componentsJoinedByString:@", "];
        }
        else{
            return @"(Not connected)";

        }

    }