Help identifying memory leak

I have a class here that is defined like this:

struct USERFPOINT
{
    POINTFLOAT UserPoint;
    POINTFLOAT LeftHandle;
    POINTFLOAT RightHandle;
    bool isBezier;
};

struct SHAPEOUTLINE {
    GLuint OutlineVBO;
    int OutlineSize;
    int OutlineWidth;
    ARGBCOLORF OutlineColor;
};

struct SHAPECONTOUR{

    std::vector<USERFPOINT> UserPoints;
    std::vector<std::vector<GLdouble>> DrawingPoints;
    SHAPEOUTLINE Outline;

};
struct SHAPEGRADIENT{
    GLuint TextureId;
    bool IsParent;
    bool active;
    int type;
    std::vector<ARGBCOLORF> colors;
};

struct SHAPEDIMENSIONS {
    POINTFLOAT Dimensions;
    POINTFLOAT minima;
    POINTFLOAT maxima;
};

class OGLSHAPE
{
private:
    int WindingRule;
    GLuint TextureCoordsVBOInt;
    GLuint ObjectVBOInt;
    UINT ObjectVBOCount;
    UINT TextureCoordsVBOCount;
    SHAPEGRADIENT Gradient;
    SHAPEDIMENSIONS Dimensions;
    void SetCubicBezier(USERFPOINT &a,USERFPOINT &b, int &currentcontour);

    void GenerateLinePoly(const std::vector<std::vector<GLdouble> > &input, int width);
public:
    std::string Name;
    ARGBCOLORF MainShapeColor;
    std::vector<SHAPECONTOUR> Contour;
    OGLSHAPE(void);

    void UpdateShape();
    void SetMainColor(float r, float g, float b, float a);
    void SetOutlineColor( float r, float g, float b, float a,int contour );
    void SetWindingRule(int rule);
    void Render();
    void Init();
    void DeInit();
    ~OGLSHAPE(void);
};

Here is what I did as a test. I created a global std::vector<OGLSHAPE> test .

In the function I was using, I created

OGLSHAPE t.

I then pushed 50,000 copies of t into test.

I then instantly cleared test and used the swap trick to really deallocate it.

I noticed that all the memory was properly freed as I would expect.

I then did the same thing but before pushing t into test, I pushed a SHAPECONTOUR (which I had just created without modifying or adding anything into the contour) before pushing t into test.

This time after clearing test, 3 more megabytes had been allocated. I did it again allocating twice as many and now 6MB we remaining. The memory usage of the program peaked at 150MB and it went down to 12MB, but it should be at 8.5MB. Therefore, this must be classified as a memory leak, although I do not see how. There is nothing that I see that could do that. SHAPECONTOUR is merely a structure of vectors with a nested structure of vectors.

Why would this cause a leak, and how could I fix it?

Thanks

Answers


If you've deleted everything, there is no leak, by definition. I don't see any unwrapped pointers, ergo everything gets deleted. Therefore, you have no leaks.

Likely, the OS has simply decided to leave that memory available to your program, for whatever reason. (It hasn't "reclaimed" it.) Or maybe it needs to allocate in groups of4MB, and so going from 12MB to 8MB wouldn't leave enough required memory. Or...

You're cutting out other reasons entirely; you should use a real memory leak tool to find memory leaks.


When you allocate large chunks of memory, the CRT keeps some of it around for when you need to allocate again. You can't use Task Manager to indicate leaks.

If your class uses self-clearing members like vector, they do not leak memory.


How about explicitly clearing the vectors of SHAPECONTOUR in the destructor of OGLSHAPE? Rerun your test with this & check if you still have the observed leak.


How are you clearing that vectors? Try using in destructors something like this:

std::vector<SHAPECONTOUR>().swap(Contour);

And do the same with other vectors from your code(UserPoints, DrawingPoints, colors).


I would suggest to use a leak detection tool to find out if you really have a leak. For example for Windows dev you can use Visual Leak Detetor (VLD).

If you do indeed have a leak then it is probably not where you think it is.

Also you may just be wrong in that there is no leak at all. For example if you are looking just at the stats via something like task manager, this does not mean there is a leak in your program. The OS that runs your program is a separate beast than the programming language that you may have a leak inside of. I.e. the OS can take longer to reclaim freed memory.

Edit:

If you want to make sure the vector has capacity 0 it is pretty common to do the following:

v.swap(std::vector<char>());
assert(v.capacity() == 0);

What debugging tools did you use? Try yet deleaker! it is simple!


Need Your Help

Shortest cost path

c++ algorithm shortest-path np

I have to find the shortest path from point D to R. These are fixed points.

Building a WHERE clause programmatically, then passing it into SQL as a parameter

c# sql where parameterized clause

On my asp.net project, there are instances where a server side filter function in C# is building an SQL WHERE clause, then passing it into an SQL stored procedure for example,