2013-06-24 91 views
0

我在打开视图多次后崩溃了应用程序时遇到问题。MonoTouch:另一个线程从处理对象访问变量

当我的视图出现时,它会在其他线程上产生多个排队的请求。从另一个线程调用委托来更新控制器。

我确实尝试取消线程,但偶尔会调用委托,引用一个不再存在的“this”(垃圾收集)。

如何检查本地对象(是为每个管理对象创建的)是否被释放?

下面是每次加载控制器时被调用多次的方法。

protected void RequestImageFromSource (string source, NIPhotoScrollViewPhotoSize photoSize, int photoIndex) 
{ 
    var isThumbnail = photoSize == NIPhotoScrollViewPhotoSize.NIPhotoScrollViewPhotoSizeThumbnail; 
    var identifier = IdentifierWithPhotoSize (photoSize, photoIndex); 
    var identifierKey = IdentifierKeyFromIdentifier (identifier); 
    var photoIndexKey = CacheKeyForPhotoIndex (photoIndex); 

    // avoid duplicate requests 
    if (ActiveRequests.Contains (identifierKey)) 
     return; 

    NSUrl url = new NSUrl (source); 

    NSMutableUrlRequest request = new NSMutableUrlRequest (url); 
    request.TimeoutInterval = 30; 

    var readOp = AFImageRequestOperation.ImageRequestOperationWithRequest (request, null, 
     (NSUrlRequest req, NSHttpUrlResponse resp, UIImage img) => 
     { 
      // Store the image in the correct image cache. 
      if (isThumbnail) { 
       ThumbnailImageCache.StoreObject(img, photoIndexKey); 

      } else { 
       HighQualityImageCache.StoreObject(img, photoIndexKey); 
      } 
      // If you decide to move this code around then ensure that this method is called from 
      // the main thread. Calling it from any other thread will have undefined results. 
      PhotoAlbumView.DidLoadPhoto(img, photoIndex, photoSize); 

      if(isThumbnail) { 
       if(PhotoScrubberView != null) 
        PhotoScrubberView.DidLoadThumbnail(img, photoIndex); 
      } 
      // ERROR THROWN HERE 
      this.ActiveRequests.Remove(identifierKey); 

     }, (NSUrlRequest req, NSHttpUrlResponse resp, NSError er) => { 

     }); 

    readOp.ImageScale = 1; 

    // Start the operation. 
    ActiveRequests.Add(identifierKey); 
    Queue.AddOperation(readOp); 
} 

这里是抛出的错误。

MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown. 
Name: NSInvalidArgumentException Reason: -[__NSCFSet removeObject:]: attempt to remove nil at 
at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend_IntPtr (intptr,intptr,intptr) 
at MonoTouch.Foundation.NSMutableSet.Remove (MonoTouch.Foundation.NSObject nso) [0x0001c] in /Developer/MonoTouch/Source/monotouch/src/Foundation/NSMutableSet.g.cs:152 
at MonoTouch.Nimbus.Demo.NetworkPhotoAlbumViewController+ <RequestImageFromSource>c__AnonStorey0.<>m__1 (MonoTouch.Foundation.NSUrlRequest req, MonoTouch.Foundation.NSHttpUrlResponse resp, MonoTouch.UIKit.UIImage img) [0x000a4] in /Users/Paul/Git/MedXChange.iOS/SubModules/MonoTouch.Nimbus/MonoTouch.Nimbus.Demo/Photos/NetworkPhotoAlbumViewController.cs:127 
at MonoTouch.Trampolines+SDImageRequestOperationWithRequestSuccess2.TImageRequestOperationWithRequestSuccess2 (IntPtr block, IntPtr request, IntPtr response, IntPtr image) [0x00053] in /Users/Paul/Git/MedXChange.iOS/SubModules/MonoTouch.Nimbus/MonoTouch.Nimbus/obj/Debug/ios/ObjCRuntime/Trampolines.g.cs:182 
at 
at (wrapper native-to-managed) MonoTouch.Trampolines/SDImageRequestOperationWithRequestSuccess2:TImageRequestOperationWithRequestSuccess2 (intptr,intptr,intptr,intptr) 
at 
at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) 
at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38 
at MonoTouch.Nimbus.Demo.Application.Main (System.String[] args) [0x00000] in /Users/Paul/Git/MedXChange.iOS/SubModules/MonoTouch.Nimbus/MonoTouch.Nimbus.Demo/Main.cs:17 

回答

0

您可以检查是否有管理对象的本地同行已经通过检查Handle属性发布:

bool IsReleased (NSObject obj) 
{ 
    return obj.Handle == IntPtr.Zero; 
} 
+0

我知道,在C#/本地互操作的世界,但完全在MonoTouch的世界里一片空白。谢谢! –