static link stdc++ without STB_GNU_UNIQUE cause memory leak when dlclose
Asked Answered
E

0

2

I have to make a dso that static link stdc++ and need can unload from memory dynamic. So I tried with compile gcc with --disable-gnu-unique-object and use gold link with -Wl,--no-gnu-unique options. But both contains memory leak issue even I do nothing except call dlopen() dlclose() in main. The test code like:

int main()
{
    for(int i=0;i<1000;i++)
    {
        void * h=dlopen(filepath);
        if(h)
             dlclose(h);
    }
    return 0;
}

Than I checked the memory cat /proc/pid/maps before and after I found only heap changes bigger and bigger every time. About 90M after 1000 time call dlopen & dlclose to me 90M is still too big.

026fb000-0274e000 rw-p 00000000 00:00 0                                  [heap]

after googled about 2 weeks but nothing helpful for this issue. Only find a document said as below here.

-fno-gnu-unique On systems with recent GNU assembler and C library, the C++ compiler uses the "STB_GNU_UNIQUE" binding to make sure that definitions of template static data members and static local variables in inline functions are unique even in the presence of "RTLD_LOCAL"; this is necessary to avoid problems with a library used by two different "RTLD_LOCAL" plugins depending on a definition in one of them and therefore disagreeing with the other one about the binding of the symbol. But this causes "dlclose" to be ignored for affected DSOs; if your program relies on reinitialization of a DSO via "dlclose" and "dlopen", you can use -fno-gnu-unique.

Is this a linux bug? Is there anyone can help me on this issue? thanks. The environment is gcc 5.3.1. I linked with definitions _GLIBCXX_USE_CXX11_ABI.

Evertor answered 18/5, 2018 at 0:50 Comment(2)
Is anyone can help me on this issue?Evertor
--no-gnu-unique et. al. must be used on the DSO which is being loaded, at the cost of violating some C++ guarantees, which are implemented with STB_GNU_UNIQUE typed symbols.Stagemanage

© 2022 - 2024 — McMap. All rights reserved.