-1

Getting a Memory leak. valgrind says it might be somewhere in here. Any help would be really appreciated. Somewhere maybe around enter relevant sort....where algorithm is one of bubble, sort2, or sort 3

void usageAbort(string progname, string message)
{
    cerr << message << endl
         << "Usage:  " << progname << " algorithm" << endl
         << "     where algorithm is one of "
         << "bubble, sort2, or sort3"
         << endl;
    exit(1);
}

/***************************************************************************/
/**                                                                       **/
/***************************************************************************/


string algorithmFromCommandLine(int argc, char *argv[])
{ 

    string program = argv[0];
    string algorithm = argv[1];
    if(argc!=2){
        usageAbort(program, "enter proper argument count");
    } 

    if((algorithm == "bubblesort") or (algorithm == "quicksort") or
        (algorithm== "insertionsort")){
            return algorithm;
    }
    else{
        usageAbort(program,"enter relevant sort");
    }
    return 0;

}
thor
  • 21,418
  • 31
  • 87
  • 173
Lauren
  • 1
  • 3
  • 1
    Are there some static pointers somewhere in your code? I don't see any memleak in this piece of your code. – Lorenz Rusch Apr 05 '16 at 06:22
  • Does changing parameters of `usageAbort()` to `const char*` works? I don't see needs of using string there. I guess valgrid assume there is possible leak because the memory in the temp `string` (in parameter) have no chance to be properly freed (as you are using `exit()` in the method) – Adrian Shum Apr 05 '16 at 06:50
  • Valgrind often shows some "unfreed" memory. The way to test it is easy - create a program with basic libraries included and with empty main. Compile it -> run it with valgrind and you can see, how much unfreed memory you can have, without feeling bad about it :) – tomascapek Apr 05 '16 at 06:52
  • Is the memory usage really growing out of control ? is it slowing down ? is it crashing ? if the answers are no, no, no I think you could use your time in a more profitable way. – Marco Apr 05 '16 at 07:01

1 Answers1

1

exit() doesn't invoke the destructors of automatic objects, in particular function arguments. So the memory used by progname and message (and probably by other variables in your code) isn't deleted properly. In general using exit() isn't a good idea, see How to end C++ code.

Community
  • 1
  • 1
Anton Savin
  • 40,838
  • 8
  • 54
  • 90