【发布时间】:2011-01-17 14:27:07
【问题描述】:
我有一个名为 Node 的类,它包含一组参数和一个名为 subNodes 的 NSMutableArray。一个进程创建一个 Node 对象作为树的根,并使用 subNodes 数组创建一棵大的 Node 树。这整个树应该传递给另一个进程,所以我设置了一个 NSConnection:
Node *tree;
// ...create Node-Tree...
NSConnection *otherProcess = [NSConnection connectionWithRegisteredName:@"MyApp"
host:nil];
MyObj *remoteObj = (MyObj*) [[otherProcess rootProxy] retain];
[remoteObj setNodeTree:tree];
通信本身有效,远程方法'setNodeTree',期望根节点将被调用。但是,树的转移不起作用。我必须为 Node 类实现一个 copyWithZone 方法:
-(id)copyWithZone:(NSZone*)zone
{
Node *nodeCopy = [[[self class] allocWithZone:zone] init];
[nodeCopy setSize:[self size]];
[nodeCopy setSubnodes:[[self subnodes] copyWithZone:zone]];
return nodeCopy;
}
但客户端终止并出现以下异常:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: '[NOTE: this exception originated in the server.]
Cannot create BOOL from object <Node: 0x10018f640> of class NSDistantObject'
*** Call stack at first throw:
(
0 CoreFoundation 0x00007fff81f687b4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x00007fff823a80f3 objc_exception_throw + 45
2 Foundation 0x00007fff8831e0c3 -[NSConnection sendInvocation:internal:] + 4304
3 CoreFoundation 0x00007fff81f3a98c ___forwarding___ + 860
4 CoreFoundation 0x00007fff81f36a68 _CF_forwarding_prep_0 + 232
5 MyProgram 0x00000001000015d5 main + 260
6 MyProgram 0x0000000100000fa8 start + 52
7 ??? 0x0000000000000002 0x0 + 2
)
terminate called after throwing an instance of 'NSException'
你知道这里出了什么问题吗?显然,在某处需要一个 BOOL 变量,但 Node 不包含任何变量,并且没有使用任何期望或返回 BOOL 的方法。
【问题讨论】:
-
如果您的
subnodes属性是retain或copy,则您正在泄漏[nodeCopy setSubnodes:[[self subnodes] copyWithZone:zone]];中的一个实例。我从不正确实施的copyWithZone:实施中得到了各种奇怪的错误。 -
糟糕,感谢您的提示。这是试图让它工作的剩余部分。
标签: objective-c exception deep-copy distributed-objects