游戏中疑似野指针的问题

Naruto1983 2018-08-28 07:37:30
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,小概率事件,但是线上会偶尔出现,很头疼,怀疑是野指针的问题,请高人看一下,到底问题出在哪儿,多谢了
...全文
1216 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Naruto1983 2018-09-07
  • 打赏
  • 举报
回复
问题已经解决了,不是null,因为while那里做了判断,是null的话进不去的,后来发现是while那里在某些极端情况下会不停的循环执行,导致path这个数组非常大,最后就crash了。
p5433251 2018-09-05
  • 打赏
  • 举报
回复
集成个bugly多好。。
[path insertObject:[prev objectForKey:[currentTarget value]] atIndex:0]
如果数据是从服务端来的,可能是[currentTarget value]或者[prev objectForKey:[currentTarget value]这个东西取了个服务端的null过来。
Naruto1983 2018-09-03
  • 打赏
  • 举报
回复
回楼上的错误信息:

Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x00000007fffffff8
VM Region Info: 0x7fffffff8 is not in any region. Bytes after previous region: 26306674681
REGION TYPE START - END [ VSIZE] PRT/MAX SHRMOD REGION DETAIL
MALLOC_NANO (reserved) 00000001d8000000-00000001e0000000 [128.0M] rw-/rwx SM=NUL ...(unallocated)
--->
UNUSED SPACE AT END

Termination Signal: Segmentation fault: 11
Termination Reason: Namespace SIGNAL, Code 0xb
Terminating Process: exc handler [0]
Triggered by Thread: 0

Thread 0 name:
Thread 0 Crashed:
0 CoreFoundation 0x0000000183b7efb4 -[__NSArrayM insertObject:atIndex:] + 1244 (NSArrayM_Common.h:278)
1 CoreFoundation 0x0000000183b7ec64 -[__NSArrayM insertObject:atIndex:] + 396 (NSArrayM_Common.h:256)
2 caocao 0x000000010507cf30 -[GameLayer dijkstraFromNode_noAttack:data:] + 1724 (GameLayer.m:26895)
3 caocao 0x0000000105079a5c -[GameLayer getHeroUsedActionValueToPoint:::] + 516 (GameLayer.m:26028)
4 caocao 0x0000000105078a94 -[GameLayer getAIMaxValue::] + 25896 (GameLayer.m:24625)
5 caocao 0x000000010504633c -[GameLayer enemyStartAction] + 400 (GameLayer.m:10797)
6 caocao 0x000000010509ba50 -[CCCallFunc startWithTarget:] + 64 (CCActionInstant.m:282)
7 caocao 0x00000001050bd788 -[CCSequence update:] + 172 (CCActionInterval.m:217)
8 caocao 0x000000010500ffb0 -[CCActionManager update:] + 200 (CCActionManager.m:312)
9 caocao 0x0000000104f62014 -[CCScheduler tick:] + 244 (CCScheduler.m:583)
10 caocao 0x000000010501dc84 -[CCDirectorIOS drawScene] + 96 (CCDirectorIOS.m:152)
iBug168 2018-09-01
  • 打赏
  • 举报
回复
贴点错误信息出来。

29,028

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧