반응형

C++에서 map<key, value>를 쓰다 보면, value를 포인터로 관리해야 하는 경우가 있다. 예를 들어, 아래와 같은 형태의 코드이다.


#define <map>

using namespace std;


class Foo {

public:

    int a;

    double b;

    Foo(int a, double b){

        this->a = a;

        this->b = b;

    }

}


int main() {

    std::map<int, Foo*> fooMap;

    fooMap[101] = new Foo(10, 505.26);

    fooMap[104] = new Foo(8, 1233.61);

    // ...



    fooMap.erase(101);

    fooMap[105] = new Foo(17, 564.98);

    // ...


    fooMap.clear();

    return 0;

}


위와 같이 실행하면 메모리 누수가 발생한다. map.erase(key)를 이용해서 특정한 pair를 삭제하더라도 포인터를 통해 할당되어 있는 메모리 영역은 자동으로 해제(delete)되지 않는다. 즉, 명시적으로 코드 어디선가 new를 통해 오브젝트를 할당했으면, map의 value로 쓰이는지 여부에 관계 없이 명시적으로 delete를 해 주어야 한다.


실제로 C++의 erase와 clear 함수의 소스코드 위에 달려 있는 주석에도 같은 내용이 명시적으로 쓰여져 있다.


map erase 함수 주석:

  /**
   *  @brief Erases elements according to the provided key.
   *  @param  __x  Key of element to be erased.
   *  @return  The number of elements erased.
   *
   *  This function erases all the elements located by the given key from
   *  a %map.
   *  Note that this function only erases the element, and that if
   *  the element is itself a pointer, the pointed-to memory is not touched
   *  in any way.  Managing the pointer is the user's responsibility.
   */

...


map clear 함수 주석:

  /**
   *  Erases all elements in a %map.  Note that this function only
   *  erases the elements, and that if the elements themselves are
   *  pointers, the pointed-to memory is not touched in any way.
   *  Managing the pointer is the user's responsibility.
   */

...



포인터를 명시적으로 delete하는 것이 번거롭다면 C++11 이후의 버전들이 smart pointer를 제공하는데, (또는 이전 버전의 경우 boost::shared_ptr) 이것을 사용하면 자동으로 메모리 할당을 해제해 주므로 명시적인 delete를 할 필요가 없다.


포인터에 대해서 더이상 참조당하고 있지 않을 경우에 자동으로 메모리를 해제하는 작업은 ns-3에도 Ptr<T>로 구현되어 있는데, std::shared_ptr, boost::shared_ptr은 아직 써본 적이 없어서 자세히 모르겠지만 아마 개념과 목적은 유사할 것으로 예상된다.

(나중에 사용한 뒤에 포스트를 수정할 예정)




반응형
블로그 이미지

Bryan_

,