• Advertisement
Sign in to follow this  

A function for checking memory usage?

This topic is 4041 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Gumgo
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.

MaterialTemp is actually a pointer to a block of memory (in this case, it contains an array of D3DXMATERIAL structures). What happens is that Direct3D stores the materials from the .x file in an array, and MaterialTemp is nothing more than the location of the array of materials.

Quote:

What does the (D3DXMATERIAL*)MaterialBuffer->GetBufferPointer(); part do? Something to do with arrays?

As above, Direct3D places the material information in an array, and this function returns the location of the array.

Quote:

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?

That's right. Since MaterialTemp is not dynamically allocated with new, it doesn't need to be deleted. It will automatically disappear when it goes out of scope. However, since MaterialTemp is a pointer, the data that it points to may need to be deleted. But since you yourself did not allocate this memory, you need to check what the proper way to release the pointed-to-data would be.

I'm not sure how ID3DXBuffer handles itself, so you might need to check that up.

Share this post


Link to post
Share on other sites
In that case, will the StaticMesh->release(); clear out all vertecies, buffers, etc. created when the mesh was loaded? Is "delete[] Material" the only thing I need to delete?

Share this post


Link to post
Share on other sites
If you are referring to memory leaks caused my new operations not being deallocated properly by delete, then perhaps you could refer to the _CrtMemState object.

It's defined in the header crtdbg.h and is used in a few runtime memory checking functions. I tend to use the function _CrtMemCheckpoint to take snap-shots of the current debug heap and the _CrtMemDifference function, to compare different snapshots.

What is more, these functions completely disappear in runtime versions of your code.

Anyway, read up on it in MSDN. It might be what you are looking for.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement