I just ran two valgrind tests on the following code snippets:
First start:
class Foo { public: Foo(){} virtual ~Foo(){} }; class Bar: public Foo { public: Bar(){} virtual ~Bar(){} }; int main(int argc, char** argv) { std::auto_ptr<Foo>arr[3]; std::auto_ptr<Foo> one(new Bar()); std::auto_ptr<Foo> two(new Bar()); std::auto_ptr<Foo> three(new Bar()); arr[0] = one; arr[1] = two; arr[2] = three; return 0; }
Second run:
class Foo { public: Foo(){} virtual ~Foo(){} }; class Bar: public Foo { public: Bar(){} virtual ~Bar(){} }; int main(int argc, char** argv) { std::auto_ptr<Foo> one(new Bar()); return 0; }
While Valgrind does show possible memory leaks in both cases, the warnings displayed are exactly the same (the same number of warnings, the same warning text, the same stack) for both of them, pointing somewhere outside my code in linux.so files. Thus, we can assume that the way to use the auto_ptr array is fine. However, as pointed out in the comments, since C ++ 0x (which is the current C ++ standard), auto_ptr is considered deprecated due to its weird copy behavior (you can find more information, for example, in this article ). It is recommended to use std::unique_ptr .
Now, if you have some additional memory leaks in your code, then most likely this is due to your own classes. Unfortunately, you did not include them in the question, so we cannot say. In this case, you should check the constructors and destructors of your classes for memory leaks, or at least show us your classes. Another reason could be a typo in the index of the array that you have in the code ( pbase[3] , which is not related to bonds).
source share