I got a set that includes pointers to an allocated memory, I am using the clear method forexample : setname.clear();
and the set itself is getting cleared and his pointers but I still get memory leaks because the allocated memory stays uncleared for some reason.
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- C#中 public virtual string Category { get; }这么写会报错:
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
Set only clears what it allocates itself. If you allocate something yourself, you'll have to clear it yourself.
std::set's clear() method does remove elements from the set. However, in your case set contains pointers that are being removed, but the memory they point to is not released. You have to do it manually before the call to
clear()
, for example:There is a library in Boost called Pointer Container that solves this problem.
Clear()
only removes the pointers not the object it points to. You'll have either to iterate on each object before removing it todelete
it, or use something likestd::tr1::shared_ptr
(also in Boost).