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

Ios Singleton-错误:[CFString isNSString__;]消息已发送到解除分配的实例,ios,objective-c,singleton,Ios,Objective C,Singleton,我在单例中有一个NSMutableArray,我想在两个类中访问它。我在另一个项目中也做过类似的工作(那个项目有ARC,这个项目没有),它在那里工作 项目未启用弧。 我得到了一个错误: *** -[CFString isNSString__]: message sent to deallocated instance 0xb3d1db0 StoreVars.h @interface StoreVars : NSObject @property (nonatomic, retain) NSMut

我在单例中有一个NSMutableArray,我想在两个类中访问它。我在另一个项目中也做过类似的工作(那个项目有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
10
***-[CFString isNSString__;]:发送到解除分配实例0xb3d1db0的消息

关于您代码中的以下行,我有一个问题:

if ([[StoreVars sharedInstance] respondsToSelector:@selector(sharedArrayOfVideo)])
为什么您试图通过选择器检查mutableArray as方法

如果您想遍历添加的字典,请执行以下操作

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.

尝试使用gcd创建单例,如下所示:

static ASBTLEManager *sharedInstance = nil;

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

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

+(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;
    }

发现问题时,在创建字典时,我执行了以下操作:

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];

我到底要怎么做?(附:那行代码是我的一位同事插入的,他正在试图修复这个bug)。这不是问题的答案。你可以使用问题下方的“添加评论”链接要求作者澄清。是的,Martin R这不是答案。但是我写它是因为代码部分。在注释中添加代码非常麻烦。一旦我弄清楚了我的问题,我会对这个答案本身进行编辑,使它成为一个完整的答案。我现在就这么做。@Lord:假设您需要从数组中访问字典。检查我的编辑。编辑完成后,添加了NSLog(@“%@”,字典);在//do stuff下,收到相同的错误:**-[CFString isNSString_u_u;]:发送到解除分配实例0xc8a33c0的消息。这只是一个简单的暗中一击,也许这让它起作用了。同样的崩溃也会发生:**-[CFString isNSString_uuu;]:发送到解除分配实例0xB3BB810的消息是您的程序在NSLog(@“%i”,[[StoreVars sharedInstance].sharedArrayOfVideo count]上崩溃了;或者其他行?它在if语句后崩溃(当尝试访问数组元素时),count显示10。Zombies工具可以告诉您释放对象的保留/释放历史。使用它来查找丢失的保留或(如您所发现的)额外释放或自动释放。