Problem overloading new and delete

Started by
0 comments, last by King_DuckZ 15 years, 9 months ago
Hello everyone! I'm trying to overload the global new and delete for debugging purposes, but something is not working quite right. Today I found what I thought to be a memory leak, but after a more careful debugging session, I think in some cases my overloaded delete just doesn't get called. I'm sure it's crammed full with bugs, so I post some code:

//Prototypes included by the precompiled header
#include <cstddef>

void* operator new ( std::size_t sz ); //Uses std::malloc(sz) internally
void operator delete ( void* ptr ); //Uses STD::free(ptr) internally



The problem seems to be localized here:

void CGigaPaq::m_Dispose() {

	//PAQ_HEADER* m_lpphHead, struct PAQ_HEADER
	if (m_lpphHead) {
		delete m_lpphHead;
		m_lpphHead = 0;
	}

	//PAQ_ITEM* lppiFileList, struct PAQ_ITEM
	if (m_lppiFileList) {
		delete[] m_lppiFileList;
		m_lppiFileList = 0;
	}

	//CIOFile* m_lpioPaq, class CIOFile
	if (m_lpioPaq) {
		delete m_lpioPaq;
		m_lpioPaq = 0;
	}

	return;



There Are a few things I don't really understand: either in Visual Studio .net and CodeWarrior, my overloaded version of new is always called (even for arrays). The same is true for delete and delete[], which both seem to call my overloaded delete. But in this special case, neither for m_lppiFileList nor for m_lpioPaq delete gets called. The debugger - instead - takes me to a standard delete function from Visual Studio. Thus the apparent memory leak. I've been searching the net for a while, but I've found nothing that looks like this... Any ideas? Edit: If I actually try to implement overloaded versions for new[] and delete[], I get a multiple definition error: Linking... dlDebugInfo.obj : error LNK2005: "void * __cdecl operator new[](unsigned int)" (??_U@YAPAXI@Z) already defined in msvcprtd.lib(newaop_s.obj) Debug/DuckLib_Debug.dll : fatal error LNK1169: one or more multiply defined symbols found
[ King_DuckZ out-- ]
Advertisement
I somehow managed to fix the problem. Also, a clean rebuild helped with the multiple inclusion error! :)
[ King_DuckZ out-- ]

This topic is closed to new replies.

Advertisement