【发布时间】:2016-04-13 18:51:26
【问题描述】:
我对 GMSPolygon 对象有一个非常奇怪的问题。 我的代码突然崩溃,出现错误“无法识别的选择器发送到实例 0x....”
(是的,它工作了一整天,然后突然开始崩溃)
我正在使用一个多边形数组(跟踪它们并动态更新它们)并在我的 -viewDidLoad 中按如下方式初始化它们:
GMSPolygon *myPoly[50];
--
GMSMutablePath *path = [[GMSMutablePath alloc] init];
// set some fake coordinates, initializing a Polygon with an empty path seems to crash as well...
[path addCoordinate:CLLocationCoordinate2DMake(1,0)];
[path addCoordinate:CLLocationCoordinate2DMake(-1,0)];
[path addCoordinate:CLLocationCoordinate2DMake(0,1)];
for (int i=0;i<50;i++) {
myPoly[i] = [GMSPolygon polygonWithPath:path];
myPoly[i].map = nil;
}
稍后在我的程序中,我尝试以相同的方式再次访问该对象,首先将其设置为 nil,以便将其从地图中删除,然后更新并在必要时再次显示它
for (int i=0;i<50;i++) {
myPoly[i] = [GMSPolygon polygonWithPath:path];
myPoly[i].map = nil; <--------- CRASH
// Do other stuf here, update the Polygon data and if needed
// display again as follows:
myPoly[i].map = mapView_;
}
但它似乎崩溃了.. 如果我将 GMSPolygon 放在 NSMutable 数组中,也会发生同样的事情。初始化数组很好,但是将 GMSPolygon 从数组中取出并设置 .map 属性会导致同样的崩溃。
更新:
这似乎是由对象内存位置引起的。如果它工作正常,内存位置如下:
[0] GMSPolygon * 0x1558ed750 0x00000001558ed750
[0] GMSPolygon * 0x1558ed750 0x00000001558ed750
- 当它崩溃时
[0] GMSPolygon * 0x12e7cbf30 0x000000012e7cbf30
[0] GMSPolygon * 0x129d36630 0x0000000129d36630
该对象仅在 viewDidLoad 中初始化一次,其他任何地方都没有! 如果对象的内存位置不同,它显然可以解释崩溃。但是这里发生了什么?
有人知道为什么吗?
更新 2,现在有崩溃日志:
2016-04-14 15:16:49.618 myApp[1130:240689] -[GMSMutablePath setMap:]: unrecognized selector sent to instance 0x1540bb560
2016-04-14 15:16:49.627 myApp[1130:240689] void uncaughtExceptionHandler(NSException *__strong) [Line 354] CRASH: -[GMSMutablePath setMap:]: unrecognized selector sent to instance 0x1540bb560
2016-04-14 15:16:49.688 myApp[1130:240689] void uncaughtExceptionHandler(NSException *__strong) [Line 355] Stack Trace: (
0 CoreFoundation 0x0000000182ebee50 <redacted> + 148
1 libobjc.A.dylib 0x0000000182523f80 objc_exception_throw + 56
2 CoreFoundation 0x0000000182ec5ccc <redacted> + 0
3 CoreFoundation 0x0000000182ec2c74 <redacted> + 872
4 CoreFoundation 0x0000000182dc0d1c _CF_forwarding_prep_0 + 92
5 myApp 0x00000001001115a0 -[mapViewController plotPoly] + 2028
6 myApp 0x000000010012c944 -[mapViewController mapView:didChangeCameraPosition:] + 556
7 CoreFoundation 0x0000000182ec4ae0 <redacted> + 144
8 CoreFoundation 0x0000000182dbc548 <redacted> + 284
9 CoreFoundation 0x0000000182dc0e70 <redacted> + 60
10 myApp 0x0000000100203370 -[GMSDelegateForward forwardInvocation:] + 108
11 CoreFoundation 0x0000000182ec2aa4 <redacted> + 408
12 CoreFoundation 0x0000000182dc0d1c _CF_forwarding_prep_0 + 92
13 myApp 0x0000000100188fec -[GMSMapView updateWithCamera:] + 176
14 Foundation 0x0000000183893ffc <redacted> + 340
15 CoreFoundation 0x0000000182e75124 <redacted> + 24
16 CoreFoundation 0x0000000182e74bb8 <redacted> + 540
17 CoreFoundation 0x0000000182e728b8 <redacted> + 724
18 CoreFoundation 0x0000000182d9cd10 CFRunLoopRunSpecific + 384
19 GraphicsServices 0x0000000184684088 GSEventRunModal + 180
20 UIKit 0x0000000188069f70 UIApplicationMain + 204
21 myApp 0x000000010013dd3c main + 124
22 libdyld.dylib 0x000000018293a8b8 <redacted> + 4)
2016-04-14 15:16:49.700 myApp[1130:240689] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[GMSMutablePath setMap:]: unrecognized selector sent to instance 0x1540bb560'
*** First throw call stack:
(0x182ebee38 0x182523f80 0x182ec5ccc 0x182ec2c74 0x182dc0d1c 0x1001115a0
0x10012c944 0x182ec4ae0 0x182dbc548 0x182dc0e70 0x100203370 0x182ec2aa4
0x182dc0d1c 0x100188fec 0x183893ffc 0x182e75124 0x182e74bb8 0x182e728b8
0x182d9cd10 0x184684088 0x188069f70 0x10013dd3c 0x18293a8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
我现在惊呆了。 初始初始化工作正常,多边形被绘制为应该的,但是一旦我调用函数来重绘多边形,崩溃就发生了..现在很奇怪,我明白为什么它崩溃了,但我不明白..它将前 5 个数组条目更改为 GMSMutablePath 和 GMSPolyLine ,而不是 GMSPolygon ?!,请参阅下面的图片链接 ..并且不知道为什么,因为我 100% 确定 GMSPolygon 的数组不是同时触及其他任何地方..
【问题讨论】:
-
不要将objective-c对象存储在C数组中,它们不会保留其内容,因此当它们仍在数组中时会被释放
-
好的,你知道如何处理这个问题吗?我对标记做同样的事情,这似乎工作正常。关键是我需要跟踪我放在地图上的一些对象,因为它们是动态的,我不想每秒左右重绘地图..
标签: ios objective-c arrays google-maps polygon