Co myślisz? Czy to prawda, czy są wycieki pamięci?QList i shared_ptr
Źródło:
#include <QList.h>
#include <boost/shared_ptr.hpp>
#include <iostream>
class A {
private:
int m_data;
public:
A(int value=0) { m_data = value; }
~A() { std::cout << "destroying A(" << m_data << ")" << std::endl; }
operator int() const { return m_data; }
};
int _tmain(int argc, _TCHAR* argv[])
{
QList<boost::shared_ptr<A> > list;
list.append(boost::shared_ptr<A>(new A(6)));
std::cout << int(*(list.at(0))) << std::endl;
return 0;
}
wyjściowa:
6
destroying A(6)
Jako zdaniu: nie używaj 'QList' gdzie 'sizeof (T)> sizeof (void *)' (i 'sizeof (shared_ptr ) == 2 * sizof (void *)', zwykle), skoro w tym przypadku 'QList' skopiuje' 'new' elementy do listy (w przeciwieństwie do trzymania jej przez wartość). Zamiast tego użyj 'QVector' lub' std :: vector'. –