0

Im new to this and just wanted to ask a quick question about deleting objects.

I have an object called MyClass1 and from it I have a number of other classes, MyClassA, MyClassB etc.

Now should I do this in MyClass1:

MyClass1::~MyClass1()
{

delete MyClassA;
delete MyClassB;

}

Or will everything created in MyClass1 automatically be deleted when I delete MyClass1?

Also, if I have more objects created in MyClassA and MyClassB, will these also have to be deleted manually in their respective class?

Thanks

Kamil Klimek
  • 12,884
  • 2
  • 43
  • 58
panthro
  • 22,779
  • 66
  • 183
  • 324
  • `MyClass1` is a **class** not an **object**. You cannot `delete` classes, you can `delete` a pointer of an `object`. – masoud Jan 01 '13 at 21:30
  • Why does this question tagged as Qt's? This is typical C++ question. – NG_ Jan 02 '13 at 09:54

5 Answers5

4

If you're asking this, you're just learning C++, so the best advice is - neither. You should know about this stuff (dynamic allocation & memory management - see Guillaume's answer for this), but what you really should do is use RAII (google this). The proper C++ way of doing it would be:

struct  MyClass1
{
  MyClassA mA;
  std::shared_ptr<MyClassB> mB;

  MyClass1() : mB(new MyClassB)
  {

  }
};

See? No more destructor, which means you also don't need a copy constructor or copy assignment operator (which is where Guillaume's answer is flawed - it's missing the last two).

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 3
    For general c++ this is true... however, do you happen to know if using `shared_ptr` for automatic memory management plays nicely with `Qt`'s parent-child hierarchy that will `delete` children when the parent is destroyed? ([This question/answer](http://stackoverflow.com/questions/2491707/memory-management-in-qt) has more info about memory management and Qt.) – tmpearce Jan 01 '13 at 21:14
  • @tmpearce not familiar with Qt, but I'm assuming it's well documented. – Luchian Grigore Jan 01 '13 at 21:57
  • In general, you shouldn't be allocating stuff dynamically anyway. If `shared_ptr` works, the object probably shouldn't be allocated dynamically to begin with (which means you don't need `shared_ptr`). There are exceptions, but anything which uses `shared_ptr` is really somewhat advanced C++, and not something for a beginner. – James Kanze Jan 01 '13 at 22:24
3

call delete operator only if you have created your objects with new operator

struct  MyClass1
{
  MyClassA mA;
  MyClassB * mB;

  MyClass1()
  {
    mB = new MyClassB;
  }

  ~MyClass1()
  {
    delete mB;
  }
};
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
Guillaume Paris
  • 10,303
  • 14
  • 70
  • 145
  • okay, I have created a page with new. On that page is a number of buttons. Do I just need to delete page or all of the buttons aswell? – panthro Jan 01 '13 at 20:57
  • 1
    See "rule of three" - your example is flawed. – Luchian Grigore Jan 01 '13 at 21:03
  • @user1013512: Qt as a special manner to manage the memory, if you have created your QPushButton by passing your page's instance as argument, then you don't have to call delete for your QPushButton..Qt will delete them for you – Guillaume Paris Jan 01 '13 at 21:06
  • If you call `new` inside the constructor [or somewhere else in the class that isn't followed by a corresponding delete in the same function], you should use `delete`. If your buttons are not created with new [or something that calls new], then you don't need to `delete` them [of course doesn't mean that you may not need to do "something" to clean them up, e.g. "unregister my button from screen" or some such...] – Mats Petersson Jan 01 '13 at 21:08
  • @LuchianGrigore From his comments, it sounds like he is dealing with GUI components. In that case, he should be banning copy and assignment, rather than worrying about how to implement them. – James Kanze Jan 01 '13 at 22:25
  • @JamesKanze Anything subclassed from `QObject` already has [copy and assignment disabled](http://doc.qt.digia.com/qt/qobject.html#no-copy-constructor-or-assignment-operator) (from the tags at least, it seems like this is likely even though it isn't explicitly stated in the question). – tmpearce Jan 02 '13 at 04:01
1

You can't delete objects that aren't pointers because that's not the purpose of delete. It's meant to free dynamic memory associated with an object. That is, whatever is created with new must be deleted. You can have pointers to a class, and they can be deleted. But since nothing was allocated with new, there's no need to use delete. The class will in fact be destructed from memory at the end of the scope in which it is created. Those objects are allocated on the stack while dynamic memory is on the heap. Objects on the stack have automatic storage duration (deleted at the end of its scope, unless its declared static in which case it has "static" storage duration); moreover, objects on the heap have dynamic storage duration. Dynamic memory in C++ is controlled by you, that's why we are given new and delete (because C++ expects us to handle the memory ourselves). And otherwise deleting an object not constructed with new is undefined behavior and may lead to a crash.

David G
  • 94,763
  • 41
  • 167
  • 253
0

delete is applied to objects, not to classes. As a rule, calling delete (or arranging to have it called automatically, via a shared pointer, or with the RAII idiom in general) is necessary only if you called new to create the object. The exception is the return value of some (library?) call being an object that the (library's?) documentation states explicitly that the caller has to dispose of with delete (in which case, think of the call as a wrapper around a new that you have become responsible for.) Of course, APIs like that should be avoided if at all possible.

arayq2
  • 2,502
  • 17
  • 21
0

If Qt, use QPointer! It is a smart pointer: nothing needed in destructor.

#include <QPointer>

class MyClass1
{
  QPointer<MyClassA> pA;
  QPointer<MyClassB> pB;
};
Naszta
  • 7,560
  • 2
  • 33
  • 49