View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# My first public solution

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

25 replies to this topic

### #21/ ProDevGURU   Members

Posted 26 September 2012 - 10:52 AM

@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
#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?

Edited by sasho648, 26 September 2012 - 10:55 AM.

### #22Brother Bob  Moderators

Posted 26 September 2012 - 11:47 AM

@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.

### #23Cornstalks  Members

Posted 26 September 2012 - 12:08 PM

@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 do {} while (false), simply because it's more idiomatic. Particularly, some compilers may generate a warning about the if (true); 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 delete(params) ptr; is not supported in C++, which is why the macro was having issues with delete.

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

[ 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 ]

### #24ATC  Members

Posted 26 September 2012 - 07:10 PM

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

___________________________________________________________________________________

### #25Khatharr  Members

Posted 26 September 2012 - 08:22 PM

MessageBoxA(0, str, "Memory leaked!", MB_OK | MB_ICONWARNING);

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).

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.

### #26/ ProDevGURU   Members

Posted 29 September 2012 - 08:03 AM

@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 End[b]Program[/b]MemCheck() ;


MemoryAllocList* & GetMemoryList()
{
[b]static[/b] MemoryAllocList* pAllocList(0);
return pAllocList;
}
MemoryAllocList** & GetMemoryCurrPtrList()
{
[b]static[/b] MemoryAllocList** ppCurAllocList(0);
return ppCurAllocList;
}


[b]inline bool & bIsWorking()
{
static bool Obj(0);
return Obj;
}[/b]
{
static unsigned long Obj(0);
return Obj;
}[/b]


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)
{
[b]if(!bIsWorking()) bIsWorking()=true;
else while(bIsWorking());[/b]
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)();
std::set_new_handler(cach);
}
else
break;
}
MemoryAllocList** & ppCurAllocList(GetMemoryCurrPtrList());
{
ppCurAllocList=&GetMemoryList();
}
(*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?

Edited by sasho648, 29 September 2012 - 09:14 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.