开发者

C++ Deleting Static Data

If I have a class that contains private static data allocated on the heap that never changes, when, if at all, should I delete it?

As I understand it, a class itself is never开发者_JAVA技巧 constructed (because classes aren't first class objects in C++) then there is no destructor to delete the static data in? Im new at C++ so sorry if my understanding of c++ is fundamentaly flawed or if the answer is obvious! Thanks in advance, ell.


If the data is static, it isn't allocated on the heap, and it will be destructed during the shutdown of the process.

If it is a pointer to the data which is static, e.g.:

Something* MyClass::aPointer = new Something;

then like all other dynamically allocated data, it will only be destructed when you delete it. There are two frequent solutions:

  • use a smart pointer, which has a destructor which deletes it, or

  • don't delete it; in most cases, there's really no reason to call the destructor, and if you happen to use the instance in the destructors of other static objects, you'll run into an order of destruction problem.


static data means, it persists the entire duration of the program.

However, if you use static in pointer as:

static A *pA = new A();

then you can delete this, by writing delete pA. But that doesn't invalidate my first statement. Because the object which is being pointed to by the static pointer is not static. Its the pointer which is static, not the object which is being pointed to by the pointer.


You can place this class in std::unique_ptr. Then it will be deleted automatically on program shutdown. Otherwise memory leak tools will complain, that your class leaks. On the other hand this memory leak is harmless because the program finished running.


I presume you're actually referring to a static pointer to an object on the heap?

This will never be deleted automatically, you must do it yourself. Most of the time it's sufficient to let the program end and the OS do the cleanup, unless you're using a memory checking tool or the destructor has side effects that you require.

The easiest thing to do is use a smart pointer, which will automatically delete the object when nobody is referring to it anymore. You can keep a copy of the pointer in main if there are times when nobody will have a copy, then the object will be deleted when main exits.


static data allocated on the heap means a member pointer that is static. If this is the case you can allocate memory to it.


I have used this solution is to add a static function to deallocate the memory. Just that somewhere in the shutdown phase the caller needs to call this function. I am not sure this practice is good or bad; at least it does clean up the memory allocated on the heap. Glad to hear comments.

class example {
   public:
      static void dealloThing() { delete ptr; }
      /**
       * You could have more elaborate arguments to set special version
       * of Thing, here not elaborating the details
       */
      static void setThing(Thing* tptr) { ptr = tptr; }
   private:
      static Thing* ptr;
};

==== example.cpp ===
Thing* example::ptr=new Example();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜