Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1112579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:44:02+00:00 2026-05-17T02:44:02+00:00

plugin1.cpp: #include <iostream> static class TestStatic { public: TestStatic() { std::cout << TestStatic create

  • 0

plugin1.cpp:

#include <iostream>

static class TestStatic {
public:
  TestStatic() {
     std::cout << "TestStatic create" << std::endl;
  }
  ~TestStatic() {
     std::cout << "TestStatic destroy" << std::endl;
  }
} test_static;

host.cpp

#include <dlfcn.h>
#include <iostream>
int main(int argc,char *argv[]) {
   void* handle = dlopen("./plugin1.so",RTLD_NOW | RTLD_LOCAL );
   dlclose(handle);
   return 0;
}

build and run:

>g++ -c plugin1.cpp -o plugin1.o -fPIC
>g++ -shared plugin.o -o plugin1.so
>g++ host.cpp -o host -ldl
>./host
>TestStatic create
>Segmentation fault

why TestStatic::~TestStatic called at ‘exit()’ but not at ‘dlclose()’ ?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T02:44:03+00:00Added an answer on May 17, 2026 at 2:44 am

    The C++ Standard requires that destructors be called for global objects when a program exits in the opposite order of construction. Most implementations have handled this by calling the C library atexit routine to register the destructors. This is problematic because the 1999 C Standard only requires that the implementation support 32 registered functions, although most implementations support many more. More important, it does not deal at all with the ability in most implementations to remove DSOs from a running program image by calling dlclose prior to program termination.

    This problem is addressed in later versions of GCC including C/C++ standard library and linker. Basically, C++ destructors should be registered using __cxa_atexit function instead of atexit (3).

    For the full technical details on __cxa_atexit, see Itanium C++ ABI specification.


    It is not clear from your question what version of the gcc, linker and standard C library you are using. Plus, the code you have provided does not meet POSIX standard as there are no RTDL_NOW or RTDL_LOCAL macros defined. They are RTLD_NOW and RTLD_LOCAL (see dlopen).

    If your C standard library does not support __cxa_atexit, you may need to disable it by specifying -fno-use-cxa-atexit gcc flag:

    -fuse-cxa-atexit

    Register destructors for objects with static storage
    duration with the __cxa_ atexit
    function rather than the atexit
    function. This option is required for
    fully standards-compliant handling of
    static destructors, but will only work
    if your C library supports
    __cxa_atexit.

    But that might result in a problem where destructors are called in different order or not called at all. So the best solution in case of broken __cxa_atexit support or no support at all is not to use static objects with destructors in your shared libraries.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which works as expected: #include <iostream> using namespace std;
compiler says: Symbol 'std' could not be resolved Symbol 'cout' could not be resolved
// InternalTemplate.cpp : Defines the entry point for the console application. // #include stdafx.h
When I try to create a new class in Eclipse using the CDT plugin
main.cpp <- line 106 to 164 invoke dlopen/dlsym/dlclose basefilter.hpp <- naked abstract base class
I'm building a plugin using Firebreath. I made a personal method in ABCPluginAPI.cpp called
So I have a directory tree as follows: pluginlist.py plugins/ __init__.py plugin1.py plugin2.py ...
The plugin base class in Play 1, is play.PlayPlugin , which has a lot
How do I properly include libraries in C++? I'm used to doing the standard
I cant understand why in the class constructor I can call this function but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.