Author |
Message |
Solo Ace Yeah, I'm in touch with reality...we correspond from time to time.

Age:37 Gender: Joined: Feb 06 2004 Posts: 2583 Location: The Netherlands Offline
|
Posted: Sun Dec 12, 2004 8:31 am Post maybe stupid Post subject: Memory leak detection? |
 |
|
|
|
Yesterday I was looking for a program that'd help me finding memory leaks in my own programs.
I didn't find anything good, only some libraries.
How do you people look for memory leaks in your programs?
Dustpuppy was talking about calling a function at the beginning and the end of the program, which returns the used memory.
Too bad he didn't remember the function name.
I haven't really used dynamic memory allocation yet, and I only know some things that should be free()'d or delete[]'d so I doubt I did it right the last time.
Any suggestions/strategy tips for eliminating memory leaks in your code?
Please tell me.
|
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: Sun Dec 12, 2004 8:34 am Post maybe stupid Post subject: |
 |
|
|
|
Valgrind.
Anything that gets created with a new must be deleteded. Arrays created with new must be delete[]ed.
If you haven't used dynamic memory allocation, you don't have memory leaks yet. _________________ Hyperspace Owner
Smong> so long as 99% deaths feel lame it will always be hyperspace to me
|
|
Back to top |
|
 |
Solo Ace Yeah, I'm in touch with reality...we correspond from time to time.

Age:37 Gender: Joined: Feb 06 2004 Posts: 2583 Location: The Netherlands Offline
|
Posted: Sun Dec 12, 2004 8:51 am Post maybe stupid Post subject: |
 |
|
|
|
Valgrind? What's that?
I knew it worked like that heh, although VC++ wanted me to use a delete[] on a new'd non-array char * (guess that still counts as an array).
Anyway, new's aren't the problem, some of my objects weren't created with a function or something like new.
MSDN tells me to get rid of some objects by using LocalFree, I want to know what other objects should be gotten rid of in some way too.
I have used dynamic memory allocation in this program, I think I should've said I didn't really use it before.
This time I did use dynamic memory allocation, and I think I have some memory leaks at some places (and that's why I'm asking about it, eh).
Oh yeah I should've said which language I was talking about right? Well, yeah C++ obviously.
|
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Sun Dec 12, 2004 9:30 am Post maybe stupid Post subject: |
 |
|
|
|
Well without getting fancy, every time you call new/delete, malloc/free, calloc/free, strdup/free, LocalAlloc/LocalFree, VirtualAlloc/VirtualFree, HeapAlloc/HeapFree, etc. log it to a file. Then you can look at the log later so see if all allocation calls have corresponding free calls.
If you are using MSVC and want go play around with automated stuff, look into _CrtDumpMemoryLeaks in MSDN. _________________ 4,691 irradiated haggis!
|
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: Sun Dec 12, 2004 10:04 am Post maybe stupid Post subject: |
 |
|
|
|
When using C++, you should really only be using new to dynamicly allocate.
Things that aren't dynamicly allocated (local variables, global variables, etc) are created on the stack and are taken care of internally. If you are getting an object by means other than new, then it's not dynamicly allocated and MUST NOT BE deleted.
|
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Sun Dec 12, 2004 2:26 pm Post maybe stupid Post subject: |
 |
|
|
|
Dr Brain wrote: | When using C++, you should really only be using new to dynamicly allocate. |
new is to allocate and construct an instance of a class (ie object) or arrays thereof. malloc is fine for raw data. You just have to make sure to pair them up with delete and free respectively.
|
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: Sun Dec 12, 2004 10:50 pm Post maybe stupid Post subject: |
 |
|
|
|
In C++, you shouldn't be using raw data
Unless of course, you know what you're doing. At which point you are no longer asking questions on a forum.
|
|
Back to top |
|
 |
50% Packetloss Server Help Squatter

Age:40 Gender: Joined: Sep 09 2003 Posts: 561 Location: Santa Clarita, California Offline
|
Posted: Mon Dec 13, 2004 1:50 am Post maybe stupid Post subject: |
 |
|
|
|
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.2
Actually that entire website might be of some help.
In theory, Windows should be keeping track of the memory that a program allocates so that when the program closes, all this memory can be deallocated. But in practice, windows is crap and from what Ive been told it fucks this up all the time. So while the program is running, keeping a log of your allocations and deallocations is a good idea. But after your program closes, windows has the job of deallocating the memory in which your program was running in and im sure that it will fail in deallocating all of it in some cases.
|
|
Back to top |
|
 |
Smong Server Help Squatter

Joined: 1043048991 Posts: 0x91E Offline
|
Posted: Mon Dec 13, 2004 4:24 am Post maybe stupid Post subject: |
 |
|
|
|
I would imagine the OS prepares some space in memory for the code, stack and heap. The malloc'd stuff would go in the heap and when the program is done the whole piece of memory that the program was assigned is just marked as available again.
That way it isn't necessary to for the OS to keep track of every single malloc, just the size of the heap and possibly the size of 'more' heaps if that space runs out.
|
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Mon Dec 13, 2004 4:29 am Post maybe stupid Post subject: |
 |
|
|
|
Dr Brain wrote: | In C++, you shouldn't be using raw data  |
In C++ you shouldn't be OOP-ing the shit out of everything. A struct is still a stuct. Don't make classes (with constructors, destructors, and lots of virtual funcs) out of every little thing.
|
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
Posted: Mon Dec 13, 2004 6:32 am Post maybe stupid Post subject: |
 |
|
|
|
Dr Brain wrote: | In C++, you shouldn't be using raw data [...]Unless of course, you know what you're doing. At which point you are no longer asking questions on a forum. |
So since I still ask coding questions on this forum, then I don't know what I'm doing. (Which I'd like to dispute.) And going on that assumption, what should I use for the following fuunction? Some crappy zlib wrapper that most likely won't work for what I need? (I need negative windowBits to suppress the zlib header and a limit of PERL_BUFF bytes written at a time.)
int deflate_file(unsigned char *in, int length, FILE *out)
{
z_stream strm; //see zlib.h
Bytef compressed[PERL_BUFF]; //This holds our compressed data until we write it.
int code; //holds return values from zlib functions
// Initialize the stream
strm.zalloc = (alloc_func)Z_NULL;
strm.zfree = (free_func)Z_NULL;
strm.next_in = in;
strm.avail_in = length;
code = deflateInit2(&strm, -1, Z_DEFLATED, -15, 9, Z_DEFAULT_STRATEGY);
// Deflate it
while (code == Z_OK)
{
strm.next_out = compressed;
strm.avail_out = PERL_BUFF;
code = deflate(&strm, Z_FINISH);
if (code == Z_OK || code == Z_STREAM_END)
fwrite(compressed, sizeof(Bytef), PERL_BUFF - strm.avail_out, out);
}
// Cleanup & Return
deflateEnd(&strm);
switch (code)
{
//blah blah blah
}
return code;
} |
Oh, and as an interesting side note, that function used to use an ofstream. There also used to be a noticeable delay before it finished on my 1.2gHz machine. _________________ This help is informational only. No representation is made or warranty given as to its content. User assumes all risk of use. Cyan~Fire assumes no responsibility for any loss or delay resulting from such use.
Wise men STILL seek Him.
|
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Mon Dec 13, 2004 7:07 am Post maybe stupid Post subject: |
 |
|
|
|
Raw zlib is quite fast, but I only ever use the buffer compress/decompress functions. I do any required I/O myself.
|
|
Back to top |
|
 |
lp_street_soldier Thing 1

Age:33 Gender: Joined: Apr 24 2004 Posts: 352 Location: Britain Offline
|
Posted: Mon Dec 13, 2004 11:37 am Post maybe stupid Post subject: |
 |
|
|
|
Mr Ekted wrote: | [..]
In C++ you shouldn't be OOP-ing the shit out of everything. A struct is still a stuct. Don't make classes (with constructors, destructors, and lots of virtual funcs) out of every little thing. |
Hence, his signature.
|
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: Mon Dec 13, 2004 12:48 pm Post maybe stupid Post subject: |
 |
|
|
|
Mr Ekted wrote: | In C++ you shouldn't be OOP-ing the shit out of everything. A struct is still a stuct. Don't make classes (with constructors, destructors, and lots of virtual funcs) out of every little thing. |
Why not? Obviously you don't need virtual functions or destructors, but a constructor that sets all of the public variables in the class would take about the same amount of machine code as a struct initialization. Constructors have the advantage of making the code much more readable.
|
|
Back to top |
|
 |
Solo Ace Yeah, I'm in touch with reality...we correspond from time to time.

Age:37 Gender: Joined: Feb 06 2004 Posts: 2583 Location: The Netherlands Offline
|
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Mon Dec 13, 2004 6:52 pm Post maybe stupid Post subject: |
 |
|
|
|
Dr Brain wrote: | Why not? Obviously you don't need virtual functions or destructors, but a constructor that sets all of the public variables in the class would take about the same amount of machine code as a struct initialization. Constructors have the advantage of making the code much more readable. |
I've always found fully OOP apps to be completely unreadable, obscure, bloated, and inefficient. Inheritance is overrated. OOP allows much more obfuscation. You can "hide" complex things in simple code, making it hard to figure out what's going on. Granted it's possible to make good code and bad code in either. But I find fully OOP code to almost force bad code.
|
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
Posted: Mon Dec 13, 2004 10:59 pm Post maybe stupid Post subject: |
 |
|
|
|
Mr Ekted wrote: | OOP allows much more obfuscation. You can "hide" complex things in simple code, making it hard to figure out what's going on. |
Anyone who's ever tried to figure out what's going on in an iostream would completely agree.
|
|
Back to top |
|
 |
D1st0rt Miss Directed Wannabe

Age:37 Gender: Joined: Aug 31 2003 Posts: 2247 Location: Blacksburg, VA Offline
|
Posted: Tue Dec 14, 2004 1:10 am Post maybe stupid Post subject: |
 |
|
|
|
If done correctly, though, it can be very efficient and simple to code, since a bunch of the hard stuff was worked out in the design _________________
|
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Tue Dec 14, 2004 9:46 am Post maybe stupid Post subject: |
 |
|
|
|
D1st0rt wrote: | If done correctly, though, it can be very efficient and simple to code, since a bunch of the hard stuff was worked out in the design |
You can make good OOP-style design in C as well, without having to resort to the obscurity of C++.
|
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: Tue Dec 14, 2004 9:59 am Post maybe stupid Post subject: |
 |
|
|
|
See, I feel it's the other way around. C++ makes it easier to write nice code, and C makes it easy to slip up and write obscure code.
Of course, I really hate the backwards compatibility of C++, as you can really do any bad stuff from C in C++.
Which is why I like Java. It enforces good OOP. Of course, it's still possible to write obscure code, but the compiler wont give up without a fight (unlike C/C++).
|
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Tue Dec 14, 2004 10:50 am Post maybe stupid Post subject: |
 |
|
|
|
Only weak programmers need the compiler or language to enforce good design.
|
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: Tue Dec 14, 2004 10:59 am Post maybe stupid Post subject: |
 |
|
|
|
Yah, and guess who's code I have to always debug? Yep, weak programmer's.
You're right, I don't need it. I can design OO in C as well as I can in C++. But not everyone can.
|
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Tue Dec 14, 2004 12:04 pm Post maybe stupid Post subject: |
 |
|
|
|
I feel your pain.
|
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
Posted: Tue Dec 14, 2004 3:40 pm Post maybe stupid Post subject: Java crap code |
 |
|
|
|
Javac didn't complain one bit about this code. (I was "forced" to debug this, too. I ended up rewriting it.)
Crappy Java Code
Backup_crap.txt - 8.03 KB
File downloaded or viewed 28 time(s)
|
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: Tue Dec 14, 2004 7:07 pm Post maybe stupid Post subject: |
 |
|
|
|
I've seen worse. Much worse.
|
|
Back to top |
|
 |
|