You need to get rid of the space between delete and (.
This doesn't work for me. Can you give me an example code or explaining me more clearly.
You need to get rid of the space between delete and (.
#define DBG_NEW new ( _NORMAL_BLOCK, __FILE__, __LINE__ )
#define new DBG_NEW
#define DBG_DELETE delete ( __FILE__, __LINE__ )
#define delete DBG_DELETE
Ok I found a way to pass the arguments in the delete operator by a global variables like this:
[source]#define DELETE_OLD delete
#define delete 0; while(bIsDeleteParamsPassing); bIsDeleteParamsPassing=true; chFile=(char*)__FILE__; inLineNo=__LINE__; DELETE_OLD[/source]
#define DBG_DELETE delete(__FILE__, __LINE__)
#define delete DBG_DELETE
using namespace std;
struct Some {
Some() {std::cout<<"constructor" <<std::endl;}
~Some() {std::cout<<"destructor" <<std::endl;}
unsigned long hash;
unsigned long hash1;
} ;
int main()
{
Some* my=new Some[2];
cout << "Hello world!" << endl;
delete my; //line 21
#ifdef DEBUG
EndOfProgramMemCheck();
#endif
system("PAUSE");
return 0;
}
#define DBG_DELETE delete(__FILE__, __LINE__)
#define delete DBG_DELETE
#include <iostream>
#include <windows.h>
#include <cstring>
#include <cstdio>
struct MemoryAllocList {
char* File;
unsigned int Line;
MemoryAllocList* next;
void* MemoryPointer;
} ;
struct LineFile {
unsigned int nLineNo;
char* szFile;
} ;
MemoryAllocList* & GetMemoryList()
{
static __thread MemoryAllocList* pAllocList(0);
return pAllocList;
}
MemoryAllocList** & GetMemoryCurrPtrList()
{
static __thread MemoryAllocList** ppCurAllocList(0);
return ppCurAllocList;
}
LineFile & GetDeleteParams()
{
static __thread LineFile Obj;
return Obj;
}
void* operator new(size_t size, const char* szFile, unsigned int nLineNo) _GLIBCXX_THROW (std::bad_alloc)
{
void* pMem;
while(true) {
pMem=malloc(size ? size : 1);
if(!pMem)
{
#ifdef WIN32_MEM_LOG
MessageBoxA(0, "No more memory to use!", "Warning!", MB_OK | MB_ICONWARNING);
#endif
#ifndef WIN32_MEM_LOG
std::cerr << "No more memory to use!" << "Warning!" << std::endl;
#endif
std::new_handler cach=std::set_new_handler(0);
if(cach) (*cach)();
else throw std::bad_alloc();
std::set_new_handler(cach);
}
else
break;
}
MemoryAllocList** & ppCurAllocList=GetMemoryCurrPtrList();
if(!ppCurAllocList) //Thread first new call
{
ppCurAllocList=&GetMemoryList();
}
while(*ppCurAllocList);
(*ppCurAllocList)=(MemoryAllocList*)malloc(sizeof(MemoryAllocList));
if(!(*ppCurAllocList))
{
#ifdef WIN32_MEM_LOG
MessageBoxA(0, "No more memory to use for the debug version of MemoryHandler. Try compiling the app \
in release mode. It will exit now!", "Error!", MB_OK | MB_ICONERROR);
#endif
#ifndef WIN32_MEM_LOG
std::cerr << "No more memory to use for the debug version of MemoryHandler. Try compiling the app \
in release mode. It will exit now!" << "Error!" << std::endl;
#endif
exit(1);
}
(*ppCurAllocList)->Line=nLineNo;
(*ppCurAllocList)->File=(char*)szFile;
(*ppCurAllocList)->MemoryPointer=pMem;
(*ppCurAllocList)->next=0;
ppCurAllocList=&(*ppCurAllocList)->next;
return pMem;
}
void* operator new[](size_t size, const char* szFile, unsigned int nLineNo) _GLIBCXX_THROW (std::bad_alloc)
{
return operator new(size, szFile, nLineNo);
}
void operator delete[](void* pMem) _GLIBCXX_USE_NOEXCEPT
{
operator delete(pMem);
}
bool PassDeleteParams (const char* szFile, unsigned int nLineNo)
{
LineFile & Obj(GetDeleteParams());
Obj.nLineNo=nLineNo;
Obj.szFile=(char*)szFile;
return false;
}
void operator delete(void* pMem) _GLIBCXX_USE_NOEXCEPT
{
LineFile CurrPos(GetDeleteParams());
if(!pMem) return;
MemoryAllocList* sourc(GetMemoryList());
while(sourc&&sourc->MemoryPointer!=pMem)
sourc=sourc->next;
if(!sourc)
{
#ifdef WIN32_MEM_LOG
char str[80];
sprintf(str, "Invalid delete call at File: %s Line: %d", CurrPos.szFile,
CurrPos.nLineNo);
MessageBoxA(0, str, "Warning!", MB_OK | MB_ICONWARNING);
#endif
#ifndef WIN32_MEM_LOG
std::cerr << "Invalid delete call at File: " << CurrPos.szFile << " Line: " << CurrPos.nLineNo << " Warning!" << std::endl;
#endif
return;
}
sourc->MemoryPointer=0;
free(pMem);
return;
}
void EndThreadMemCheck() {
MemoryAllocList* pCurAllocTree(GetMemoryList());
while(pCurAllocTree) {
if(pCurAllocTree->MemoryPointer) {
#ifndef WIN32_MEM_LOG
std::cerr << "Object at line: " << pCurAllocTree->Line << " in file: "
<< pCurAllocTree->File << " wasn't deleted!" << std::endl;
#endif
#ifdef WIN32_MEM_LOG
char str[80];
sprintf(str, "Object at line: %d in file: %s wasn't deleted!", pCurAllocTree->Line,
pCurAllocTree->File);
MessageBoxA(0, str, "Memory leaked!", MB_OK | MB_ICONWARNING);
#endif
}
MemoryAllocList* cach(pCurAllocTree);
pCurAllocTree=pCurAllocTree->next;
free(cach);
}
}
#pragma once
/*Sasho 648 Memory leaks and inavlid delete calls finder (MultiThread support)
Set the MemoryHandler.cpp file to compile only in debug mode
and include the MemoryHandler.hpp only when the DEBUG symbol is defined. Then
at the end of a thread call EndThreadMemCheck() too when the DEBUG symbol is defined.
Define WIN32_MEM_LOG for Windows Messages.
*/
void* operator new(size_t size, const char* szFile, unsigned int nLineNo) _GLIBCXX_THROW (std::bad_alloc);
void* operator new[](size_t size, const char* szFile, unsigned int nLineNo) _GLIBCXX_THROW (std::bad_alloc);
void operator delete[](void* pMem) _GLIBCXX_USE_NOEXCEPT;
void operator delete(void* pMem) _GLIBCXX_USE_NOEXCEPT;
bool PassDeleteParams (const char* szFile, unsigned int nLineNo);
#define new new ( __FILE__ , __LINE__ )
#define DEL_OLD delete
#define delete if(PassDeleteParams(__FILE__, __LINE__)); else DEL_OLD
void EndThreadMemCheck() ;
if(obj->isDead())
delete obj;
else
obj->update();
#define FOO do { /* code here, single or multiline-code */ } while(false)
Thank yoy very much for the critiques, this is exactly what I need because I learn c++ only from books and internet. The error behind "This doesn't work." is :
||=== Memw, Debug ===|
C:\Projects\Memw\main.cpp||In function 'int main()':|
C:\Projects\Memw\main.cpp|21|warning: left operand of comma operator has no effect [-Wunused-value]|
C:\Projects\Memw\main.cpp|21|error: type 'int' argument given to 'delete', expected pointer|
C:\Projects\Memw\main.cpp|21|error: expected ';' before 'my'|
||=== Build finished: 2 errors, 2 warnings ===|
[...]
with defined
#define DBG_DELETE delete(__FILE__, __LINE__)
#define delete DBG_DELETE
void* operator delete(void*, const char* szFile, unsigned int nLineNo);
delete(myObject, __FILE__, __LINE__);
delete myObject;
// will be expanded to
delete(__FILE__, __LINE__) myObject;
delete(myObject, __FILE__, __LINE__);
delete(__FILE__, __LINE__, myObject);
#include <iostream>
class Foo {
public:
Foo() {}
};
void* operator new(size_t size, const char* f, unsigned int line) {
std::cout << "new: " << f << ":" << line << std::endl;
return malloc(size);
}
// just here to avoid the warning "'void *operator new(size_t,const char *,unsigned int)' : no matching operator delete found; memory will not be freed if initialization throws an exception"
void operator delete(void* ptr, const char* f, unsigned int line) {
std::cout << "delete: " << f << ":" << line << std::endl;
return free(ptr);
}
void Destroy(void* ptr, const char* f, unsigned int line) {
std::cout << "delete: " << f << ":" << line << std::endl;
return free(ptr);
}
#define DBG_NEW new(__FILE__, __LINE__)
#define new DBG_NEW
#define DBG_DELETE(p) Destroy(p, __FILE__, __LINE__)
#define delete(p) DBG_DELETE(p)
int main() {
Foo* myFoo = new Foo();
delete(myFoo);
return 0;
}