Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
Memory leak detection?
Goto page 1, 2  Next
 
Post new topic   Reply to topic Printable version
 View previous topic  Quick Question #3 Post :: Post Quick question.  View next topic  
Author Message
Solo Ace
Yeah, I'm in touch with reality...we correspond from time to time.


Age:37
Gender:Gender:Male
Joined: Feb 06 2004
Posts: 2583
Location: The Netherlands
Offline

PostPosted: Sun Dec 12, 2004 8:31 am   Post maybe stupid    Post subject: Memory leak detection? Reply to topic Reply with quote

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. sa_tongue.gif

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. icon_smile.gif
Back to top
View users profile Send private message Add User to Ignore List
Dr Brain
Flip-flopping like a wind surfer


Age:39
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Sun Dec 12, 2004 8:34 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Solo Ace
Yeah, I'm in touch with reality...we correspond from time to time.


Age:37
Gender:Gender:Male
Joined: Feb 06 2004
Posts: 2583
Location: The Netherlands
Offline

PostPosted: Sun Dec 12, 2004 8:51 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

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

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? icon_smile.gif Well, yeah C++ obviously.
Back to top
View users profile Send private message Add User to Ignore List
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Sun Dec 12, 2004 9:30 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List
Dr Brain
Flip-flopping like a wind surfer


Age:39
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Sun Dec 12, 2004 10:04 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Sun Dec 12, 2004 2:26 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List
Dr Brain
Flip-flopping like a wind surfer


Age:39
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Sun Dec 12, 2004 10:50 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

In C++, you shouldn't be using raw data tongue.gif

Unless of course, you know what you're doing. At which point you are no longer asking questions on a forum.
Back to top
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
50% Packetloss
Server Help Squatter


Age:40
Gender:Gender:Male
Joined: Sep 09 2003
Posts: 561
Location: Santa Clarita, California
Offline

PostPosted: Mon Dec 13, 2004 1:50 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Send email AIM Address
Smong
Server Help Squatter


Joined: 1043048991
Posts: 0x91E
Offline

PostPosted: Mon Dec 13, 2004 4:24 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Visit posters website MSN Messenger
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Mon Dec 13, 2004 4:29 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

Dr Brain wrote:
In C++, you shouldn't be using raw data tongue.gif


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
View users profile Send private message Add User to Ignore List
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Mon Dec 13, 2004 6:32 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

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

Code: Show/Hide
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
View users profile Send private message Add User to Ignore List Visit posters website
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Mon Dec 13, 2004 7:07 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List
lp_street_soldier
Thing 1


Age:33
Gender:Gender:Male
Joined: Apr 24 2004
Posts: 352
Location: Britain
Offline

PostPosted: Mon Dec 13, 2004 11:37 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Send email MSN Messenger
Dr Brain
Flip-flopping like a wind surfer


Age:39
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Mon Dec 13, 2004 12:48 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Solo Ace
Yeah, I'm in touch with reality...we correspond from time to time.


Age:37
Gender:Gender:Male
Joined: Feb 06 2004
Posts: 2583
Location: The Netherlands
Offline

PostPosted: Mon Dec 13, 2004 2:57 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

What a bunch of discussions got started here! sa_tongue.gif
Won't try to join, my English and knowledge suck too bad for making any good posts here.

At least I know what I'm trying to do, you don't see that a lot here on these forums.
I just want to know if I'm doing it right. icon_sad.gif

When I'm commenting out all the LocalFree()s _CrtDumpMemoryLeaks() doesn't output anything, although it does when I do something like
Code: Show/Hide
char *p = (char *) malloc(10);

without free'ing it (just to see if I actually could make it output anything).

Anyway, since I'm not getting any output from _CrtDumpMemoryLeaks() and I followed all the instructions on MSDN's documentation so I must've done it right. icon_smile.gif
Back to top
View users profile Send private message Add User to Ignore List
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Mon Dec 13, 2004 6:52 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Mon Dec 13, 2004 10:59 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Visit posters website
D1st0rt
Miss Directed Wannabe


Age:37
Gender:Gender:Male
Joined: Aug 31 2003
Posts: 2247
Location: Blacksburg, VA
Offline

PostPosted: Tue Dec 14, 2004 1:10 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Visit posters website
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Tue Dec 14, 2004 9:46 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List
Dr Brain
Flip-flopping like a wind surfer


Age:39
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Tue Dec 14, 2004 9:59 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Tue Dec 14, 2004 10:50 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

Only weak programmers need the compiler or language to enforce good design. icon_smile.gif
Back to top
View users profile Send private message Add User to Ignore List
Dr Brain
Flip-flopping like a wind surfer


Age:39
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Tue Dec 14, 2004 10:59 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Tue Dec 14, 2004 12:04 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

I feel your pain.
Back to top
View users profile Send private message Add User to Ignore List
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Tue Dec 14, 2004 3:40 pm   Post maybe stupid    Post subject: Java crap code Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Visit posters website
Dr Brain
Flip-flopping like a wind surfer


Age:39
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Tue Dec 14, 2004 7:07 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

I've seen worse. Much worse.
Back to top
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Trash Talk All times are GMT - 5 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 25 page(s) served in previous 5 minutes.

phpBB Created this page in 0.710769 seconds : 52 queries executed (75.8%): GZIP compression disabled