Search code examples
c++linuxmemoryshared

Shared library can not access memory of a program


Sorry for my English. I have created a shared library and my program loads this dynamically. It works ok, but the problem is that I pass a pointer to the shared library, but this can not access the pointer memory. In the other way there are no problems.

Any ideas on how to solve this?

Note: I use linux, but if the idea works on other OS I will search for this equivalent

Here the code:

unsigned analizer::analizeText() {
std::cout << "thread started" << std::endl;
while(true)
{
    pause.lock();
    varMu.lock();
    unsigned enable = __enable;
    unsigned line   = __line;
    unsigned _colum = __colum;
    unsigned offset = __offset;
    varMu.unlock();

    unsigned begin = offset;
    std::cout << "get usafed docs" << std::endl;
    std::cout << doc->getText() << std::endl;//the program crash
    vector<document*> doc = _openDocuments->getUnsavedDocs(_openDocuments->traductDoc(this->doc)); std::cout << "relized" << std::endl;
    while (beginElements.find(_openDocuments->getChar(begin-l)) == beginElements.end() and begin > 0)
        --begin;

    word = this->doc->getText().substr(begin,_offset-begin);

    delete UunFile;

load library:

#include <iostream>

pBear::language *_language;

using namespace pBear;

void language::load()
{
    boost::filesystem::directory_iterator end;
    for (boost::filesystem::directory_iterator it(languagePath); it != end; ++it)
    {
        void *hndl = dlopen(it->path().c_str(), RTLD_NOW);
        if(hndl == NULL)
        {
            std::cerr « dlerror() « std::endl;
             exit(-1);
        }
        std::function<LanguageDatas*(pBear::openDocuments*)> fun = (LanguageDatas*(*)(pBear::openDocuments*)) dlsym(hndl, "colorMaker");
        LanguageDatas *dat = fun(openDocuments);
        dataLanguages[dat->getName()] = dat;
    }
}

pass the pointer:

ana = _language->getLanguage(doc->getProyect()->getLanguage())->getAnalizer(doc);

Last messages of the program:

analize

thread started

get usafed docs

Segmentation fault (core dumped)


Solution

  • In:

    std::function<LanguageDatas*(pBear::openDocuments*)> fun = (LanguageDatas*(*)(pBear::openDocuments*)) dlsym(hndl, "colorMaker");
    LanguageDatas *dat = fun(openDocuments);
    

    It looks like you try to find a C++ function with name colorMaker in that shared library. And then you invoke the result of dlsym without ever checking it for NULL.

    It can be that dlsym returns NULL, hence the segmentation fault.

    It is suspicious that a C++ function has a C-style name colorMaker. Double-check the symbol names in that shared library.

    dlsym never throws C++ exceptions, so you need to check its return value:

    LanguageDatas(*fun)(pBear::openDocuments*) = static_cast<(LanguageDatas(*)(pBear::openDocuments*)>(dlsym(hndl, "colorMaker"));
    if(!fun)
        throw std::runtime_error("colorMaker function is not found");
    fun(openDocuments);
    

    Note, that you do not need to wrap the function pointer into std::function in order to invoke it.