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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:21:01+00:00 2026-05-29T04:21:01+00:00

I am working on a library where users should be able to use static

  • 0

I am working on a library where users should be able to use static global instances. These instances (being run before main) register themselves in another global vector which can then be used…

Currently, it goes somewhat like this…

class A;

std::vector<A*> v;

class A {
public:
    A (int i) : i(i) {
        v.push_back(this);
    }

    int get () const {
        return this->i;
    }
private:
    int i;
};

A a(1);
A b(2);

int main ()
{
    for (A* const& c : v)
        std::cout << c->get() << std::endl;

    for (std::vector<A*>::iterator i = v.begin(); i != v.end(); i++)
        delete *i;

    return 0;
}

However, I’m afraid this code will leak… even more so when I don’t want users to explicitely delete the contents of the vector (they’ll forget it anyway), it should happen automatically at the end of main.

Are there other solutions? I wanted to use a vector of std::unique_ptr, but apparently they don’t work that way…

  • 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-05-29T04:21:02+00:00Added an answer on May 29, 2026 at 4:21 am
    1. Your code doesn’t leak. The only thing that allocates memory there is std::vector, and it cleans itself up on destruction.
    2. a and b are correctly destroyed and released at the end of execution.
    3. You shouldn’t delete things that weren’t allocated with new.

    Also, you are relying on v being initialised before a and b are constructed. You should lazily initialise v instead (see below).

    std::vector<A*>& global_v()  
    {  
        static std::vector<A*> v;  
        return v;  
    }  
    
    // use global_v() instead of v in the A constructor.
    

    To get what you want, just remove the loop that deletes those objects.

    Here’s your code running in action with the loops removed and some debug output added. Notice that both objects are destroyed correctly.

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

Sidebar

Related Questions

I'm working on an SDK, distributed as a static library, for iOS developers to
I'm working on a thread library which implement user level threads (i have something
I am working on a client-server application that uses boost::serialization library for it's serialization
I'm working on a source-code visualization project that uses the Processing core library. The
I am trying to get a multiple upload library working for my Codeigniter based
I'm trying to get the Boost library working in my C++ projects in Eclipse.
I'm working using scriptaculous library. However I'm facing some issues with inclusion of the
I'm working on a Java library and would like to remove some functions from
I'm working on a piece of library code around IDisposable . The managed path
I'm working on a Python library that interfaces with a web service API. Like

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.