-1

I have a requirement in which I have to use a singleton class, acting as a database to store some values. I have also declared a run function in the cpp file of this singleton class in order to change the data and print. Then, I create a shared object of this class using g++ -fPIC -shared -o lib1.so ClassA.cpp. I try to load the library and execute the run function, but a runtime error is generated due to undefined symbol. This is due to the fact that I am not declaring my static variable as nullptr in the implementation of the singleton class, rather I am trying to do it in main. The sole purpose of doing this is keep this singleton object alive in the memory of main process, so that subsequent dynamic libs which are loaded can access the same object. Is there any better way to do this? Here is my current implementation:

// ClassA.h
#ifndef _CLASSA_
#define _CLASSA_


class ClassA{
    private:
        int integer;
        static ClassA* classA;
        ClassA(){}

    public:
        static ClassA* getOrCreate();
        ~ClassA();
        int getInt();
        void setInt(int& a);
};


#endif // _CLASSA_

// ClassA.cpp
#include "ClassA.h"
#include <iostream>

ClassA* ClassA::getOrCreate() {
    if (classA == nullptr) {
        classA = new ClassA();
    }
    return classA;
}

ClassA::~ClassA() {
    delete classA;
}

int ClassA::getInt() {
    return integer;
}

void ClassA::setInt(int& a) {
    integer = a;
}


extern "C" void run() {
    int intin = 42;
    ClassA::getOrCreate()->setInt(intin);
    std::cout << "Value set: " << ClassA::getOrCreate()->getInt() << std::endl;
}

// main.cpp
#include <iostream>
#include <dlfcn.h>
#include "ClassA.h"


ClassA* ClassA::classA = nullptr;
int main() {
    void* handle = dlopen("./lib1.so", RTLD_LAZY);
    if (!handle) {
        std::cerr << "Cannot open library: " << dlerror() << std::endl;
        return 1;
    }
    typedef void (*run_func)();
    dlerror();
    run_func run = (run_func) dlsym(handle, "run");
    char* error = dlerror();
    if (error != NULL) {
        fprintf(stderr, "%s\n", error);
        dlclose(handle);
        exit(EXIT_FAILURE);
    }

    run();
    dlclose(handle);
    return 0;
}

13
  • What are you trying to do exactly? Do you need to have the singleton alive even if the library gets unloaded? If that is the case then maybe it would be easier to just static-link that DSO in main (that is, make is a dependency so it gets loaded at program start)? Commented Mar 31 at 8:07
  • @AndreyTurkin, Yes I want the singleton and all the objects pointed by the pointers inside the singleton alive, even if the library gets unloaded, so that the next library can read and write on same data. I can't go with the static linking approach as the libraries linked are based on the user input, and I can't link all the libraries statically. Commented Mar 31 at 8:12
  • Not sure I understand the scenario, but isn't it enough to call getOrCreate() in main and store it in a variable ? Commented Mar 31 at 8:29
  • 2
    @TejasSharma why do you have to unload the library in main ? You can keep it loaded for as long as you need the singleton to stay alive. Commented Mar 31 at 8:55
  • 2
    Read carefully. .fini is responsible for destruction of objects with static storage duration. It cannot possibly destroy heap allocated objects. It doesn't know where these objects are. Commented Mar 31 at 17:55

1 Answer 1

1

I want the singleton and all the objects pointed by the pointers inside the singleton alive, even if the library gets unloaded, so that the next library can read and write on same data

Dynamically allocated memory is not affected by dynamic loader but you do need some kind of a "root" pointer to keep a pointer to it. In your implementation it is classA. It can't be a static variable of your library if it gets unloaded since its value would be lost. So your options are limited:

  • don't unload the library, ever (either make it statically-dependent from main executable or dload using RTLD_NODELETE) - you can then use its static memory since it never goes away
  • don't store it in the library - that is your current approach; fairly OK solution if you absolutely must unload that library
  • if you must unload the library and if you really don't want to make that "reverse" dependency and pollute main application with library details then you can make a helper DSO that just holds the singleton pointer (or whatever else you need persisted) and load THAT helper from your library with RTLD_NODELETE
Sign up to request clarification or add additional context in comments.

2 Comments

Let’s say I can keep all the libs alive in the main, but can we access that same singleton class object which was used to populate the data, in the function of any other library?
@TejasSharma Your existing code has getOrCreate defined in a library; all other libraries use it from there. I.e. its symbol is a global defined in a library, and all the others refer to it as an undefined global (note: it ought to be explicitly made global/default visible as usually done with DSO symbols). You could use that accessor to access the singleton and then your singleton variable can just be a local static (which would be alive as long as the containing library is alive so singleton would work as expected).

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.