Update:
I have added a method that checks for null objects in dictionary and indeed, they were found:
Did anyone ever seen anything like this? This can't be caused by incorrect memory management. Note that dictionary object tree on the left is actually correct.
End update
I'm adding a number of entries to the NSMutableDictionary
with ARC enabled. After one of the additions the object added suddenly becomes null. Here's the code (I expanded it to make sure I haven't gone mad):
NSMutableDictionary *base = [NSMutableDictionary dictionary];
id o = [GAConstant kTRUE];
id k = [GAClause parse:@"(x 1 1)"];
[base setObject:o forKey:k];
NSLog(@"### Added (%@ -> %@): %@", k, o, base);
o = [GAClause parse:@"(y X)"];
k = [GAClause parse:@"(x 2 X)"];
[base setObject:o forKey:k];
NSLog(@"### Added (%@ -> %@): %@", k, o, base);
o = [GAConstant kTRUE];
k = [GAClause parse:@"(y 3)"];
[base setObject:o forKey:k];
NSLog(@"### Added (%@ -> %@): %@", k, o, base);
o = [GAConstant kTRUE];
k = [GAClause parse:@"(y 6)"];
[base setObject:o forKey:k];
NSLog(@"### Added (%@ -> %@): %@", k, o, base);
Here's the output:
2013-03-25 16:10:33.546 Jungle[1978:11303] ### Added (<GAListClause: 0x75b0dc0> -> <GAConstant: 0x75b12b0>): {
"<GAListClause: 0x75b1b00>" = "<GAConstant: 0x75b12b0>";
}
2013-03-25 16:10:33.548 Jungle[1978:11303] ### Added (<GAListClause: 0x75b2870> -> <GAListClause: 0x75b1aa0>): {
"<GAListClause: 0x75b2270>" = "<GAListClause: 0x75b1aa0>";
"<GAListClause: 0x75b1b00>" = "<GAConstant: 0x75b12b0>";
}
2013-03-25 16:10:33.549 Jungle[1978:11303] ### Added (<GAListClause: 0x75b2b90> -> <GAConstant: 0x75b12b0>): {
"<GAListClause: 0x75b2d80>" = "<GAConstant: 0x75b12b0>";
"<GAListClause: 0x75b2270>" = "<GAListClause: 0x75b1aa0>";
"<GAListClause: 0x75b1b00>" = "<GAConstant: 0x75b12b0>";
}
2013-03-25 16:10:33.550 Jungle[1978:11303] ### Added (<GAListClause: 0x75b2f00> -> <GAConstant: 0x75b12b0>): {
"<GAListClause: 0x75b2d80>" = "<GAConstant: 0x75b12b0>";
"<GAListClause: 0x75b2270>" = "<GAListClause: 0x75b1aa0>";
"<GAListClause: 0x75b1b00>" = "<GAConstant: 0x75b12b0>";
"<GAListClause: 0x75b1d60>" = (null);
}
The method [GAConstant kTRUE]
initializes and returns a static variable. [GAClause parse:]
returns a new parsed object every time.
It's clear why addresses of k
variables do not correspond to the contents of the dictionary: it copies them. Still not clear how a null can sneak in as a value. The location of null in the dictionary changes with every run, sometimes I'm getting two of them.
Looks like something is going on with the memory management, but what? ARC is enabled here.
Here's the code relevant to [GAConstant kTRUE]
method:
+ (GAConstant *)kTRUE {
static GAConstant *kTRUE = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
kTRUE = [[GAConstant alloc] initWithString:@"true"];
});
return kTRUE;
}
Sometimes null is fluctuating across the dictionary:
2013-03-25 17:09:16.426 Jungle[2294:11303] ### Added (<GAListClause: 0x7182ce0> -> <GAConstant: 0x7183430>): {
"<GAListClause: 0x7183a30>" = "<GAConstant: 0x7183430>";
}
2013-03-25 17:09:16.428 Jungle[2294:11303] ### Added (<GAListClause: 0x75467b0> -> <GAListClause: 0x71839d0>): {
"<GAListClause: 0x7546a30>" = (null);
"<GAListClause: 0x7183a30>" = "<GAConstant: 0x7183430>";
}
2013-03-25 17:09:16.429 Jungle[2294:11303] ### Added (<GAListClause: 0x7546cd0> -> <GAConstant: 0x7183430>): {
"<GAListClause: 0x7546ec0>" = "<GAConstant: 0x7183430>";
"<GAListClause: 0x7546a30>" = "<GAListClause: 0x71839d0>";
"<GAListClause: 0x7183a30>" = "<GAConstant: 0x7183430>";
}
2013-03-25 17:09:16.430 Jungle[2294:11303] ### Added (<GAListClause: 0x7547040> -> <GAConstant: 0x7183430>): {
"<GAListClause: 0x75470f0>" = (null);
"<GAListClause: 0x7546ec0>" = "<GAConstant: 0x7183430>";
"<GAListClause: 0x7546a30>" = "<GAListClause: 0x71839d0>";
"<GAListClause: 0x7183a30>" = "<GAConstant: 0x7183430>";
}
Here's what it looks like in the debugger:
[base setObject:[o copy] forKey:[k copy]];
[GAConstant kTRUE]
saved correctly to the static variable? That is, as a strong reference? I suspect the invalid value in your dictionary is actually a[NSNull null]
. Is there a place in your code where you actually useNSNull
? Maybe some collision happening? Is[NSObject hash]
and[NSObject isEqual:]
implemented correctly onGAClause
?init
for all your objects.