3

TL;DR: Is it possible to load a class object from a library at runtime, close the library and then use the object as a "normal" object (after closing)?

I am trying to implement a plug-in system with some sort of "hot swap" functionality. Suppose my program expects a doSomething() function from its plugins. My idea would be to scan the fileystem for any libs in a specific folder, extract the functions and then close the lib (before using the functions!). This way, a monitor thread could just monitor changes on the filesystem and reset the function pointer in case something changed and thus plug-ins could be "hot swapped".

I believe that the function pointer would become invalid as soon as I close the library (Is that so?). Therefore my idea is to let the library return a copy of an object which does the desired functionality. In this case, I would call the lib to create the object before closing it and save the copy of the object in my program. However, since the object can use other objects/functions of the library, I am not sure if this would work, since these objects/functions would not be available, would they?

3
  • possible duplicate of Copy a function in memory and execute it Commented Feb 19, 2014 at 17:45
  • I think this is effectively asking if he can copy the function's code into his own program's memory so he does not have to use a system linker, effectively writing his own linker with more functionality... Commented Feb 19, 2014 at 17:46
  • Not really in my opinion, but thanks for the link! Since I do not know how complex a plugin could be, I have no real idea what exactly I would have to load into the memory and how it has to be structured etc. Commented Feb 19, 2014 at 18:05

2 Answers 2

4
  1. You cannot copy the object and close the library, since only data, but not the code of those objects is copied. Instead of it OS loads code of the library to the memory and all function pointers points to this region of memory. What will be if OS unloads the library?

  2. You can implement something like this. You can have a Proxy object that contains a pointer to current loaded implementation. If a new library is detected, you can load new library, create instance of a new implementation, delete old instance of implementation, close old library. In this way you implement a "hot swap" mechanism and avoid problem with shared libraries code.

  3. If you chose way described in item 2, beware of concurrency problems (what if another thread is scheduled when old implementation is deleted, but before the pointer is changed?).

Sign up to request clarification or add additional context in comments.

6 Comments

Hey, so I suppose, that dlopen/loadLibrary do not lock the library (leading to messages like "File X is used by program Y")? Once the lib is loaded, the whole code/data lies inside the memory and no reference is left to the actual file?
@user1228633 When dlopen/loadLibrary loads the library into the memory, code in the library file is used for virtual memory mechanism. If there are small amount of free physical memory OS can unload some pages from the memory that contains your library code. But if code in the library is used again OS will read unloaded parts of the library from the file and load them back. Of course this works if the library was not closed.
@user1228633 When OS loads library into the memory the memory consumed by the library is used for global data and code. Data of objects you create will be placed in other parts of memory, while all function pointers will point to chunk of memory dedicated for the loaded library.
Just to be clear about this: This does not enable hot replacing of the same file, does it? Given a lib lib1 used by my program, the user cannot just copy/past a newer version of lib1 into the plugin folder, since my program may load data from lib1 at any time.
@user1228633 I very-very much doubt it and pretty much sure that this is impossible. What you can do is subscribe to filesystem events like file creation and if you receive a notification that there is a new file in some predefined folder, try to load it. For monitoring filesystem events please refer to en.wikipedia.org/wiki/Inotify or more high-level mechanisms on your platform.
|
1

An object is data, not code. A copy of an object is a copy of the data, but it still refers to the original code. As soon as you unload a dynamic library, its code is gone from memory, and any objects still referencing that code (i.e. of a type provided by the library) will be in trouble as soon as they are asked to execute a member function (such as the destructor).

So no, it's not possible to unload a library and keep using its code.

1 Comment

But on a more positive note, it is possible to load a library, use it and, once all it's work is done & all relevant objects deleted, then unload the library. I don't see why what you want means you have to unload the library before using it, so that might well work for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.