Etc

[C++] Memory Leaks 메모리 누수

데브렉스 2011. 12. 21. 16:40
반응형

What is a memory leak in C++ ?

A memory leak occurs when a piece (or pieces) of memory that was previously allocated by a programmer is not properly deallocated by the programmer. Even though that memory is no longer in use by the program, it is still “reserved”, and that piece of memory can not be used by the program until it is properly deallocated by the programmer. That’s why it’s called a memory leak – because it’s like a leaky faucet in which water is being wasted, only in this case it’s computer memory.


What problems can be caused by memory leaks?

The problem caused by a memory leak is that it leaves chunk(s) of memory unavailable for use by the programmer. If a program has a lot of memory that hasn’t been deallocated, then that could really slow down the performance of the program. If there’s no memory left in the program because of memory leaks, then that could of course cause the program to crash.


What is the difference between delete and delete[ ]?

The difference is big, and very important to understand. Whenever you create an object in C++ using the new keyword you will have to delete it. Whether you use “delete” or “delete[ ]” really depends on what kind of object you are creating.

This is best illustrated by an example:

void foo ( )
{
    string *sp = new string[50];
    string *s = new string;
    
    delete s;

    // have to use "[ ]" since sp points to an array of strings:
    delete[ ] sp;
}

we had to use the “[ ]” when deleting the memory that “sp” points to, because the sp pointer points to an array of strings.
So, in order to properly remove memory created with a “[ ]“, we have to use the “delete [ ] “.

Case1) An example of a memory leak in C++

(Pointer goes out of scope) Here is an example of a memory leak in C++:

void foo ()
{
  int *i = new int;
  *i = 44;
}

OR

void foo()
{
  char* str = new char[20];
strcpy(str,"memory leak");
}

So, the problem with the code above is that the “*data” pointer is never deleted – which means that the data it references is never deallocated, and memory is wasted.


Case2) An example of a memory leak in C++

(Wrong usage of new/delete) Here is an example of a memory leak in C++:

void foo()
{
  double d1 = new double;
  double* d2 = new double[10];

  delete[] d1;
delete d2; // delete d[0];
}

This could create a memory leak since using “delete” when “delete[ ]” is required will not properly delete all of the memory in the array.


Case3) An example of a memory leak in C++

(Wrong usage of new/delete) Here is an example of a memory leak in C++:

void foo()
{
  int *i;
  while(someCondition)
  {
    i = new int;
    // some code
  }
delete i;
}

In the end, one of pointers is deleted. But, it could make several issues. Before exiting a loop(‘WHILE block’), we have to make sure that each dynamic memory must be deleted.


Case4) An example of a memory leak in C++

("Lost" pointers) Here is an example of a memory leak in C++:

void foo()
{
  char* str1 = new char [30];
  char* str2 = new char [40];

  strcpy(str1, "Memory leak");

  str2 = str1; // Bad! Now the 40 bytes are impossible to free.

  delete [] str2; // This deletes the 30 bytes.
delete [] str1; // Possible access violation. What a disaster!
}

In this case, ‘str2’ lost its pointer in line 4 (str2 = str1), which means that the data(40bytes) it references is never freed, and memory is wasted.


Case5) An example of a memory leak in C++

("Lost" pointers) Here is an example of a memory leak in C++:

class Sample
{
  int* val;

public:
  Sample()
  {
    val = new int;
    *val = 44;
  }

  ~Sample()
  {
    delete val;
  }
};

void foo()
{
  Sample* a = new Sample;
  Sample* b = new Sample;
  a = b;

  delete a; // actually deletes b

  //delete b; // already deleted
}

Same case as Case4


Case6) An example of a memory leak in C++

("Lost" pointers) Here is an example of a memory leak in C++:

void foo()
{
  char* str = new char [30]; // Give str a memory address.

  // delete [] str; // Remove the first comment marking in this line to correct.

  str = new char [60]; // Give str another memory address with the first one gone forever.

  delete [] str; // This deletes the 60 bytes, not just the first 30.
}

Same case as Case4


반응형