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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:03:05+00:00 2026-05-19T17:03:05+00:00

Possible Duplicate: deleting memory allocated in static function in C++ Hi All, I have

  • 0

Possible Duplicate:
deleting memory allocated in static function in C++

Hi All,

I have C++ class as follows

interface myInterface;


class anotherClass : public myInterface {};

class myClass {
   private:
    myClass() {}
    ~myClass() {}

    typedef std::map<string, myInterface* > stringToClass;
        static stringToClass s_stringToClass;

    public:
         static myInterface& getStringToclass(string name);

};

in above class for getStringToClass defintion is as follows

 myInterface& myClass::getStringToClass(string name) {
     stringToClass::iterator iter;
     iter = s_stringToClass.find(name);

     if(iter == s_stringToClass.end()) {
          typedef stringToClass::value_type stringToClassPair;
          anotherClass* pothClass = new anotherClass();
          s_stringToClass.insert(stringToClassPair(name, pothClass));
          return pothClass;
     }
     else {
        return iter->second;
     }
}

now my question is we are allocating memory in static function and returning a pointer of class type, but here i want to retrun a reference as we don’t want to give control of pointer to user. And also i don’t want to delete immedetily unless user asked to or want to delete at end of program How can we delete memory? As there is only instance of class will be there, as there are only static functions.

Thanks for the help.

  • 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-19T17:03:05+00:00Added an answer on May 19, 2026 at 5:03 pm

    Note:
    You should reformat on the old thread, instead of creating new question. Anyway, I paste my answer here.

    I think in your case, the destructor won’t help, because there is no any object of MyClass.

    I propose three ways
    1. Don’t store pointer, store the object itself.
    2. Put the delete function into atexit; In your case

    class MyClass
    {
    .....//Your already existing code
    static void Destroy()
    {
     //iterate s_StringToClass and delete them
    }
    
    static void getStringToClass( string name )
    {
     struct DestroySetter
     { 
          DestroySetter()
          { 
               atexit( MyClass::Destroy );
          }
     };
     static DestroySetter setter; //Setup the destoyer
     //Your existing code here
    }
    
    1. Use smart pointer to manage the resource, shared_ptr is recommended.
      Though I put a lot in second way, I suggest the 3rd way.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.