Skip to main content
gamedev.net
🔒 Locked

Making a Simple Memory Manager

Started by InsaneX 9 replies 600+ views
Original Post
InsaneX
InsaneX
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
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]
SiCrane
SiCrane
I see two problems:
1) you forgot a ; at the end of your class.
2) I have no idea how you''re supposed to use that Alloc<>() function. Did you mean T *& ptr?
InsaneX
InsaneX
So, would this be correct?

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);
SiCrane
SiCrane
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]
InsaneX
InsaneX
I think that this is mostly right (not sure if the check on the DeAlloc<>() function is right).

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]
SiCrane
SiCrane
Compiles fine for me with MinGW. (version 3.3.1). What''s the exact error message you''re getting? It could be a #define clobber going on. What other files are you including, and in what order are you including them in?
InsaneX
InsaneX
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.
InsaneX
InsaneX
I found an article on gamedev about organizing files, and after the first paragraph I knew it was for me.
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
SiCrane
SiCrane
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.
antareus
antareus
Probably better to make it so the memory manager only allocates single instances, not arrays, since you can''t detect whether you''re deleting a pointer to a single object, or an array.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
henrym
henrym
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.