const bool ARRAY = true;
const bool SINGLE = false;
class CMemoryManager
{
int allocs;
int deallocs;
public:
CMemoryManager()
{
allocs = 0;
deallocs = 0;
}
~CMemoryManager()
{
if(deallocs < allocs)
PostError("Memory Leak Detected.\nNot all memory Deallocated");
if(deallocs > allocs)
PostError("Memory Leak Detected.\nToo much memory Deallocated");
}
template <typename T>
bool Alloc(T ptr, bool array = SINGLE, int num = 1)
{
if(array)
ptr = new T[num];
else
ptr = new T;
if(!ptr)
return false;
allocs += 1;
return true;
}
template <typename T>
bool DeAlloc(T ptr, bool array)
{
if(array)
delete [] ptr;
else
delete ptr;
deallocs += 1;
}
}
<SPAN CLASS=editedby>[edited by - InsaneX on February 2, 2004 8:36:07 PM]</SPAN>[/source]
[edited by - InsaneX on February 2, 2004 8:36:50 PM]
🔒 Locked
Making a Simple Memory Manager
Advertisement
Original Post
I am trying to make a simple memory manager using templates. At the point I have it now, I keep getting this weird error "MemoryManger declared as a 'virtual' field" error. The error is strangely in the objidl.h file. This error pops up a bunch of times, not just once either. Heres the full code for the class
So, would this be correct?
and then, If I want to call it, I would just
const bool ARRAY = true;
const bool SINGLE = false;
class CMemoryManager
{
int allocs;
int deallocs;
public:
CMemoryManager()
{
allocs = 0;
deallocs = 0;
}
~CMemoryManager()
{
if(deallocs < allocs)
PostError("Memory Leak Detected.\nNot all memory Deallocated");
if(deallocs > allocs)
PostError("Memory Leak Detected.\nToo much memory Deallocated");
}
template <typename T>
bool Alloc(T* &ptr, bool array = SINGLE, int num = 1)
{
if(array)
ptr = new T[num];
else
ptr = new T;
if(!ptr)
return false;
allocs += 1;
return true;
}
template <typename T>
bool DeAlloc(T* &ptr, bool array)
{
if(array)
delete [] ptr;
else
delete ptr;
deallocs += 1;
}
};
and then, If I want to call it, I would just
CMemoryManager MemMan;
int* pInt;
MemMan.Alloc(pInt);
The Alloc<>() function looks semi-ok now. However, your DeAlloc<>() function is a mess. It's not returning a value, and you have a bool return type. Also, the use of T *& is probably not what you want for a deallocation function because you may at times want to deallocate a pointer that isn't an l-value. Just T * should be fine.
[edited by - SiCrane on February 2, 2004 9:03:25 PM]
[edited by - SiCrane on February 2, 2004 9:03:25 PM]
I think that this is mostly right (not sure if the check on the DeAlloc<>() function is right).
I still get errors in objbase.h and objidl.h, and I get the "CMemoryManager declared as 'virtual' field". I am using DevCpp in case you were wondering, (maybe the Mingw compiler uses templates differently?)
[edited by - InsaneX on February 2, 2004 9:12:41 PM]
const bool ARRAY = true;
const bool SINGLE = false;
class CMemoryManager
{
int allocs;
int deallocs;
public:
CMemoryManager()
{
allocs = 0;
deallocs = 0;
}
~CMemoryManager()
{
if(deallocs < allocs)
PostError("Memory Leak Detected.\nNot all memory Deallocated");
if(deallocs > allocs)
PostError("Memory Leak Detected.\nToo much memory Deallocated");
}
template <typename T>
bool Alloc(T* ptr, bool array = SINGLE, int num = 1)
{
if(array)
ptr = new T[num];
else
ptr = new T;
if(!ptr)
return false;
allocs += 1;
return true;
}
template <typename T>
bool DeAlloc(T* &ptr, bool array)
{
if(array)
delete [] ptr;
else
delete ptr;
if(ptr)
return false;
deallocs += 1;
return true;
}
};
I still get errors in objbase.h and objidl.h, and I get the "CMemoryManager declared as 'virtual' field". I am using DevCpp in case you were wondering, (maybe the Mingw compiler uses templates differently?)
[edited by - InsaneX on February 2, 2004 9:12:41 PM]
I am including windows.h, gl.h, glu.h, fstream.h, and a bunch of my own includes. The whole thing is really messed up, I originally tried to have a general include file GameEngine.h, that would have all the includes, and I would just include it in all my files, but that didn''t work (I had to include it in the header files, so they tried to include themselves, not sure if that was the problem though). My whole organization is messed up anyway. I am having alot of problems with variables between files and the like. Its probably a problem with the way my files are organized or something.
I found an article on gamedev about organizing files, and after the first paragraph I knew it was for me.
not sure if that would work, but you get the idea
quote:
However, many beginning programmers may not realize what the point of this is - especially since some may have tried it themselves and run into so many problems that they decided it wasn''t worth the effort.
not sure if that would work, but you get the idea
Well I included all those files, and a few extra random ones and it still compiles and runs fine for me, so I''m guessing that your header setup is just FUBAR.
Oh and the conditional inside your DeAlloc() function is no good. There''s no meaningful check you can do on your deallocation as long as you''re assuming that the pointer you''re being passed is good.
Oh and the conditional inside your DeAlloc() function is no good. There''s no meaningful check you can do on your deallocation as long as you''re assuming that the pointer you''re being passed is good.
quote:
Original post by InsaneX
I am including windows.h, gl.h, glu.h, fstream.h, and a bunch of my own includes.
You''re including all that into your Memory Manager?
fstream and your own includes I can understand, and poossibly windows.h, but does it really need to be able to access GL functions?
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement