dynamic memory across shared libraries

hi all,

this is my first post here..

i wonder if anybody can direct me to (or present) a good tutorial on sharing dynamically allocated objects across shared libraries in the same process, and between shared libaries and main().

in particular, i need to know what creation and destruction sequences are valid when libraries are being loaded and unloaded. for example, is it valid to allocate an object from inside a shared library procedure, and then delete that pointer from a different module, especially in the case where the allocating module has already been unloaded.

i imagine there might be all kinds of problems with this. although my preliminary tests seem to work most of the time, i get crashes from time to time, but i'm not sure if they're caused by memory management or by threading issues.

i've been restructuring my code to use a global context object to manage object creation and destruction from main(), but i'd like to find a clear exposition of the specific issues i'm dealing with before i go too much further.

can anybody offer any advice on this?

cheers
Jono
No, each process gets its own memory, and should be solely accountable for its own stuff -- create and clean itself up.

You need to set up some form of IPC. I recommend you start at mmap()
http://linux.die.net/man/2/mmap
This way you can have a chunk of memory which is shared between processes.

Good luck!
hi Duoas,

thanx for the reply.

the thing is, i don't need to share objects across processes, just between shared libraries in the same process.

as far as linkage is concerned, apart from its exports each library is a world in itself, however it is quite valid for a shared library to access data in main() if it is provided with a suitable reference.

the situation i'm trying to manage is where objects are created dynamically in a shared library, but persist for longer than the lifetime of the library.

i'm fairly sure this is valid c++, and i'm fairly sure it works ok with windows dll's. i've even seen architectures that use 'hot swappable' classes, wherein new implimentations of classes can be installed while the old ones are still in use in memory (provided the interfaces are constant).

the problem i have is that i suspect there are some gotchas with deletion of these objects. i'm using boost::shared_ptr, which is reference counted, so deletion is automatic, so i need to get this right.

i have been tempted to use mmap actually... btw, do you know if there is a Linux analog to the windows #pragma data_seg(".shared_stuff") mechanism? it is a great alternative to memory mapped files in windows. [http://msdn.microsoft.com/en-us/library/h90dkhs0(VS.80).aspx]

i'd prefer to use default memory allocation tho, if possible as my program is quite complex and uses -=many=- small objects.

any ideas?

cheers
Jono




Last edited on
closed account (S6k9GNh0)
You could always push memory to the stack and then make the pointers accessible to both libraries... I dunno besides that.
yes, i'm passing a structure to an exported init function, which includes a reference back to main(). it isn't a problem to share the addresses, i just want to know when or if the objects they refer to become invalid if the library goes out of scope.

for example, if an object referred to a shared library function or an object created on the stack by the library, it would be in trouble.

i want to know what other issues of that kind there are.

sorry if i'm being a bit obscure...
Topic archived. No new replies allowed.