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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:02:21+00:00 2026-06-13T14:02:21+00:00

Caveat: This question arose because I have to work with a huge pile of

  • 0

Caveat: This question arose because I have to work with a huge pile of awful code, without proper documentation, written by someone else as a research project 6 years ago. Obviously a better solution is not to cause these issues in the first place, with proper design…

That said the question is: What is the best way to get out of a situation like this:

  1. A class allocates memory on the heap, and frees it in the destructor.
  2. Somewhere, an instance of the class is declared at global scope.
  3. A function exists that initializes this instance.
  4. That function’s return value is used to initialize a static variable.
  5. The globally scoped variable is used outside of the static scoping.

Minimal working example:

File “myclass.h”:

#ifndef MYCLASS_H
#define MYCLASS_H

#include<vector>
using namespace std;

class myclass{
        vector<int> *onTheHeap;
public:
        myclass(int len=0){
                onTheHeap = new vector<int>(len);
        }

        ~myclass(){
                delete onTheHeap;
        }

};

#endif

file “static_loader.cpp”

#include "class.h"
myclass existsForever;

int cause_static_global_creation(){
        existsForever = myclass(5);

}

static int bootstrap = cause_static_global_creation();

and file “main.cpp”:

#include "class.h"

extern myclass existsForever;

int main(){
        return 0;
}

Build with:

g++ -g -c static_loader.cpp
g++ -g main.cpp static_loader.o

and run as:

 valgrind --leak-check=full ./a.out

Result: The variable is freed when its destructor is called in the exit handler’s below main, but also in the static_initialization_and_destruction_0 function below main from static_loader!

Is there a way to ensure that these variables are freed exactly once that doesn’t involve re factoring the code extensively? In the library I have to work with, there are several dozen instances of this pattern…

EDIT:

Adding functions:

    void operator=(myclass other){
            delete this->onTheHeap;
            this->onTheHeap = other.onTheHeap;
    }

and

    myclass(const myclass& other){
            this->onTheHeap = new vector<int>(*(other.onTheHeap));
    }

Does not change the behavior.

Second EDIT:

    myclass& operator=(const myclass& other){
            delete this->onTheHeap;
            this->onTheHeap = new vector<int>(*(other.onTheHeap));
            return *this;
    }

Resolves all the issues. My library has a memory leak with sources like this anyway, but I’m no longer sure how to reproduce it. At least it’s not this though, and thanks for the suggestions on refactoring etc. as well!

  • 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-13T14:02:23+00:00Added an answer on June 13, 2026 at 2:02 pm

    Your assumption is broken. myclass existsForever; is initialized not by cause_static_global_creation, but by myclass::myclass. Instead, cause_static_global_creation assigns a value to the already-initialized object.

    And as the class violates the Rule Of Three, it’s no surprise that the assignment causes issues.

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

Sidebar

Related Questions

[Caveat] This is not directly a programing question, but it is something that comes
This is a question concerning what is the proper way to synchronize a shared
First a caveat: I posted this question here on SuperUser, but it is clearly
I've seen this question about advice for C# programmers writing Python code but I
Caveat: I realize this is potentially a server configuration question, but I thought there
Variants of this question have been asked many times, but none of the answers
This is essentially a duplicate of this question but with one caveat: I want
Caveat : I'm not sure if this post might be better suited for SuperUser
Okay, this is just a minor caveat. I am currently working with the lovely
Here's the caveat... If you have a table that has a single column and

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.