• Advertisement

Archived

This topic is now archived and is closed to further replies.

Custom mem tracker problems

This topic is 5215 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

I am currently developing a simple memory tracker to trace memory leaks. I am using this article for it. The used code I have so far is like this:
#include <malloc.h>

#ifdef _DEBUG
// Overloaded new operator, taking as parameters the Size of the block to be
// allocated, the filename and linenumber from where it''s called
inline void * __cdecl operator new(unsigned int Size, const char *szFile, int Line)
{
    void *ptr = (void *)malloc(Size);
    AddTrack((DWORD)ptr, Size, szFile, Line);
    return (ptr);
};

// Overloaded delete operator
inline void __cdecl operator delete(void *p)
{
    RemoveTrack((DWORD)p);
    free(p);
};
#endif

#ifdef _DEBUG
#define DEBUG_NEW new(__FILE__, __LINE__)
#else
#define DEBUG_NEW new
#endif
#define new DEBUG_NEW
 
However, this gives me an compiler warning, as shown below:
F:\Projects\CWindow\main.cpp(55) : warning C4291: ''void *__cdecl operator new(unsigned int,const char *,int)'' : no matching operator delete found; memory will not be freed if initialization throws an exception
        f:\projects\cwindow\memmanager.h(6) : see declaration of ''new''
 
I am confused, what is the problem? Toolmaker
-Earth is 98% full. Please delete anybody you can.

Share this post


Link to post
Share on other sites
Advertisement
If you''re using exceptions, and a placement-new throws an exception, the corresponding placement-delete will be called in order to clean up. That''s the only instance where it''ll be called. So if you''re using exceptions, implement the corresponding placement-delete.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
Meaning I need a


inline void * __cdecl operator new(unsigned int Size, const char *szFile, int Line)


function aswell? I am a bit confused with the answer, so excuse me

Toolmaker



-Earth is 98% full. Please delete anybody you can.

Share this post


Link to post
Share on other sites
Not quite. Instead of the size, a void* is passed in; and it''s called "delete", not "new" (tho I''m sure that was just an oversight on your part). Other than that, yeah.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
Uhm, I think I am confusing you and myself. So, could you post some function prototypes for me as a reference? I think I need sleep too

Tool



-Earth is 98% full. Please delete anybody you can.

Share this post


Link to post
Share on other sites
void* operator new(size_t size, int blah1, char* blah2, double blah3);
void operator delete(void* location, int blah1, char* blah2, double blah3);



How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
I have a suggestion: Rather than build your own memory-leak-hunter, why not use a solidly proven one?

I recommend MMGR from Fluid Studios, been using it for quite a while now, and can''t picture working without it. It''s only weakness is always finding five leaks when STLport is installed. (Except the OGRE team seems to have found a way around that, hmm)

Anyway, here''s the site: http://www.fluidstudios.com/publications.html

Share this post


Link to post
Share on other sites
Sneftel, thanks, but I found out by searching in the MSDN too I was kinda not thinking then.

This is what I came up with to track the memory:

void CTracker::AddTrack(DWORD Addr, DWORD Size, const char *szFile, DWORD Line)
{
MEM_INFO *Mem = new(MEM_INFO);

strncpy(Mem->szFile, szFile, 79);
Mem->dwAddress = Addr;
Mem->dwSize = Size;
Mem->dwLine = Line;
AllocList.insert(AllocList.begin(), Mem);
}

void CTracker::RemoveTrack(DWORD Addr)
{
MEM_INFO *Mem = NULL;

for (std::list<MEM_INFO*>::iterator i = AllocList.begin(); i != AllocList.end(); ++i)
{
if ((*i)->dwAddress == Addr)
{
Mem = (*i);
AllocList.remove((*i));
delete(Mem);
break;
}
}
}


Toolmaker



-Earth is 98% full. Please delete anybody you can.

Share this post


Link to post
Share on other sites

  • Advertisement