游戏中疑似野指针的问题
int count = [tiles count]; //tiles为该类的成员变量,一个数组
NSMutableDictionary* dist = [[NSMutableDictionary alloc] initWithCapacity: count];
NSMutableDictionary* prev = [[NSMutableDictionary alloc] initWithCapacity: count];
NSMutableArray* q = [[NSMutableArray alloc] initWithArray:tiles];
NSMutableArray* path = [[NSMutableArray alloc] init];
MyNode* current = nil;
MyNode* currentTarget = nil;
MyNode* neighbor = nil;
nNeighbors = 0;
for (i = 0; i < count; i++)
{
[dist setValue:max forKey:[[tiles objectAtIndex_safe:i] value]];
[prev setValue:nil forKey:[[tiles objectAtIndex_safe:i] value]];
}
if(source!=nil)
{
[dist setObject:[NSNumber numberWithInt: 0] forKey:[source value]];
}
qCount = [q count];
while (qCount > 0)
{
current = [self nodeWithSmallestDistance:dist inQueue:q];
if (([[dist objectForKey: [current value]] intValue] == [max intValue])||(current == nil))
{
break;
}
[q removeObject: current];
nNeighbors = [[current neighbors] count];
for (i = 0; i < nNeighbors; i++)
{
neighbor = [[[current neighbors] objectAtIndex_safe:i] node];
alt = [[dist objectForKey: [current value]] intValue] + [[current distanceTo:neighbor] intValue];
int neighborDist = [[dist objectForKey: [neighbor value]] intValue];
//NSLog(@"%d,%d",[[neighbor value] intValue],alt);
if (alt < neighborDist)
{
[dist setObject:[NSNumber numberWithInt: alt] forKey: [neighbor value]];
[prev setObject:current forKey:[neighbor value]];
}
}
qCount = [q count];
}
currentTarget = target;
// NSLog(@"%@",[currentTarget value]);
while ([prev objectForKey:[currentTarget value]] != nil)
{
[path insertObject:[prev objectForKey:[currentTarget value]] atIndex:0];
currentTarget = [prev objectForKey:[currentTarget value]];
}
-(MyNode*) nodeWithSmallestDistance:(NSDictionary*) dist inQueue: (NSArray*) q
{
NSInteger i, count, min;
MyNode* node = nil;
NSString* nodeValue;
count = [q count];
min = TagForFriendArmy;
for (i = 0; i < count; i++)
{
nodeValue = [[q objectAtIndex_safe: i] value];
if ([[dist objectForKey: nodeValue] intValue] < min)
{
min = [[dist objectForKey: nodeValue] intValue];
node = [q objectAtIndex_safe: i];
}
}
return node;
}
上面while里面insertObject红色的那行有时会crash,小概率事件,但是线上会偶尔出现,很头疼,怀疑是野指针的问题,请高人看一下,到底问题出在哪儿,多谢了