My first public solution

Started by
24 comments, last by sasho648 11 years, 6 months ago
@Brother Bob Thanks for the problem respond but I don't understand how do {} while() loop will help me.
@brx With your solution the user need to type () after the delete but the most delete calls are without them.

Here is my solution (which I'll correct in my last post):


#define DEL_OLD delete
#define delete if(PassDeleteParams(__FILE__, __LINE__)); else DEL_OLD


The problem now is more complex - the threads in the program can assign dynamicly created object on an global variables like this:


char* String;
unsigned long __stdcall Thread1 (void* lp)
{
String=new char[50];
#ifdef DEBUG
EndThreadMemCheck();
#endif
return 0;
}


In this case it will give an error for an memory leak and when I delete it from the main thread it will give me message for an invalid delete call. Any suggestions to avoid this?
Advertisement

@Brother Bob Thanks for the problem respond but I don't understand how do {} while() loop will help me.

When it comes to calling macros, it doesn't force the use of a semicolon. It is never an error, nor is it never required, to call a macro with or without a semicolon; nor can you force any single convention upon the macro call itself. What the macro expands to for the compiler, however, may or may not require a semicolon.

The purpose of the do-while construct is to force the convention of a semicolon after the macro call just like you are forced to have a semicolon after a delete statement.

Now, I did overlook the else-statement in your macro so the comment to your particular macro was not correct. I believe it does expand correctly in any situation where the delete-syntax is correct. I would, however, suggest that if you take the advice with the do-while loop construct for any further macro that has any sort of local scope or control structure as the base of the macro.

@Brother Bob Thanks for the problem respond but I don't understand how do {} while() loop will help me.

Here is my solution (which I'll correct in my last post):


#define DEL_OLD delete
#define delete if(PassDeleteParams(__FILE__, __LINE__)); else DEL_OLD


I suggest a normal [font=courier new,courier,monospace]do {} while (false)[/font], simply because it's more idiomatic. Particularly, some compilers may generate a warning about the [font=courier new,courier,monospace]if (true);[/font] body being empty. But it's up to you, I don't see a particular issue with that method.

Sorry I misled you on placement delete. It turns out that the syntax [font=courier new,courier,monospace]delete(params) ptr;[/font] is not supported in C++, which is why the macro was having issues with delete.

One more thing: you should use [font=courier new,courier,monospace]NULL[/font] (or [font=courier new,courier,monospace]nullptr[/font] if using C++11) for null pointers. 0 works, but [font=courier new,courier,monospace]NULL[/font]/[font=courier new,courier,monospace]nullptr[/font] are preferred in practice.

I don't know much about your threading issue as I've never used [font=courier new,courier,monospace]__thread[/font] before, sorry I'm not much help there.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

skijl.png


HAHAHAHA!!! I was just watching that episode the other day lol! Sorry, but you literally made me LOL... XD
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________

[quote name='sasho648' timestamp='1347299520' post='4978639'][source]MessageBoxA(0, str, "Memory leaked!", MB_OK | MB_ICONWARNING);[/source]


You make this call multiple times. Don't. Just use 'MessageBox'. The reason that there's a macro defining the name is for character width (I think).
[/quote]

The macro just resolves to either MessageBoxA() or MessageBoxW() depending on the compiler's set character width. If he's giving it a string literal it's best to call MessageBoxA().
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
@Brother Bob I again don't understand how to include do{} while() loop in the delete define. Can you please snap a code example?

My thread issue now Is not simple - I need to use one static list structure because the global variables. So my program need to change:


void EndProgramMemCheck() ;



MemoryAllocList* & GetMemoryList()
{
static MemoryAllocList* pAllocList(0);
return pAllocList;
}
MemoryAllocList** & GetMemoryCurrPtrList()
{
static MemoryAllocList** ppCurAllocList(0);
return ppCurAllocList;
}



inline bool & bIsWorking()
{
static bool Obj(0);
return Obj;
}

inline unsigned long & UlThreads()
{
static unsigned long Obj(0);
return Obj;
}



These function will be needed for the new opeartor:


void* operator new(size_t size, const char* szFile, unsigned int nLineNo) _GLIBCXX_THROW (std::bad_alloc)
{
if(!bIsWorking()) bIsWorking()=true;
else while(bIsWorking());

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();
}
UlThreads()++;
NumThreadsInFunction=UlThreads();

(*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;
}


The first bold lines in this code are for threads which call the function with delay. The second bold lines are to resolve if there are multi-thread using the function at the same time. Here is the problem I can check and understand where and how many threads are using this code (at same time) but I don't know what to do next. Any suggestions?

This topic is closed to new replies.

Advertisement