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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:28:36+00:00 2026-06-08T23:28:36+00:00

Suppose I have an shared library file (say libtemp.so) which has a global variable.

  • 0

Suppose I have an shared library file (say libtemp.so) which has a global variable. If I dynamically load this library, assign a heap memory to it, and then close the library. If I load the library again, then is the old heap memory leaked? (I think its true since global variables will be reset when the library is loaded again)

Is it a bad practice to assign heap memory to a global variable? Are there any cases where we end up having to do this?

  • 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-08T23:28:38+00:00Added an answer on June 8, 2026 at 11:28 pm

    On Linux, the library global variable sits in some data segment of the shared object, and that segment would have been munmap-ed when dlclose-ing the libtemp.so and would have been mmap-ed again at the next dlopen so the global would have been reinitialized.

    (I am assuming you are doing only one single dlopen i.e. that the libtemp.so is not dlopen-ed twice)

    BTW, you could do initialization in constructor functions (and finalization in destructor), see function attributes in GCC. The constructors are executed at dlopentime, and the destructors at dlclose time. Read also Linux dlopen(3) man page for details. (Notice that POSIX dlopen don’t have these tricks)

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

Sidebar

Related Questions

Suppose we have a shared library named libtest.so, there is one function foo in
Possible Duplicate: Are file descriptors shared when fork()ing? Suppose I have following code in
I have a shared library which is supposed to export only one function which
Suppose I have an SConstruct file that looks like this: env = Environment() env.Program(a,
Suppose we have a shared queue (implemented using an array), which two threads can
Suppose that I compiled a dynamic library (Windows DLL and/or Linux shared object file,
Suppose my application is using a library object Server which has a callback onConnected
I have problems using a shared library that was linked against a shared library.
Suppose we have a class like: Public Class Question Private Shared _field as Integer
I have two scenarios. Suppose I have 3 shared libraries that export C++ symbols,

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.