user1014366
user1014366

Reputation: 95

Does allocating function scope member causes memory leak?

I used to allocate function scope static members , as it serves very well when the calls to these function(s) are limited to debug purposes and memory is very limited. I tested the next c++ code:

#include <stdio.h>

class myClass
{
public:
    myClass(int index) : m_index(index)
    {
        printf("Constructing element %d\n", m_index);
    }

    ~myClass(void)
    {
        printf("Destructing element %d\n", m_index);
    }
    int m_index;
};



void foo()
{
    static myClass class1(1);
    static myClass *class2 = new myClass(2);

    return;
}

void main()
{
    foo();

    return;
}

The printout is

Constructing element 1
Constructing element 2
Destructing element 1
Press any key to continue . . .

Have I caused any memory leak ? Where does element 1 allocated ? Where does element 2 allocated ?

Upvotes: 3

Views: 129

Answers (4)

Ankur
Ankur

Reputation: 205

Yes that is a memory leak, because you are not freeing up the memory that you have allocated.In this case the OS will reclaim the memory when the program exits.

If you want, you can make the program free up the allocated memory by using c++11 unique pointer. but the order in which the destructor will be called is difficult to predict , don't go on this route if you are relying on the order.

#include <stdio.h>
#include <memory>

class myClass
{
   public:
   myClass(int index) : m_index(index)
   {
       printf("Constructing element %d\n", m_index);
   }

   ~myClass(void)
   {
       printf("Destructing element %d\n", m_index);
   }
   int m_index;
};

void foo2()
{
   static std::unique_ptr<myClass> class2(new myClass(2));
}

void foo()
{
   static myClass class1(1);

   return;
}

int main()
{
    foo2();
    foo();
    foo2();

    return 0;
}

Upvotes: 1

Pustovalov Dmitry
Pustovalov Dmitry

Reputation: 1047

  1. class1 allocated in a static memory, it's destructor executes after main returns.

  2. class2 allocated on a heap because of new, but pointer to it in a static memory also, when main returns it destroys a pointer but not a referenced objects.

Second one is a bad practice but in reality after main returns all memory consumed by process should return to OS anyway.

Upvotes: 0

Brian Bi
Brian Bi

Reputation: 119184

That depends on how "memory leak" is defined. Some define it to mean that you allocate memory and then don't deallocate it. In that case, yes, your program has a memory leak since class2 is never deleted. Others define it to mean that you allocate memory and then can't deallocate it because the last remaining pointer to the memory is overwritten or goes out of scope. In that case, your program does not have a memory leak.

Whether your code is "bad" is a matter of opinion. I advise against using explicit new wherever a smart pointer can be used instead. However, some recommend this use of static pointers in order to deliberately prevent the object from being destroyed, in order to avoid bugs that would otherwise occur if a static object is accessed after the end of its lifetime (i.e., during the destructor of another static object, or the invocation of a function registered by atexit).

Upvotes: 6

The Marlboro Man
The Marlboro Man

Reputation: 971

You have leaked memory by allocating a myClass pointer (class2) with new and not deleting it. In the scope of this example is negligible as the memory will be reclaimed by the system when the program ends, but one can argue it could get nastier in bigger projects.

As for freeing it, just delete it before it goes out of scope or use a smart pointer: it will get destroyed when main finishes, along with the rest of static data.

Upvotes: 1

Related Questions