A function for checking memory usage?

Started by
11 comments, last by TerrorFLOP 17 years, 2 months ago
Is there some way to return how much memory a program is using? It would be very useful for me because I am new to C++ and want to check if any of my functions have memory leaks. Also, one other quick question. I have a class and within the constructor of that class, I use the "new" operator. Then I create an instance of that class also using "new". When I "delete" the class, is it necessary to use "delete" to delete the new data type created in the class's constructor or is it automatically cleared when the instance of the class is deleted? I'm trying to write a function for loading .x models, and this part is for storing multiple materials. I looked this up from a tutorial but unfortunately the tutorial used global variables for all this and never deleted anything "new" because it was just an example. Thanks.
Advertisement
Ok, first.. you dont need to call delete unless you dynamically allocated memory.

You do this by using "new", so..

Foo f = new Foo();

You dont usually need to do this, so you can just do:

Foo f;

And C++ will take care of it for you.

However, dynamically allocating memory is useful at times.. let's sya you have an array, but you dont know how many members it will have. That is a case where you'd want to dynamically allocate memory.

Anyone feel free to correct me, I'm still beginning all this as well.
As to your question, I'm not sure.

Though you can Ctrl+alt+del and look at processes andf see how much % the program is using, if you're using windows.
Yeah, I'm creating a "new" array to store the different materials for the .x model. The problem with ctrl+alt+delete is that (1) it displays memory in kb, not bytes and a very small variable wouldn't use even a kilobyte. (2) the program is in fullscreen so switching doesn't work... and (3) I want it to write the memory to a log file so I can just view it afterwards, if possible.
As far as I know, when you allocate dynamic memory using "new", nesting aside, each item is pointed to a completely separate block of memory. Simply deleting your class will have no bearing on the memory block given to your data type.
Quote:Original post by hereticprophecy
As far as I know, when you allocate dynamic memory using "new", nesting aside, each item is pointed to a completely separate block of memory. Simply deleting your class will have no bearing on the memory block given to your data type.


Well, if you delete the dynamically allocated piece of memory in the class' destructor, yes it will.

I believe delete just calls the destructor, and so delete-ing the class will do so if you wrote the interface correctly.

Also, if you want to right the size of memory used.. use fstream (I'm assuming you know how, if not post and I'll help ya) and then write sizeof(variable_type) * number of elements.
Quote:Original post by Gumgo
Yeah, I'm creating a "new" array to store the different materials for the .x model. The problem with ctrl+alt+delete is that (1) it displays memory in kb, not bytes and a very small variable wouldn't use even a kilobyte. (2) the program is in fullscreen so switching doesn't work... and (3) I want it to write the memory to a log file so I can just view it afterwards, if possible.


It'd actiually just be easier if you posred your code
//*************************************************************************************************// Func: objStaticMesh Constructor// Desc: Load a static mesh//*************************************************************************************************objStaticMesh::objStaticMesh( const std::string& fname, LPDIRECT3DDEVICE9 dev ) : StaticMesh(NULL) // set texture to null originally{	HRESULT loaded;	loaded = D3DXLoadMeshFromX( fname.c_str(),		// File Name								D3DXMESH_SYSTEMMEM,	// Where to store								dev,				// Direct3D Device								NULL,				// Adjacency								&MaterialBuffer,	// The materials								NULL,				// Effects								&MaterialNumber,	// Number of materials								&StaticMesh );		// Texture handle	if (FAILED(loaded))	{		std::string errorstr = "Could not load object: " + fname;		WriteToLog( errorstr );	}	else	{		std::string errorstr = "Loaded object: " + fname;		WriteToLog( errorstr );		// Get pointer to buffer with material info		D3DXMATERIAL * MaterialTemp = (D3DXMATERIAL*)MaterialBuffer->GetBufferPointer();		// Create material buffer for each material in the mesh		Material = new D3DMATERIAL9[MaterialNumber];		// Do this for each material		for ( DWORD i = 0; i < MaterialNumber; i ++ )		{			Material = MaterialTemp.MatD3D;		// Material info			Material.Ambient = Material.Diffuse;	// Ambient = diffuse		}	}}//*************************************************************************************************// Func: objStaticMesh Destructor// Desc: Free a static mesh//*************************************************************************************************objStaticMesh::~objStaticMesh( void ){	if (StaticMesh)	{		StaticMesh->Release();		delete[] Material;		std::string errorstr = "Object released.";		WriteToLog( errorstr );	}}


I've already got functions that write to a log (WriteToLog), so assuming the fstream is about that, I've got it covered. But perhaps the sizeof is what I need. I'm not sure though, because basically, what I'm trying to do is figure out if I correctly freed the memory used. So if sizeof just checks the size of a variable but the variable was deleted it may not help.

I googled some stuff like "C++ finding memory usage" but couldn't find much.

Also, does anyone have any ideas why I'm having trouble with "source" tags? Basically whenever I try to use them, when I hit "Reply" it just sits there for a while and goes to "page cannot be displayed". And then I can't post for a little while. LIKE JUST A MOMENT AGO! Arrrrgh...
Looks fine. To answer one of your questions, this should not leak memory:
//This allocates memory then calls the constructor, where the materials are allocated:objStaticMesh* myMesh = new objStaticMesh(blah);//This calls the destructor, which will delete Material, then frees up the memory.delete myMesh;

About finding memory leaks, search for things like memory leak detector, memory leak finder or just memory leak tool.
___________________________________________________David OlsenIf I've helped you, please vote for PigeonGrape!
Thank you very much. There's one thing that I'm still a bit confused about though, and that is "MaterialTemp" (which the tutorial said to use, though it was called something different). I notice that it is used as an array later in the code:
Material = MaterialTemp.MatD3D;
But isn't declared as one... or at least, doesn't appear to be. What does the (D3DXMATERIAL*)MaterialBuffer->GetBufferPointer(); part do? Something to do with arrays? And finally, since I'm not using "new" with MaterialTemp, I don't have to ever "delete" it in the destructor since it is just a local variable of the class? The only reason I ask this is because since Material is dynamically created and MaterialTemp seems to "go along" with it, maybe it was somehow dynamically created too and I have to use "delete"...??? Of course, I don't doubt that I'm totally wrong, because I really know very little about this. :)

This topic is closed to new replies.

Advertisement