Memory usage growth over time

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Memory usage growth over time

Post by chili » February 24th, 2017, 6:29 am

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.
Chili

User avatar
SlevinKelevra
Posts: 80
Joined: September 9th, 2013, 12:58 am
Location: Germany

Re: Memory usage growth over time

Post by SlevinKelevra » February 24th, 2017, 7:18 am

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.
Carpe noctem

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Memory usage growth over time

Post by albinopapa » February 24th, 2017, 7:37 am

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.
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

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Memory usage growth over time

Post by MrGodin » February 24th, 2017, 9:05 am

There are always these too .. ;)

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
Or..

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

Post Reply