Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete pointer memory and confirm it

Considering the following:

tbModelHFrame = new TbModelHeaderFrame(this, storage->getDataBase());

I guess the correct way to delete tbModelHFrame memory will be

delete tbModelHFrame;

Right?

How do I check that the memory was really released?

like image 996
KcFnMi Avatar asked Aug 01 '26 02:08

KcFnMi


2 Answers

How do I check that the memory was really released?

You don't.

C++ has no means of telling whether a pointer points to a valid object or a random region in memory. The latter includes a region that was valid at some point, but has been deleted since.

It is up to the developer to organize their code in a way that this cannot happen.

The only guarantee that the language gives you to help you out here, is that a delete call never fails. So if you call delete once on the object, you can be reasonably sure that the object destroyed properly and the memory was released. Just don't attempt to access it again afterwards, or you'll be in trouble.

like image 138
ComicSansMS Avatar answered Aug 02 '26 17:08

ComicSansMS


Yes, what is allocated with new should be freed with delete. A way to check if every dinamically allocated memory has been freed is to use Valgrind's Memcheck

Anyway, it is usually safer to use smart pointers (See here).

like image 33
bznein Avatar answered Aug 02 '26 16:08

bznein