Page 1 of 2

Coding - Memory Leaks

Posted: Sat Sep 17, 2005 9:18 pm
by va!n
Another interesting article by chaos/farbrausch. here we go...


Visual C++ has some good features for finding memory leaks. But when you are not using MFC, the compiler will not tell you the line where you allocated the memory.

Here is a hack that add's that info to Visual C++ memory checking. Put this in your most important header:

Code: Select all

/****************************************************************************/

#define _MFC_OVERRIDES_NEW
void * __cdecl operator new(unsigned int,const char *,int);
inline void __cdecl operator delete(void *p, const char *, int) 
{ ::operator delete(p); }
#define new new(__FILE__,__LINE__)

/****************************************************************************/

The implementation:

/****************************************************************************/

// this is the hack

#undef new
void * __cdecl operator new(unsigned int size,const char *file,int line)
{
  return _malloc_dbg(size,_NORMAL_BLOCK,file,line);
}
#define new new(__FILE__,__LINE__)

// initialize memory debugging
// if you don't see the testleak, then you know it's not working!

#include <crtdbg.h>

void MemInit()
{
  _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG)|
    _CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
  sChar *test = new sChar[16];
  sCopyString(test,"TestLeak",16);
}

// nothing needed when exiting

void MemExit()
{
}

// when you abort, you don't need to check for memory leaks
// most likely you will have thousands....

void MemAbort()
{
  _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG)&
    ~(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF));
}

/****************************************************************************/

If you need d3dx9.h, you might run into some trouble. Do the following:

/****************************************************************************/

#include "types.hpp"      // my own headers are incompatible with windows.h
#include "intro.hpp"      // they must come first  

#include <windows.h>      // now I include windows
#include <d3d9.h>         // and d3d9
#undef new
#include <d3d9x.h>        // but d3d9x needs unmodified operator new
#define new new(__FILE__,__LINE__)

/****************************************************************************/

Re: Coding - Memory Leaks

Posted: Tue Sep 20, 2005 7:40 am
by traumatic
Regarding all your recent posts:
Why didn't you simply post a link to chaos' and rygs' websites?

Too easy? Too nice?

http://www.xyzw.de/
http://www.farb-rausch.de/~fg/

Did both Fabian and Dierk ask you to re-post this stuff here?

I don't understand...

Re: Coding - Memory Leaks

Posted: Tue Sep 20, 2005 12:21 pm
by PB
> Why didn't you simply post a link to chaos' and rygs' websites?

Yes, I also think it's strange with all the C postings in a Basic forum.

I think a single post, with links, and re-edited when needed, would be
a far better solution. I don't want to see the Off Topic section become
a library of small C code snippets of questionable value to Basic coders.

Posted: Tue Sep 20, 2005 12:43 pm
by dagcrack
I've been asking that in the IRC but no one cared.. why so many posts about other's articles that are not even about PB? ok its programming - its global - but just posting the links would of worked.

Posted: Tue Sep 20, 2005 4:57 pm
by va!n
ok guys, i complete agree! in future i will only post the link(s)! so sorry for flooding the forum... i only thought i would make more sence to help some people...

@traumatic:
complete agree! but i dont think it make any problems to poste chaos original article as long as i dont change/modify nor dont give credits for it...!? i still think its a world where everyone should help everyone... isnt it? :roll:

@dagcrack:
ok, why not add a new section as the france forum? there is an extra section for ASM stuff only... so it would make sence to have an ASM only and a C/C++ to PB Tips/Coverting section here too!? :wink:

@PB:
if you want, you can delete my coding related links by chaos and leave traumatics posting with the links alone ;)

Posted: Tue Sep 20, 2005 5:15 pm
by Dare2
Personally, I don't see the issue here. Credit was given, code was shown.

As to the other points, is this worse than having screenshots appear? Is this worse than a dozen posts with "lol" and smilies filling up a screen? Is it less helpful?


lol. :) and also :? * confused *


Perhaps a definition of what is on-topic in off-topic (and what is off-limits) is required?

Posted: Tue Sep 20, 2005 5:18 pm
by Brice Manuel
Sheesh, whats with all the C++ garbage on the forums as of late? If you want to promote and discuss C++, don't do it here!

Posted: Tue Sep 20, 2005 5:19 pm
by Dare2
Quite a few libs and etc supporting PureBasic are C.

:)

Edit:

BTW, Brice, that 3Impact engine is pretty neat!

Posted: Tue Sep 20, 2005 11:13 pm
by traumatic
va!n wrote:but i dont think it make any problems to poste chaos original article as long as i dont change/modify nor dont give credits for it...!? i still think its a world where everyone should help everyone... isnt it? :roll:
You'll know the german "Urheberrecht" which is similar (but not the same)
to the copyright in other countries. Law says you mustn't simply copy&paste
from everywhere you want (Same with music, pictures and so on).
This is about everything I wanted to point out.

Of course I like people sharing ideas and code, nice links etc. - that was not
my point and it's not my intention to stop you or anyone else from this.

It's obviously much better to have code snippets in C++ than polls about
favourite drinks... ;)

Posted: Tue Sep 20, 2005 11:22 pm
by va!n
@traumatic:
thanks for showing me this point about the german "urheberrecht"... i didnt thought about it on this way! (i know fr isnt just only a demogroup... its a company too ;)

Posted: Tue Sep 20, 2005 11:26 pm
by traumatic
va!n wrote:(i know fr isnt just only a demogroup... its a company too ;)
the "urheberrecht" is something _everyone_ has, no matter if it's a single private person or a company.

aber das führt jetzt etwas zu weit :)

Posted: Tue Sep 20, 2005 11:35 pm
by va!n
no problem...!
hab schon verstanden um was es dir ging :)

Posted: Wed Sep 21, 2005 2:32 am
by Dare2
Okay, understood.

Posted: Wed Sep 21, 2005 2:33 am
by dagcrack
generally, you make a thread, share links, discuss about the links.
Plus, if you want to talk about c++ do it with bill gates daughter, I preffer to talk about coffee while I work, alright, whats the problem?

I'll post the bible tomorrow, the whole thing, so you guys can see it. hoyhoy

Posted: Wed Sep 21, 2005 4:13 am
by PB
> if you want to talk about c++ do it with bill gates daughter

LOL! :lol:

Just to clarify: I don't mind C++ discussion -- I can't dictate what people
want to discuss here -- but it looked like a whole bunch of code snippets
were starting to be posted here and turn this area into an unofficial "C"
section of the forums. :shock: One big thread, which snippets inline, would
be a better way to do it, and it seems some others here agree with me.