problem with dlclose and destructors of global objects

Dominik Siatkowski dominiks@aldec.katowice.pl
Wed May 12 10:37:00 GMT 2004


Hello.

I'm using gcc 3.3.2.

Here is simple example for problem that I encoutered.

test.cpp:

#include <dlfcn.h>

#include <stdio.h>

class klasa {

int m_a;

public:

klasa() {}

~klasa() {printf("~klasa\n");}

void set(int a) {m_a = a;}

int get() {return m_a;}

};

klasa Objekt;

int main(int a, char** b)

{

const char* lib = "libscaner.so";

Objekt.set(a);

if (a > 1)

lib = b[1];

void* h = dlopen(lib, RTLD_LAZY);

if (h == NULL) {

printf("Error on loading %s: %s", lib, dlerror());

return 1;

}

a=Objekt.get();

printf("loaded\n");

dlclose(h);

printf("closed\n");

return 0;

}

-------------------- end of test.cpp

testso.cpp:

#include <stdio.h>

class klasa {

int m_a;

public:

klasa2() {}

~klasa2() {printf("~klasa2\n");}

void set(int a) {m_a = a;}

int get() {return m_a;}

};

klasa2 Objekt2;

int dummy(int a) {

Objekt2.set(a);

return Objekt2.get();

}

-------------------- end of testso.cpp

I compile both files with command:

gcc -o test test.cpp -ldl -lstdc++

gcc -o libdupa.so testso.cpp -shared -fPIC

with gcc 3.3.2 on host with glibc 2.2.5.

When I run this test on the same host (and other with glibc 2.2.5 or 2.2.4)
all works ok:

$> ./test libdupa.so

loaded

~klasa2

closed

~klasa

But if I run the test on host with glibc 2.3.2 destructor of klasa is called
on dlclose:

&> ./test libdupa.so

loaded

~klasa

~klasa2

closed

Problem does not occur when I compile this test case on host with glibc
2.3.2.

Whats the problem? Is this gcc or glibc issue?

Is the problem fixed on newer gcc?

Regards

Dominik Siatkowski




More information about the Gcc-help mailing list