首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MonoTouch:来自已释放对象的另一个线程访问变量

MonoTouch:来自已释放对象的另一个线程访问变量
EN

Stack Overflow用户
提问于 2013-06-24 15:54:32
回答 1查看 250关注 0票数 0

在多次打开视图后,我的应用程序崩溃,我有一个问题。

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

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

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

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

代码语言:javascript
复制
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);
}

下面是抛出的错误。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-24 21:00:10

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

代码语言:javascript
复制
bool IsReleased (NSObject obj)
{
    return obj.Handle == IntPtr.Zero;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17279763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档