Search code examples
c++qtmemoryshared-ptr

std::shared_ptr in QList does not delete content on deletion


The following places a bunch of shared_ptrs containing an arbitrary object in a QList. With the curly braces I create a stack, which triggers the deletion of the list when the instruction pointer leaves it. Somehow the shared_ptrs get not deleted. Why? I track the memory consumption in gnome-systemmonitor and htop.

{
    QList<std::shared_ptr<QChar>> l;
    for (int i =0; i< 1024*1024*10; ++i)
        l.append(std::make_shared<QChar>('h'));
}
qDebug() <<"done";
sleep(10);

I just tested it. The same problem with QSharedPointer, but not with regular types (non [shared] pointers).


Solution

  • Small memory allocations come from a heap managed within the process, not directly from the operating system. Tools that measure the process's memory usage won't see them being deallocated, since the memory is still allocated to the process.