Coding - Memory Leaks

For everything that's not in any way related to PureBasic. General chat etc...
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Coding - Memory Leaks

Post 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__)

/****************************************************************************/
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Re: Coding - Memory Leaks

Post 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...
Good programmers don't comment their code. It was hard to write, should be hard to read.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Coding - Memory Leaks

Post 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post 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.
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post 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 ;)
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post 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?
@}--`--,-- A rose by any other name ..
Brice Manuel

Post 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!
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Quite a few libs and etc supporting PureBasic are C.

:)

Edit:

BTW, Brice, that 3Impact engine is pretty neat!
@}--`--,-- A rose by any other name ..
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post 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... ;)
Good programmers don't comment their code. It was hard to write, should be hard to read.
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post 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 ;)
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post 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 :)
Good programmers don't comment their code. It was hard to write, should be hard to read.
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post by va!n »

no problem...!
hab schon verstanden um was es dir ging :)
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Okay, understood.
@}--`--,-- A rose by any other name ..
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post 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
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Locked