removeConnection results in EXC_BAD_ACCESS

。_饼干妹妹 提交于 2020-01-04 04:33:31

问题


This is Apple's code

- (BOOL)removeConnection: (MIDINetworkConnection *)connection;

in

-[MIDINetworkSession removeConnection:]

yet it results in an EXC_BAD_ACCESS. This only happens in iOS 9.

Any help or workarounds?


回答1:


Yar's answer helped me, except it doesn't cover the case where a disconnection happens from the other device. Instead of storing the objects to an array in removeConnection: I have a manager object that listens to the MIDINetworkNotificationSessionDidChange notification, looks for any new connections, and adds the references to an NSMutableSet.

So, in my manager init I have:

self.connRefs = [NSMutableSet set];

[[NSNotificationCenter defaultCenter] addObserver:self 
     selector:@selector(sessionChanged:) 
     name:MIDINetworkNotificationSessionDidChange object:nil];
[self sessionChanged:nil];

... and my sessionChanged: method:

- (void)sessionChanged:(NSNotification *)n {
  // ios9 bug hack to keep ref to prevent bad_exec
  for (MIDINetworkConnection *c in [MIDINetworkSession defaultSession].connections) {
    [self.connRefs addObject:c];
  }
}

That seems like a fast way to figure out how to store a reference to each connection, no matter who initiated it. Then when the connection is removed (either by your app, or the other device), the reference is already stored, and no crash!




回答2:


It's the MIDINetworkConnection that's getting dealloced and causing the issue.

The workaround that I'm using is that I add these objects to an NSMutableArray before calling removeConnection: (mine is called connectionsThatHaveBeenClosed ;) ). Unfortunately, I have to keep this array growing until the App is closed, which is a leak.



来源:https://stackoverflow.com/questions/32686214/removeconnection-results-in-exc-bad-access

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!