0

多次打开视图后,我的应用程序崩溃了。

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

我确实尝试取消线程,但偶尔会调用委托,引用不再存在的“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
4

1 回答 1

0

您可以通过检查 Handle 属性来检查托管对象的本机对等点是否已被释放:

bool IsReleased (NSObject obj)
{
    return obj.Handle == IntPtr.Zero;
}
于 2013-06-24T21:00:10.130 回答