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 8278699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:06:07+00:00 2026-06-08T09:06:07+00:00

I have two shared libraries: mylib and loglib (names are changed). Both have destructor

  • 0

I have two shared libraries: mylib and loglib (names are changed).
Both have destructor functions (extension of gcc).
The destructor function of mylib requires functions of loglib. This way:

mylib.c from libmy.so:

void __attribute__ ((destructor)) mylib_destructor()
{
    loglib_write_log("destructor");
}

loglib.c from liblog.so:

void loglib_write_log( const char* txt )
{
   fprintf( log_file, "%s\n", txt );
}

void __attribute__ ((destructor)) loglib_destructor()
{
    if( log_file )
    {
        fclose( log_file );
        log_file = NULL;
    } 
}

As you can see, problems occurs if loglib_destructor() is called before mylib_destructor():
fprintf will get NULL pointer parameter.

I can not change loglib.c.

How can I ensure that mylib_destructor is called before destructors of other libraries?

I don’t want to set highest priority for mylib_destructor, because users of mylib may want to use even higher priority for own destructors.

  • 1 1 Answer
  • 0 Views
  • 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-06-08T09:06:08+00:00Added an answer on June 8, 2026 at 9:06 am

    From the gcc docs:

    You may provide an optional integer priority to control the order in which constructor and destructor functions are run. A constructor with a smaller priority number runs before a constructor with a larger priority number; the opposite relationship holds for destructors. So, if you have a constructor that allocates a resource and a destructor that deallocates the same resource, both functions typically have the same priority. The priorities for constructor and destructor functions are the same as those specified for namespace-scope C++ objects (see C++ Attributes).

    Have you tried this?

    It doesn’t have to be the highest priority for mylib, just large enough. If it’s documented, users of your library can rely on it.

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

Sidebar

Related Questions

I have two scenarios. Suppose I have 3 shared libraries that export C++ symbols,
I have two shared libraries in Linux that when built are identical in all
Let's say I have two iPhone apps with shared core functionality. The apps isn't
I have a partial view that is shared between two controllers and I'm trying
Two Windows processes have memory mapped the same shared file. If the file consists
I have two separate applications (both part of the same system) that share a
I have two tables, Users and People, both of which share a common attribute,
Lets say I have two shared variables - a and b - that are
I have two projects in the same Subversion repository. They both use some standard
Example: I have two shared objects (same should apply to .dlls). The first shared

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.