Singeleton - 错误:[CFString isNSString__]消息发送到解除分配的实例

时间:2013-08-13 07:14:07

标签: ios objective-c singleton

我在单例中有一个NSMutableArray,我想在两个类中访问它。我在另一个项目中完成了这个(那个人有ARC,这个没有),并且它在那里工作。

项目没有ARC。

我收到了错误:

*** -[CFString isNSString__]: message sent to deallocated instance 0xb3d1db0

StoreVars.h

@interface StoreVars : NSObject
@property (nonatomic, retain) NSMutableArray *sharedArrayOfVideo;
+ (StoreVars *)sharedInstance;
@end

StoreVars.m

@implementation StoreVars
@synthesize sharedArrayOfVideo;
+ (StoreVars *) sharedInstance {
    static StoreVars *myInstance = nil;
    if (myInstance == nil) {
        myInstance = [[[[self class] alloc] init] retain];
        myInstance.sharedArrayOfVideo = [[NSMutableArray alloc] init];
    }
    return myInstance;
}
@end

异步填充数组:

NSDictionary *tempDict = [[NSDictionary alloc] initWithObjectsAndKeys:
                                      ID,@"id",
                                      title,@"title", nil];
[[StoreVars sharedInstance].sharedArrayOfVideo addObject:tempDict];

这是发生崩溃的地方:

 NSLog(@"%i",[[StoreVars sharedInstance].sharedArrayOfVideo count]);
 NSLog(@"%@",[StoreVars sharedInstance]);
 if ([[StoreVars sharedInstance] respondsToSelector:@selector(sharedArrayOfVideo)]) {
        **NSLog(@"%@",[StoreVars sharedInstance].sharedArrayOfVideo);**
       //NSLog(@"%@",[[StoreVars sharedInstance].sharedArrayOfVideo objectAtIndex:8]);
}

输出:

10
<StoreVars: 0xb381b00>
*** -[CFString isNSString__]: message sent to deallocated instance 0xb3d1db0

4 个答案:

答案 0 :(得分:1)

发现问题,在创建字典时,我做了:

NSString *ID = [[[arrayOfEntry objectAtIndex:index] objectForKey:@"id"]; autorelease]

NSString *title = [[[arrayOfEntry objectAtIndex:index] objectForKey:@"title"] autorelease];

而不是:

 NSString *ID = [[arrayOfEntry objectAtIndex:index] objectForKey:@"id"];

 NSString *title = [[arrayOfEntry objectAtIndex:index] objectForKey:@"title"];


 NSDictionary *tempDict = [NSDictionary dictionaryWithObjectsAndKeys:ID,@"id",title,@"title", nil];

答案 1 :(得分:0)

我对您的代码中的以下行有疑问:

if ([[StoreVars sharedInstance] respondsToSelector:@selector(sharedArrayOfVideo)])

Q值。你为什么试图通过选择器检查mutableArray作为方法?

如果您想迭代添加的词典,请执行以下操作

StoreVars *storeVars = [StoreVars sharedInstance];
if(storeVars.sharedArrayOfVideo.count>0)
{
   for(int i=0; i<storeVars.sharedArrayOfVideo.count; i++)
   {
      NSDictionary *dictionary = [storeVars.sharedArrayOfVideo objectAtIndex:i];

      // do stuff with the grabbed dictionary.
   }
}
else { NSLog(@"sharedArrayOfVideo is empty"); }

此外,在您的单身人士创建代码中,执行以下编辑:

而不是以下:

if (myInstance == nil) {
        myInstance = [[[[self class] alloc] init] retain];

使用以下内容:

if (myInstance == nil) {
        myInstance = [[StoreVars alloc] init]; 
        // you already own the above object via alloc/init, so no extra retains.

答案 2 :(得分:0)

尝试使用gcd创建单例:

static ASBTLEManager *sharedInstance = nil;

+ (ASBTLEManager *)sharedManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[ASBTLEManager alloc] initInstance];
    });
    return sharedInstance;
}

答案 3 :(得分:0)

尝试这种方式来创建单例实例,这比你正在尝试的更准确

+(StoreVars *) sharedInstance
    {
        static StoreVars * myInstance = nil;
        static dispatch_once_t oneTimePredicate;

        //get calls only once.
        dispatch_once(&oneTimePredicate, ^{
            myInstance = [[self alloc] init];
            myInstance.sharedArrayOfVideo = [[NSMutableArray alloc] init];
        });
        return myInstance;
    }