Delete in the destructor?

I have the following code and I wonder if this delete b is needed here? Will my operating system automatically clear the allocated memory area?

 class A { B *b; A() { b = new B(); } ~A() { delete b; } }; 

Many thanks.

+8
c ++ memory-management
source share
9 answers

Yes, you must delete to create every object created with the new that you have . In this case, it seems that class A owns the same instance of class B and is responsible for calling delete .

You will be much better off using a smart pointer to control the lifetime of a class B instance. Also note that you need to either implement or deny the assignment operator and copy constructor in class A to prevent small copying of the object, which can cause you big problems.

+10
source share

Your operating system will probably free the allocated memory, but this will be done when your program exits. Long programs will occur in memory.

Using smart pointers for objects with dynamic coverage is always helpful. They will do all deleted things for you.

  • stand :: auto_ptr
  • promotion :: shared_ptr
  • boost :: scoped_ptr
+5
source share

If you invoke a new one, appropriate deletion is always advisable.

How much the operating system deletes your memory .. yes, this will happen, but only after the completion of the whole process (i.e. your application terminates). Only then will all memory and other resources be restored by the OS.

Thirdly, try using new / delete only if necessary. In your script, you can simply write

 class A { B b; A() {} ~A() {} }; 

It will have the same effect, and you will avoid the additional allocation of dynamic memory.

+2
source share

It is certainly necessary as you wrote it. Even with delete , however, the class is fundamentally broken because it controls the resource, but does not follow rule three .

Nevertheless, there is practically no reason for manual memory management - it rarely happens. You should probably either just have object B as a member variable, or use a smart pointer like QScopedPointer :

 struct A { QScopedPointer<B> b; A() : b(new B()) { } // No ~A() needed; when the A object is destroyed, QScopedPointer ensures // that the B object pointed to by the member 'b' is destroyed and deleted. }; 

You want to make sure that you have a good C ++ introductory book so that you can learn how to write the right C ++ programs.

+1
source share

It will clear the area only when the process is completed, but the area will remain all the time until then, which means a memory leak .

+1
source share

Resource management is more than freeing up memory. Yes, most platforms will make any memory that you allocate when the process ends, and the memory will be cheap enough that you may not notice for some time. But what about the file b keeps open, or does it unlock the mutex in its destructor? Before you run out of memory, you may run into problems associated with the fact that your objects will live past their usefulness.

+1
source share

Member b will be allocated to the heap. It is true that the operating system will free up all the memory occupied by the heap; however, this will happen only once: upon exiting the program.

So, if you do not free the allocated heap blocks at the very time when they become unused (usually in the destructor of some object), you run the risk of a memory leak .

So, the answer is basically: yes , you have to call delete manually, since the memory will not be automatically freed by ASAP (although smart pointers will help you achieve something similar).

0
source share

Everything that you allocate with new , you must free with delete , otherwise there will be a memory leak in your application.

On most modern operating systems (of course, the OS that usually runs on desktop computers) everything that uses the process will be cleared when the process ends, so if you forget delete , then the memory will be freed anyway, But you have to rely on this is.

I had programmed Amiga in C long ago. Its operating system was much less complex than today's operating systems. If you allocate memory and do not free it, it will remain allocated until you turn off or restart the computer even after the process is completed. Then a memory leak was an even more serious problem.

0
source share

1) Long-term applications will encounter problems, since the OS can only restore memory when the application stops working.

2) delete b; also causes the destructor of the executable instance B to start. Otherwise, it will never start, since there is no longer a way to get to it. This destructor can do something important.

0
source share

All Articles