How to Debug Memory Leaks in GDB

The GDP utility is a program used for C++ and low-level programs that checks for memory leaks. Memory leaks are a program phenomena that happens when your program does not "let go" of memory used while the program runs on the desktop. You use GDP to check memory issues for your program, so your program does not cause slowness issues on your users' computers.

Instructions

    • 1

      Click the Windows "Start" button and type "cmd" in the search text box. Press "Enter" to open the command line.

    • 2

      Type "gdp run" and press "Enter." In the "Program" prompt, type the C source code file you want to evaluate. After you type the source code file in the prompt, the program monitors the source code.

    • 3

      Print the variable to the screen. Type "print /x" and press "Enter" where "x" is the variable you want to watch. This command "watches" the variable and displays the memory map for the variable.

    • 4

      Review the memory map of the variable. The amount of memory used for the variable should match the variable's data type and the amount of space needed to store the data type. The GDP utility flags variables that create memory leaks.

    • 5

      View the code for the variable. After you find the memory leak, you must view the code and change the code's value or change the function that causes the leak. The code changes you need to make are dependent on your function and variable. For instance, the following code sets up dynamic memory allocation for the "p" pointer, but the static assignment of the variable causes a leak:

      void functionname()
      {
      char *p = malloc(10);
      p[10] = 7; // <--- the bug
      free(p);
      }

Related Searches:

References

Comments

Related Ads

Featured