C++ idiom to avoid memory leaks?

后端 未结 11 1606
被撕碎了的回忆
被撕碎了的回忆 2021-01-28 21:01

In the following code, there is a memory leak if Info::addPart1() is called multiple times by accident:

typedef struct
{
}part1;

typedef struct
{
}         


        
11条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-28 21:18

    You should read about the smart pointer idiom and about RAII. I suggest taking a look into the new technical report (TR1).
    Take a good look here and here.
    Also take a look at boost's smart pointers.
    I recommend loki-lib's SmartPtr or StrongPtr classes.

提交回复
热议问题