Thread: how can i detect memory leaks?

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    451

    how can i detect memory leaks?

    i have 1 nice tools\class's for use win32. but when i execute the application, time by time, the memory used is increased. and know that these is a memory leak. what i don't know is how to know where is the problem, on code
    can anyone advice me please?

  2. #2

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Consider also using smart pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you read Stroustrup's answer to the FAQ How do I deal with memory leaks?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    i belive that my memory leak is here:
    Code:
    else if(wParam==JoystickTimer)
                        {
                            JOYINFOEX  b={0};
                            b.dwSize=sizeof(JOYINFOEX );
                            b.dwFlags=JOY_RETURNALL;
    
                            //cicle all possible joysticks
                            for (int i=0; i<16; i++)
                            {
                                int direction=0;
                                if(joyGetPosEx(i,&b)== JOYERR_NOERROR) //if theres any error then continue to next joystick
                                {
                                    if(i==0)
                                        inst->Joystick(-1, -1,-1);
                                    else
                                        inst->Joystick(0, -1,-1);
                                    break;
                                }
    
                                long h =b.dwButtons;
    
                                //testing the directions
                                //will be 8 directions(inclued diagonals)
    
    
                                if (b.dwXpos == 0 && b.dwYpos ==65535)
                                {
                                    direction=5;
                                }
                                else if (b.dwXpos == 65535 && b.dwYpos ==0)
                                {
                                    direction=6;
                                }
                                else if (b.dwXpos == 65535 && b.dwYpos ==65535)
                                {
                                    direction=7;
                                }
                                else if(b.dwXpos == 0 && b.dwYpos == 0)
                                {
                                    direction=8;
                                }
    
                                else if (b.dwXpos == 0)
                                {
                                    direction=1;
                                }
                                else if (b.dwXpos == 65535)
                                {
                                    direction=2;
                                }
                                else if (b.dwYpos == 0)
                                {
                                    direction=3;
                                }
                                else if (b.dwYpos == 65535)
                                {
                                    direction=4;
                                }
                                inst->Joystick(i, direction,h); //lambda function
                            }
                        }
    - the form is a subclass window(like my others controls). the inst is the pointer to the form objects\instances.
    why i think that it's these 'if'? i comment it, and i don't see the memory leak.
    so how can i detect how many Joysticks are connected to pc?

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You seem to be using "magic numbers" a lot in your program. I suggest creating suitable named constants/macros to use in place of your direction and possibly other values in your program.

    What does joyGetPosEx do? Is it your function? Win32? What does the JOYINFOEX structure look like? I don't see any memory allocation happening in the code that you posted, so I don't think that's the source of your memory leak, if indeed one exists.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by Elkvis View Post
    You seem to be using "magic numbers" a lot in your program. I suggest creating suitable named constants/macros to use in place of your direction and possibly other values in your program.

    What does joyGetPosEx do? Is it your function? Win32? What does the JOYINFOEX structure look like? I don't see any memory allocation happening in the code that you posted, so I don't think that's the source of your memory leak, if indeed one exists.
    Moderator: i have found the memory leak. is about win32 Joystick function. so, if you can, change these topic to Windows subforum.. thanks.

    joyGetPosEx() is for get the joystick information. but if i don't have a joystick connected to pc, these function give me a memory leak.
    so i need test more and do a nice function for detect how many joysticks are connected to pc

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    *moved as requested*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-15-2012, 05:16 PM
  2. memory leaks
    By evariste in forum C Programming
    Replies: 10
    Last Post: 02-07-2010, 12:46 PM
  3. Memory usage and memory leaks
    By vsanandan in forum C Programming
    Replies: 1
    Last Post: 05-03-2008, 05:45 AM
  4. memory leaks
    By bazzano in forum C Programming
    Replies: 11
    Last Post: 04-19-2007, 08:39 AM
  5. Odd memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 05-25-2006, 12:56 AM