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 に答える