How to check deallocation of memory
Asked Answered
Z

7

13

How to check if memory to which pointer p points has been succesfully deallocated?

Zosi answered 10/11, 2010 at 10:21 Comment(5)
More detail needed: how was the memory allocated initially ? and why do you need to check whether it has been deallocated ?Eliza
At the application layer, it can't. If however you are working at some very low level kernel layers, there could be ways to do that.Hertha
Yeap, what's the real problem you're trying to solve?Salvador
What I'm trying to do is this: int* p = ::operator new(sizeof(int)*10); delete p; and now I would like to know if that mem has been successfully deallocated.Zosi
If p was valid pointer to allocated block, then after delete p; your memory is successfully deallocated. No other options. However, calling correct destructors of objects is another thing.Grandson
N
12

In few words: you can't.

Check out tools like Valgrind to help you debugging memory leaks issues.

Some other things you should consider:

  • Use smart pointers so that you don't have do think about memory management,
  • Set your pointers to 0 after you free them, so that a further delete has no effect,
  • Use standard classes (vector, ...) instead of rolling your own,
  • Finally, don't use pointers (actually you almost can)
Nebulosity answered 10/11, 2010 at 10:24 Comment(9)
To avoid double free issues. It could be what OP wants after all.Nebulosity
@VJo: What do you mean ? delete p; p = 0; is UB ?Nebulosity
@Alexandre C. Yes, accessing the pointer after deletion is undefined behaviour. Besides, using raw pointers in c++ is a bad practice.Soundless
@VJo: It obviously isn't UB if you just set the pointer to 0.Murrah
@VJo: delete p; *p = 0 is UB. I do not mean to access the pointed-to memory, only the variable holding its (now non valid) address.Nebulosity
+1 for "Don't use pointers." Many people don't realize that they are often unnecessary.Murrah
@VJo: No, setting an invalid pointer to a null pointer or to another valid pointer is not UB. delete p; p = p; is UB if p was not null initially.Salvador
It is UB even if p was null initially. Accessing the pointer in any way after it is deleted in UB.Soundless
@VJo: Accessing the pointed-to value (ie. dereferencing a deleted pointer) is UB. Accessing the pointer is perfectly legal.Nebulosity
E
5

Sorry, very short answer "You can't"

Elegize answered 10/11, 2010 at 10:23 Comment(0)
P
2

Use IBM rational purify tool to check correct deallocation of memory.

Polyamide answered 10/11, 2010 at 10:27 Comment(0)
L
2

Define successfully! Define deallocated!

After deallocating memory (whether it is free or delete) you must not use that pointer again. All other assumptions are irrelevant.

After all, you call the C/C++ runtime to deallocate memory, but the C/C++ runtime also calls functions of the operating system to free the page. You could even have a custom memory allocator on top of the C/C++ runtime that e.g. uses caching to implement a faster memory allocation algorithm.

All of these layers may keep the deallocated memory for themselves (because of fragmentation or just because they like to keep it to themselves) or may tell the underlying layer to deallocate it. Anything can happen, just don't use that pointer anymore.

Lipoprotein answered 10/11, 2010 at 10:29 Comment(0)
A
1
  1. Some tools which are doing static code analysis can point some problems regarding the memory deallocation.
  2. Use valgrind to check whether you have memory leaks
  3. Avoid raw pointers - use smart pointers instead
Ancell answered 10/11, 2010 at 10:27 Comment(0)
A
1

Exception handling. I.e. try/catch blocks.

Anestassia answered 31/10, 2012 at 13:40 Comment(1)
Incorrect; a destructor must not throw an exception, so this won't do anything.Vilma
W
0

In C++, you can safely assume deallocation never fails. Destructors must not throw exceptions, and the actual memory reserved should never fail to be released, so given those two points, nothing can go wrong.

However, if you delete a pointer that has already been deleted, your program will probably crash. This isn't a problem with deallocation itself though - the original delete worked successfully. It's a problem with your program's memory management if it tries to delete a pointer twice, but that's rarely necessary with modern STL and smart pointers like std::vector, std::unique_ptr, etc...

Wristband answered 10/11, 2010 at 13:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.