This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Destructor never gets called.
- To: "'gcc at gcc dot gnu dot org'" <gcc at gcc dot gnu dot org>
- Subject: Destructor never gets called.
- From: "Liu, Guangsheng" <guangsheng dot liu at intel dot com>
- Date: Mon, 11 Jun 2001 17:25:16 -0400
Hi,
I have the following program with a global C++ object "aObj". The
destructor never gets called. The main thread creates 2 threads (t1, t2)
each of which there is just one printf() statement, then pthread_exit()
exits itself. After all the threads exit (t1, t2, main thread), I didn't
see Class A's destructor being called.
However, if main thread call pthread_join() waiting for t1, t2 finish.
The destructor is called.
I am using GCC 2.95.3. Redhat 6.2.
Build command: g++ -g -o th th.c -pthread
class A {
public:
A() {
printf("Constructor A called.\n");
}
~A() {
printf("Destructor A called.\n"); // NEVER gets called.
};
} aObj ;
void* send(void* ptr) ;
void* recv(void* ptr) ;
int main() {
pthread_t t1, t2 ;
pthread_attr_t attr;
printf("mainthread: pid = %d, threadid = %d\n", getpid(),
pthread_self());
if (pthread_attr_init(&attr) == -1) {
perror("pthread_attr_init");
}
if (pthread_create(&t1, &attr, send, 0 )) {
perror("pthread_create1-send");
}
if (pthread_create(&t2, &attr, recv, 0)) {
perror("pthread_create1-receive");
}
// pthread_join(t2, 0);
// pthread_join(t2, 0);
printf("*****main thread exit\n");
int ret = 1;
pthread_exit(&ret);
}
void* send(void* ptr) {
printf("Send:pid = %d, threadid = %d\n", getpid(), pthread_self());
}
void* recv(void* ptr) {
printf("Recv: pid = %d, threadid = %d\n", getpid(), pthread_self());
}