views:

52

answers:

4

A tiny pointer problem :)

So basically, I have two applications. Application 1 launches Application 2 but it remains in memory. When Application 2 is started it needs to be given a pointer to a CALayer object which is stored in the first application. The object represented by the pointer needs to be accessible by both applications. I am using Objective-C. I would really appreciate any examples.

I need something more cross-platform and Distributed Objects are Mac OS X specific. And it is not a matter of me not knowing that CALayer's are Mac specific (well kindof because of iOS). I know that pretty damn well, it is just that I want to be using a cross-platform solution instead of a specific one.

+1  A: 

use distribute object in Mac OS X, with distribute object concept you can share your CALayer object between Application1 and Aplication2

Girish Kolari
I need something more C-like. Not something too platform specific.
Nick Brooks
@Nick You want something platform neutral but you're using `CALayer`? You apparently don't understand what you're asking for. `CALayer` exists only in the Cocoa/Cocoa-Touch frameworks.
Dave DeLong
Actually, I do know that. CALayer was a general example.
Nick Brooks
I suggested distribute object since you where using Objective C, most of the time cross platform solution use to be in C/C++.If you want some cross-platform solution then you have to use IPC(may be local BSD socket) and implement object serialization.
Girish Kolari
A: 

There is no portable (cross-platform) way to pass pointers between processes (applications). Portably, the processes can exchange serializations of objects using network connections or files.

Peter G.
+2  A: 

You will not be able to pass a pointer across application boundaries as the processes will have different address spaces. If you need to refer to that object and initiate operations against it you will need to use a distributed object system or other interprocess communication layer.

The impression I get is that you want to spawn the child process, have it update some shared data structure and then exit. One possibility would be to use a shared memory system such as mmap (2) or System V shared memory. Unfortunately portable shared memory is a bit harder to achieve across Windows and Unix/Linux, and you would probably have to make some platform specific wrappers. This MSDN article should give you a starter on a mmap-like facility on Windows, but I've never done shared memory on this platform so I can't really vouch for any of the ins and outs.

Another possibility would be to serialise the object and pass the serialised data structure between the processes with a pipe or other mechanism. Again, doing this portably is somewhat fraught as IPC on Unix and Windows works quite differently.

Note that with shared memory, the object will have to be initialised within the shared memory pool, which means that this will have to be allocated first. Otherwise you will need to copy the data in and out of the shared pool explicitly. If the data structure has any pointers within it then this movement will break the pointers.

The latter point also means that the parent ahd child process will also need to map the shared memory into the same virtual address if you intend to follow pointers within the shared data structure. The alternative if you need references within the data structure is to implement them as handles or offsets from a base. This will complicate the code that builds your data structure so mapping to the same address so you can use pointers is a win.

Also swot up on security of files used for mmap if you decide to use this.

ConcernedOfTunbridgeWells
Could you give an example of using shared memory on Max OS X?
Nick Brooks
On any unix-derived system, look up mmap(2) which allows you to map a file into virtual memory. This will work on OSX, Unix, Linux etc but it's not portable to Windows. You would have to write wrappers to do it on Unix/Windows. Get mmap working on OSX first and see if it does what you want. Another API that will do this is System V shared memory. Try googling for "System V shared memory". I presume that OSX supports SYSV shmem but I'm not sure off the top of my head.
ConcernedOfTunbridgeWells
A: 

A portable way of sharing data between applications is by using Inter Process Communication. In this case, this is shared memory. Both POSIX and System V APIs are available on Mac OS X.

Some links can be found in Alternatives to Notifications section of Notification Overview document.

mouviciel