MCP
MCP

Reputation: 4536

How serious should I take Valgrind and small memory leaks (let's say < 100 bytes)?

If I initialize a temp variable (say a char *, NOT with "new") in the loop of a function, do I then need to explicitly free it or will it be taken care of when it goes out of scope? The latter is what I've been taught... When I run my code through Valgrind I'm getting "15 bytes definitely lost" but they seem to be in areas similar to above...temp variables that go out of scope. These are not things allocated with "new"... Does Valgrind get some of it's numbers based on what the code looks like or is it actually catching orphaned memory? When Valgrind is used do most people expect to see some small numbers lost or is 0 bytes lost the name of the game? Thanks guys.

==7669== 15 bytes in 1 blocks are definitely lost in loss record 1 of 1                    │        std::cout << "    ...printing newly initia
==7669==    at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memc│lized customer..." << std::endl;                  
heck-x86-linux.so)                                                                         │        if (true)                                 
==7669==    by 0x8049C4B: Customer::copyString(char const*&, char*&) (customer.cpp:539)    │        {                                         
==7669==    by 0x804992A: Customer::getName(char*&) (customer.cpp:379)                     │                const char * name = "bunches, oate
==7669==    by 0x8048C2D: main (main.cpp:78)                                               │s";                                               
==7669==                                                                                   │                Customer cus5(name);              
==7669== LEAK SUMMARY:                                                                     │                cus4 = cus5;                      
==7669==    definitely lost: 15 bytes in 1 blocks                                          │        }                                         
==7669==    indirectly lost: 0 bytes in 0 blocks                                           │                                                  
==7669==      possibly lost: 0 bytes in 0 blocks                                           │        cus4.displayData();                       
==7669==    still reachable: 0 bytes in 0 blocks                                           │        std::cout << std::endl;                   
==7669==         suppressed: 0 bytes in 0 blocks  

Both the "copyString()" method and the "getName()" method only have temp variables...everything else is freed... "main.cpp" has some memory that I haven't freed but as it's my testing harness I'm not too worried about cleaning that up at this point. If things are listed by Valgrind (such as the 3 lines: copyString, getName, and main) does that mean they are definitely loosing memory or just that they might be?

This is the "getName()" function:

int Customer::getName(char *& toReturn)
{
        if (name)
        {
                const char * nameCopy = name; //casting
                delete toReturn;
                copyString(nameCopy, toReturn);
                delete nameCopy;
                return 1;
        }
        else
        {
                toReturn = NULL;
                return 0;
        }
}

This is the copyString() function:

int Customer::copyString(const char *& toCopy, char *& set)
{
        //prevent strlen or strcpy from being called on NULL
        if (!toCopy)
                return -1;

        //copy data into new char * and set "set"
        int length = strlen(toCopy) + 1;
        char * new_name = new char[length];
        strcpy(new_name, toCopy);

        delete set; //delete anything pointed to by this
        set = new_name;
        return 1;
}

Also I should add, just to stave off questions, I'm using the cstring library because it is a requirement of the class I'm taking. I'm going above and beyond by profiling my code at this point...just trying to learn as much as possible... Thanks!

Upvotes: 0

Views: 384

Answers (2)

Alin Huruba
Alin Huruba

Reputation: 35851

First of all, you should try to fix memory leaks, unless you're certain that your application won't run for a long time and they won't cause many problems. However, don't use this to justify not fixing them. They can cause plenty of problems once your application get's bigger.

Second of all, i noticed that you used C++ and C-style strings. It is better to use stl::string, since they already manage the memory, while with char* strings you need to do it all by yourself and mistakes can appear very easy.

Upvotes: 1

Kevin Grant
Kevin Grant

Reputation: 5431

The constant string const char * name = "bunches, oates"; is passed to delete in Customer::getName(char* &toReturn).

Upvotes: 3

Related Questions