0

I have a class called Node which contains a set of parameters and an NSMutableArray called subNodes. One process creates one Node object as a root of the tree and uses the subNodes arrays to create a large tree of Nodes. This entire tree should be passed to another process, so I set up an NSConnection:

Node *tree;
// ...create Node-Tree...
NSConnection *otherProcess = [NSConnection connectionWithRegisteredName:@"MyApp"
                                           host:nil];
MyObj *remoteObj = (MyObj*) [[otherProcess rootProxy] retain];
[remoteObj setNodeTree:tree];

The communication itself works, the remote method 'setNodeTree', which expects the root-Node will be called. However, the transfer of the tree doesn't work. I had to implement a copyWithZone method for the Node class:

-(id)copyWithZone:(NSZone*)zone
{
  Node *nodeCopy = [[[self class] allocWithZone:zone] init];

  [nodeCopy setSize:[self size]];
  [nodeCopy setSubnodes:[[self subnodes] copyWithZone:zone]];

  return nodeCopy;
}

But the client terminates with the following exception:

*** 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'

Any ideas what went wrong here? Apparently a BOOL variable is expected somewhere, but the Node doesn't contain any and there is no method used which expects or returns a BOOL.

  • If your `subnodes` property is either `retain` or `copy`, you are leaking an instance in `[nodeCopy setSubnodes:[[self subnodes] copyWithZone:zone]];`. I've gotten all kinds of weird errors from improperly implemented `copyWithZone:` implementations. – Richard Jan 17 '11 at 15:23
  • Ooops, thanks for the hint. That was a leftover from trying to get it working. – blastar Jan 17 '11 at 19:55

1 Answers1

0

Two alternatives are possible:

  • serialization and deserialization of whole tree,
  • shared memory between processes.

For performance and code readability, my preference goes to shared memory. A reference start is shmget(2) manual page.

mouviciel
  • 66,855
  • 13
  • 106
  • 140
  • I have tried serializing and passing the entire tree, but the Exception remains the same. Shared memory is a problem because I don't always know in advance how large the tree will become. – blastar Jan 17 '11 at 19:54