hmmm, you are correct. It's a habit I formed when working with com ptrs in directx because you have to call ptr->Release(), so you need to make sure that it is pointing to something first.
So scratch the check, it is unnecessary.
Memory usage growth over time
- SlevinKelevra
- Posts: 80
- Joined: September 9th, 2013, 12:58 am
- Location: Germany
Re: Memory usage growth over time
Another thing I would suggest when deleting a pointer is to set it to NULL afterwards:
if( NULL != map)
{
delete[] map;
map = NULL;
}
It could be, that you check for NULL somewhere else... And if it is not NULL you try to do something with map, because you assume it is not deleted... So the check for NULL before deleting is not always necessary, but setting it to NULL afterwards should be always done.
Edit: maybe "map" is not the best example for this, but I hope you get my point.
if( NULL != map)
{
delete[] map;
map = NULL;
}
It could be, that you check for NULL somewhere else... And if it is not NULL you try to do something with map, because you assume it is not deleted... So the check for NULL before deleting is not always necessary, but setting it to NULL afterwards should be always done.
Edit: maybe "map" is not the best example for this, but I hope you get my point.
Carpe noctem
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: Memory usage growth over time
I've got an example of why you should do the check,...move semantics.
You have to do the check or else you delete the memory to you gave ownership to some other class.
You have to do the check or else you delete the memory to you gave ownership to some other class.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com
Re: Memory usage growth over time
There are always these too ..
Or..
Code: Select all
#ifndef SAFE_DELETE
#define SAFE_DELETE(p) { if (p) { delete (p); (p)=NULL; } }
#endif
#ifndef SAFE_DELETE_ARRAY
#define SAFE_DELETE_ARRAY(p) { if (p) { delete[] (p); (p)=NULL; } }
#endif
#ifndef SAFE_RELEASE
#define SAFE_RELEASE(p) { if (p) { (p)->Release(); (p)=NULL; } }
#endif
Code: Select all
template<class T> void SafeDelete(T& t)
{
if (t)
{
delete t;
t = nullptr;
}
}
template<class T> void SafeDeleteArray(T& t)
{
if (t)
{
delete[] t;
t = nullptr;
}
}
template<class T> void SafeRelease(T& t)
{
if (t)
{
t->Release();
t = nullptr;
}
}
Curiosity killed the cat, satisfaction brought him back