C-Scholl20
C-Scholl20

Reputation: 377

C++ shared object initialization constructor destructor

I'm working on a Shared Object compiled with g++ that serves as a logging API. I've written a little utility to exercise the library, but I've found that the shared object constructor/destructor are called every time the program starts and exits. I'd like to be able to maintain the state of any variables/objects in the .so file, even if there are no active applications linked. Is there a mechanism for accomplishing this?

Upvotes: 0

Views: 117

Answers (1)

Employed Russian
Employed Russian

Reputation: 213386

I'd like to be able to maintain the state of any variables/objects in the .so file, even if there are no active applications linked. Is there a mechanism for accomplishing this?

In a modern OS, everything that is in memory "belongs" to one or more processes, and there is no such thing as ".so without an active application".

In addition, when your .so is linked into several active applications (running processes), there is still no shared state that your library can record -- each instance of your .so will have no way of observing that other instances of that .so are active in other processes, except by using some IPC mechanism.

You could create a shared memory segment using shmat, and record shared state there. That state would also persist until explicitly removed via smtctl system call.

Beware: using shared memory this way is fraught with complications, and is very likely an overkill for a logging API library.

Upvotes: 1

Related Questions