Server Help

Trash Talk - Memory leak detection?

Solo Ace - Sun Dec 12, 2004 8:31 am
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. 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
Dr Brain - Sun Dec 12, 2004 8:34 am
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.
Solo Ace - Sun Dec 12, 2004 8:51 am
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). 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.
Mr Ekted - Sun Dec 12, 2004 9:30 am
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.
Dr Brain - Sun Dec 12, 2004 10:04 am
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.
Mr Ekted - Sun Dec 12, 2004 2:26 pm
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.
Dr Brain - Sun Dec 12, 2004 10:50 pm
Post subject:
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.
50% Packetloss - Mon Dec 13, 2004 1:50 am
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.
Smong - Mon Dec 13, 2004 4:24 am
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.
Mr Ekted - Mon Dec 13, 2004 4:29 am
Post subject:
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.
Cyan~Fire - Mon Dec 13, 2004 6:32 am
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.)

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.
Mr Ekted - Mon Dec 13, 2004 7:07 am
Post subject:
Raw zlib is quite fast, but I only ever use the buffer compress/decompress functions. I do any required I/O myself.
lp_street_soldier - Mon Dec 13, 2004 11:37 am
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.
Dr Brain - Mon Dec 13, 2004 12:48 pm
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.
Solo Ace - Mon Dec 13, 2004 2:57 pm
Post subject:
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
Mr Ekted - Mon Dec 13, 2004 6:52 pm
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.
Cyan~Fire - Mon Dec 13, 2004 10:59 pm
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.
D1st0rt - Tue Dec 14, 2004 1:10 am
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
Mr Ekted - Tue Dec 14, 2004 9:46 am
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++.
Dr Brain - Tue Dec 14, 2004 9:59 am
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++).
Mr Ekted - Tue Dec 14, 2004 10:50 am
Post subject:
Only weak programmers need the compiler or language to enforce good design. icon_smile.gif
Dr Brain - Tue Dec 14, 2004 10:59 am
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.
Mr Ekted - Tue Dec 14, 2004 12:04 pm
Post subject:
I feel your pain.
Cyan~Fire - Tue Dec 14, 2004 3:40 pm
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.)
Dr Brain - Tue Dec 14, 2004 7:07 pm
Post subject:
I've seen worse. Much worse.
Cyan~Fire - Wed Dec 15, 2004 5:48 am
Post subject:
My point was that
Dr Brain wrote:
Of course, it's still possible to write obscure code, but the compiler wont give up without a fight

that wasn't true.

You can never trust a compiler to do well what the programmer should do himself.
Dr Brain - Wed Dec 15, 2004 8:00 am
Post subject:
You think the compiler never complained to the programmer who wrote that? It's possible, but I think it's unlikely.
Dr Brain - Wed Dec 15, 2004 9:43 am
Post subject:
Check this out:

Some nice C code for you. See if you can figure out what it is before you compile and run.

(This was part of the 2001 IOCCC)

Code: Show/Hide
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

#define __ "Y\n : ! ,.?>"
#define D(f) ; } int f(int i) { return
#define E(a,b,c,d,e) b##d,c,e,a
#define e(a,b,c,d) a##b##c##d
#define B *p++ - ' ' || !p++
#define _(k) o(*#k - 64)
#define G(g,l,k,o,e,d,n,c,i,b,a,f,j,m,p) \
a#b#c#d#e#f#g#a#i#j#k#l#m#n#o e,i l##c,m##b; d g j n h(k 30:1); o p f}

         G(E(f ( 9) -s
            ? -s + 'q'
            ? f( 5 ) -s &&'d' -
                s?U(  3 ) ||_(J)
            :A ( s- f( 5 ))
            :(c= 4 *
               4):

   g(0)D(

      V)c <= 3 *4 + 3&&c && V(X ( i) ) ;
   }

   int , time ( 0)& , c - 15 || P

   (1

):0,
   & ( !!d       ( 0 )
     ||(a=! _(S)) ,b= 0 )||
   a
      ?d(a  = 0)
         ? _
(I):         w ( j =!W(j=_ (V ))) ,

            l(_
                (Y+ s ) )
              )e

   (
   main(){;
       ;;;s , ra,
    n, d), && C ( j +R( s-j+t( i- 1) )
         )D( W)m[ c* 4| z( j ) ]
       &&_( X )E (d      ( 0
             )&&  R( 6)/ 6
            ? c= 17|_ ( T )
            :01,||+
                , v =i & 3 ^ 2
                ,+j & 3&&W ( j)||y ( 10 )D (k) --
               j ? k ( O[j]=R(15 ))
: 1 D
          ( H )o ( 7+! !  i+3 )+

              i?

    c=i /4 & 63,
    a= 1

:0)

    D ( f )( p =M ,q ( i),*p+86)
    D ( l) * f, B?

            B ?B ?B? B ?B ?B
       ?7 :B?B ?36: 59 * 89 *5 * 2 :B?6587
        *77 :9 + 2:B ?B?B?9: 5 : 37 : B ? 5 +5:4:20:
                B?B?B?32

        :e( 4, 3 , 1 *4 * 59  * 1 , 7):B?
                17: 34 : 19 :B ?
                  8 *2 :B ?
                  2:26: B?
                   B?B ?B ?

   82 *+ 576: B ?33 :22  :5 * 5:3 *4 :3:B ?B ?B? B ?6 +
   + 17 : B?B?13:6 : 9 *2:B?15 : 7 *2 :

             B ?
             B ?
             27:24: 31 : B?


        B? +  29 :,
        m [a^ 2  ] =T( 8 * 7) ;j= 3
            ;
          ;
         Q, ;int
          P ( int
        i )

      {time_t(x )            =+ 0+
   time(0) ;                       j=E( u (i  ) : i D(R )e(r ,
      a ,n ,d) ()% i+1,                 ( e( l  , o,
      caltim , e ) (& x))     ->e(          tm_h , o, u  , r)
   ;retur ,            i?_( W
):   _( U) D(Y)           *p&&  *p<3 * 11 ?
     ( ++ p ),              n j= + 34  +( j< 6
                     ||j>19 ),Y (i)
: *p

    D ( F)   i == 9  ?1 :
   F(   (i+ 7)% ( 7 +  3) ) +!O[ i
   ]D(u)   *p &&* p -32? p ++)
    D (h  )   i ?*++*S=i%  ( 38)- 1,
    D ( A )!    n?i&&!g ( (0  ) ) ||K (  j=4| ! !i ) :  N(!i )D(
                Q)++j<

          (s )  &&Q ( t( i)^i )
    D( U   )i>= 0 ?U( i- 1 )||~f (i+1 ) == ~  s
                  &&H (m[c* 4 |z(i)] )
:0;D


      ( X) + b && b<, for (*S = M  ; *
p; ),

      gets ( L, + 98 , stdin ) ?
         p= L, E (r( 0 ): 0 ,h( i /  38 ) : 0;D( C)!
         ( m[j]+m [a= i] ) ?m[j[m]= i]=j:0;D (d) !
           b+(*  O-
            c)*
              *  O; D (z )


                     -~- ~ -~i +
   v
      & 3 D(y)( putchar ) ( i)
    D ( q )   i& ,!(O[j = i]
       || _ ( O))+a :0;
    D (w)O[   j] ==c && o

   (( !i ++ ? ! !_ (M

):

   0) +15)|| ++ j<9 &&w (
i)D   ( r )*p?         *p -4?
               * p -6 ?
                 y(*p <9 + 2?__[
                   * p
]:          * p+ 7 +79):o( j+1) : o (26+j),&q ( -!
        * p ++ + i )D(x)i? a=x ( ( -- i)), ++ p )
       D( t ) i,   | C((j=i*
       T(i-4 ))  +R
    (3 ) + 4 )
   D(J) !j                                --E( 0 D  ( K )!-!N, || i &,
                                                _( R ) :i* c== (O
   [ j ] )            &&n==f (j+   6  +    5   * 4)?O
    [j]=            !i *    c-_   (    L)    :J(i )
    D ( N )         j=9, & d(0 )? j= 6 | ! i , J(i )D
         ( o)(*S = p , ++ S,p = M , r (q (i) ),-- S,  p = *S))
          ( (l (_ (Q )),n =s,i ^5 ) ) ;, E(j ,
      sign ,  v ,ed ( b ) ;;
         int   c =1 ,O[9])
            ;int(  s
)=
    E (n ;char, 1 ,a,6 ,o ())*  Z[
   E(M [37 * 9 *3 ] ,0,L [ 99] ,3] , * *S=Z ) ;
    E (*p=+1 ,char( m)[6 , co , 4 ],ol)
              +,
      V ( v=*O = k( j= 9 )) ;
   return ! c&& - ~ ~ - -! printf
    ( ( "%d \n") , F (P ( 0) +6)            *50 + !  ! b*
     ( +b - (int)time ( 0 )) )
;, (         b= 60 + time ( 0)+ 60 * 4)              ;, n =Y
              (u (s =Y (i) ) )

   :  (exit(                                      0)
   ,i)D( g )x (  _(N) ^ 9)? !( a =0) : _  ( P )D (T)!  i|,
   (2); s=35; Q(3);

   s=59; Q(6);


   a=1;




    )

Cyan~Fire - Wed Dec 15, 2004 1:17 pm
Post subject:
Dr Brain wrote:
You think the compiler never complained to the programmer who wrote that?

Well it didn't complain to me, so I don't see why it would've to him.

And about your code, yes, I realize that it's quite fun and easy to obfuscate C code, but that wasn't my point. My point was, as I had said before, you shouldn't rely on the compiler to prevent it. A language is not better just because it has a "smarter" compiler or more rigid style control
Solo Ace - Wed Dec 15, 2004 1:22 pm
Post subject:
Netcat's code made me freak out already (well, I saw Qndre's weird code before, but that didn't make sense) but wtf is this?!
Dr Brain - Wed Dec 15, 2004 2:44 pm
Post subject:
Cyan~Fire wrote:
[..]


Well it didn't complain to me, so I don't see why it would've to him.

And about your code, yes, I realize that it's quite fun and easy to obfuscate C code, but that wasn't my point. My point was, as I had said before, you shouldn't rely on the compiler to prevent it. A language is not better just because it has a "smarter" compiler or more rigid style control


You only saw the finished product. Someone who codes like that is bound to run into a few compiler errors along the way.

No, that C code had nothing to do with the point I was making. I just posted that for fun, and it vaguely related to this topic.
D1st0rt - Wed Dec 15, 2004 2:59 pm
Post subject:
hold on lemme get some stuff from my cs class last year, we did stuff from http://www.robocode.net

my favorite

that folder has a bunch of the other ones in it too, I forget if mine's in there or not
Dr Brain - Wed Dec 15, 2004 4:55 pm
Post subject:
That code has functions and multicharacter variable names.

My teammate wrote some demonstation code (code to help teach others) without a single function, just a main loop, and with every variable name consisting of one letter. He also felt that tabulation was completly optional, and consistant tabulation wasn't important.
D1st0rt - Thu Dec 16, 2004 8:08 pm
Post subject:
I realize its readable, but it makes me want to claw my eyes out

this one is also funny
Blindmonkey21 - Thu Dec 16, 2004 10:27 pm
Post subject:
Woah it is just code distort no reason to claw your eyes out...
Dr Brain - Thu Dec 16, 2004 11:13 pm
Post subject:
Blindmonkey, you don't know what you're talking about.

Yah, that code is pretty bad, d1.
Cyan~Fire - Fri Dec 17, 2004 6:23 am
Post subject:
Are they actually using those arrays or is it just to test garbage collection?
Anonymous - Sat Dec 18, 2004 2:33 pm
Post subject:
Probably used to introduce some sort of delay.
D1st0rt - Tue Dec 21, 2004 8:24 pm
Post subject:
That guy was funny (and very random), I think he just didn't want it to work. He was very successful at that, but he didn't crash the whole program. The computers we use are so crappy, I doubt it got through the first array before dying
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group