2013-06-24 125 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的世界裏一片空白。謝謝! –