25

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 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.

Ell
  • 4,238
  • 6
  • 34
  • 60
  • "*classes aren't first class objects in C++*" Now **that's** a new one... – ildjarn Jul 27 '11 at 19:23
  • Oh dear, read my fundamental flaw bit – Ell Jul 27 '11 at 19:24
  • Wasn't criticizing, just observing. ;-] – ildjarn Jul 27 '11 at 19:25
  • @Ell: If you're new to C++, the C++ community here on Stack Overflow can recommend [some good introductory C++ books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – In silico Jul 27 '11 at 19:27
  • in what case would you have dynamically allocated static data (sounds like a contradictio in terminis to me)? – KillianDS Jul 27 '11 at 19:27
  • 3
    @ildjam: I am not sure that classes are first class objects in C++. Indeed, they cannot be treated like object, i.e., created, modified, etc. They can only be instantiated. Classes are first class object in other languages: ruby, python, javascript, objc... – sergio Jul 27 '11 at 19:27
  • haha, okay :) Is it true, incorrect or does it just not make sense? :O – Ell Jul 27 '11 at 19:28
  • About the dynamically allocated static data, I wouldn't use the stack to store a large amount of data because that could cause a StackOverflow, couldn't it? – Ell Jul 27 '11 at 19:29
  • @sergio : That depends who's definition of "first-class object" you're using. If you're using e.g. Smalltalk's definition, sure; however, this is C++, not e.g. Smalltalk, and by C++'s definition, classes most certainly _are_ first-class. (Interesting that you exclusively picked dynamic languages as your examples... ;-]) – ildjarn Jul 27 '11 at 19:43
  • I thought first-class was independant of language? I meant it by Ruby's defenition, as in a class is an object of type class... hmm okay nevermind I'v confused myself :O. But now I understand the pointer is static, not the data it points to! Thanks guys :) – Ell Jul 27 '11 at 19:52
  • @Ell : The definitions of "object" and "object-oriented" vary substantially between different languages, and consequently cause lots of religious wars about what _really_ means what. :-P – ildjarn Jul 27 '11 at 19:59
  • 3
    @ildjarn: in theoretical CompSci, the question "are types objects themselves" is a well-understood question. The answer to that question in C++ is no. The closest C++ has is `typeid()` / `std::type_info`, which is an object _describing_ a type. – MSalters Jul 28 '11 at 07:43

6 Answers6

18

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.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
15

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.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • So even though I new it, I don't need to delete it? So thats an exception to the rule that there should be one delete for every new? – Ell Jul 27 '11 at 19:27
  • 1
    @Ell: the `static` keyword means that the data 'lives' in the class. `new` creates a new instance of the class (an object) that does not contain any of the static data, instead that object knows its class and accesses its class's data whenever it's needed. – Chris Pfohl Jul 27 '11 at 19:30
  • 5
    @Ell: If your static object is a pointer to dynamically allocated memory, you *should* delete it, but oftentimes it is not strictly necessary. Modern systems will reclaim shortly after you would have freed it anyhow. The pointer itself will continue on either way, and it is the pointer that is static, not the object it points to. – Dennis Zickefoose Jul 27 '11 at 19:33
8

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.

Andrew
  • 5,839
  • 1
  • 51
  • 72
ks1322
  • 33,961
  • 14
  • 109
  • 164
  • Thank you, this was very helpful but I have marked James' answer as accepted because it informs me that Im making the mistake that it was the pointer that is static, not the data. Thanks :) – Ell Jul 27 '11 at 19:54
0

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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
0

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

cprogrammer
  • 5,503
  • 3
  • 36
  • 56
0

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();
Kemin Zhou
  • 6,264
  • 2
  • 48
  • 56