-1

I've seen other questions up on here before relating to this, but my issue doesn't revolve around a QVector containing pointers.

I have a 'Mesh' class which contains several QVector< T >'s , for holding vertices, indices, normals, etc, all held as standard objects.

As of recently I've noticed that each time I delete a mesh, the amount of memory used by my application doesn't decrease. I've been monitoring my applications memory usage with the Windows Task Manager, and have seen it go as high as 1000mb without dropping a single byte.

I have verified with a debugger that I am reaching the deconstructor of my mesh class, and my vectors are being deleted, but the memory still isn't being freed.

The deconstructor in question:

Mesh::~Mesh()
{
    QVector<QVector3D> *vertices = this->vertices;
    QVector<QVector3D> *normals = this->normals;
    QVector<QVector3D> *tangents = this->tangents;
    QVector<QVector2D> *textures = this->UVMap;
    QVector<GLushort> *indices = this->indices;

    vertices->clear();
    vertices->squeeze();
    delete vertices;

    normals->clear();
    normals->squeeze();
    delete normals;

    tangents->clear();
    tangents->squeeze();
    delete tangents;

    textures->clear();
    textures->squeeze();
    delete textures;

    indices->clear();
    indices->squeeze();
    delete indices;
}

I have used the Visual Leak Detector, and it seems to mostly show leaks within the library I'm using (Qt), in addition to telling me that some of my constructors are leaking, like the following:

Mesh::Mesh()
{
    vertices = new QVector<QVector3D>();
    indices = new QVector<GLushort>();
    UVMap = new QVector<QVector2D>();
    normals = new QVector<QVector3D>();
    tangents = new QVector<QVector3D>();
}

But I don't see any problems here, since those objects were not initialized prior to those calls.

I really don't know much about smart pointers yet, but I'm hesitant to switch over everything in my application to use them because I don't know if they would even help really or perfectly replace my current usage without any issues. For instance I may pass some pointers around (like an entire mesh for instance) to another class, but I may not want that other class to delete that mesh upon its own destruction.

Edit: I thought I had a decently written out question but it seems people prefer to bandwagon downvotes instead. I will try other programs to monitor my problem.

Yattabyte
  • 1,280
  • 14
  • 28
  • 3
    Not sure I see a memory leak here but have you considered just allocating these vectors on the stack instead of the heap? Unless there's some problem with `QVector` that I don't know about, this code seems unnecessarily complicated. – MrEricSir Jun 29 '15 at 05:11
  • I pass the references around quite a bit because I'd rather not keep making copies all the time – Yattabyte Jun 29 '15 at 08:03
  • @Yattabyte - you can pass references (or pointers) to things that are part of an object around quite easily. – Michael Kohne Jun 29 '15 at 17:06
  • @Michael Kohne I'm aware, don't worry. However that really isn't the scope of this question though. – Yattabyte Jun 29 '15 at 17:15

1 Answers1

1

There is not at all any memory leak. Following is the output given by valgrind

==7881== Memcheck, a memory error detector
==7881== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==7881== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==7881== Command: ./abstractitemmodel
==7881== 
==7881== 
==7881== HEAP SUMMARY:
==7881==     in use at exit: 0 bytes in 0 blocks
==7881==   total heap usage: 8 allocs, 8 frees, 168 bytes allocated
==7881== 
==7881== All heap blocks were freed -- no leaks are possible
==7881== 
==7881== For counts of detected and suppressed errors, rerun with: -v
==7881== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

If you really want to check for memory leak, you should use specialist software such as valgrind(for linux).valgrind is the an excellent tool for this purpose.

Vedanshu
  • 2,230
  • 23
  • 33
  • I'm not using Linux so I can't use valgrind. It really doesn't make sense, because I'm watching the memory usage stay the same despite calling clear() , squeeze(), and delete on each vector. – Yattabyte Jun 29 '15 at 07:57
  • Check this [link](http://stackoverflow.com/questions/413477/is-there-a-good-valgrind-substitute-for-windows). Programmers do not check task manager for memory leakage. They use software made specifically for this purpose. I'm guessing you're a new to programming. There is absolutely no memory leakage in the code given above. – Vedanshu Jun 29 '15 at 09:43
  • I program for fun since my local university doesn't offer much, and throughout those lectures not once was a better alternative suggested. I've been practicing through personal projects for years, but I've never had a leak of this scale before. Avoid snide comments and assumptions please. – Yattabyte Jun 29 '15 at 17:40