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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:36:02+00:00 2026-06-06T05:36:02+00:00

I have a class with a static factory constructor which returns a pointer to

  • 0

I have a class with a static factory constructor which returns a pointer to the object created.

I have to declare the object as a static object inside a namespace but I don’t know how to delete it correctly

class Foo
{
   public:
   Foo(int, int* );
   virtual ~Foo();
   static Foo* MyFooInitializer(int n )
   {
      int *p = new int[n];
      for (int i=0; i<n; i++)
         p[i]=i;

      Foo *ret = new Foo(n,p);
      delete p;
      return ret;
   }
   int someFooFunction(int a);
}

Then in my namespace I have a static inline function

namespace MyNamespace
{

    static inline void  myfunction()
    {
        static Foo  *foo1 = Foo::MyFooInitializer(10); 
        int y = somevalue();
        int x = foo1->someFooFunction(int y);
    } 
}

I obviously have a memory leak here because the object is never deleted.

The important fact is that I need that foo1 is declared as static because once created it must be the same object during all the program and must be unique (it keeps track of some variables).

Probably this is a design problem, but I don’t know how to delete it when my program exits or when I explicitly want to delete it to reinitialize it.

SOLUTION:

I modified the body of MyFooInitializer this way:

   static Foo* MyFooInitializer(int n )
   {
      int *p = new int[n];
      for (int i=0; i<n; i++)
         p[i]=i;

      static Foo ret = Foo(n,p);
      delete[] p;
      return &ret;
   }

This allows me to release all the memory correctly when the program terminates. Valgrind says all the heap memory is freed!

  • 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-06T05:36:04+00:00Added an answer on June 6, 2026 at 5:36 am

    There is no need for allocating that Foo on the heap here:

    static Foo* MyFooInitializer(int x) {
        static Foo some_foo(x);
        return &some_foo;
    }
    

    There are no leaks in that code, that Foo will be destroyed when your program ends.

    Note that if the pointer returned by MyFooInitializer actually points to some class which inherits from Foo, then you’d just have to use the derived type for the static variable:

    static Foo* MyFooInitializer(int x) {
        static SomeFooDerived some_foo(x);
        return &some_foo;
    }
    

    Edit: Since you provided the actual function body, my answer is valid. You’d do it like this:

    static Foo* MyFooInitializer(int n ) {
       // Don't know what this p is, anyway...
       int *p = new int[n];
       for (int i=0; i<n; i++)
          p[i]=i;
    
       static Foo ret(n,g); // what is g?
       delete[] p; // smart pointer plx
       return &ret;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class with static method which has a local static variable. I
I have a class which has some unit tests, but when I am running
I have created a File class, which takes care of all operations on files,
I have a factory class that decides which of four available subclasses it should
I have class A such that: class A { static int i; A(); f1();
I have a class with static members. I want to get a list of
I have this class: public static class CsvWriter { private static StreamWriter _writer =
say I have: class Test { public static int Hello = 5; } This
If I have a class called Test :: class Test { static std::vector<int> staticVector;
Let's say I have a class (not a static class), A , that in

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.