Menu

Memory leak when using mglGraphPS

Help
2011-02-15
2012-10-16
  • Nobody/Anonymous

    Hi.
    First of all, thank you for making this very nice plotting library.
    It works fine and makes plots without problems, but there seems to be a memory
    leak.
    As I intend to use this in a program that will run for days/weeks/longer and
    automatically produce plots,
    I cannot have memory leaks.
    I made a small test case that replicates the problem:

    So, am I using MathGL wrong, or is there a bug in the mglGraph memory
    handling?

    Source code (file "testingMathGL.cpp"):

    include <cstdlib> // For atoi()

    // MathGL

    include "mgl/mgl.h"

    include "mgl/mgl_eps.h"

    include "mgl/mgl_zb.h"

    void testingMathGL()
    {
    mglGraphPS postscriptPlot;

    unsigned int i, j;
    unsigned int longs = 100, lats = 100;

    // Set up data containers
    mglData VTECData(longs, lats), GIVEData(longs, lats), lon(longs), lat(lats);

    // Insert dummy data
    for(i = 0; i <= longs; i++) {
    for(j = 0; j <= lats; j++) {
    double VTEC = 2;
    VTECData.Put(VTEC, i, j);

    double GIVE = 3;
    GIVEData.Put(GIVE, i, j);
    }
    }
    for(i = 0; i <= longs; i++) lon.Put(i, i);
    for(j = 0; j <= lats; j++) lat.Put(j, j);

    // Clear plot
    postscriptPlot.Clf();

    // Set up plot
    postscriptPlot.CAxis(0, 4);
    postscriptPlot.XRange(lon);
    postscriptPlot.YRange(lat);

    // Plot panel 1
    postscriptPlot.SubPlot(1,2,0);
    postscriptPlot.Dens(VTECData);
    postscriptPlot.Axis();
    postscriptPlot.Box();
    postscriptPlot.Colorbar(0, 0.9, 0, 0.8, 1);
    postscriptPlot.Label('x', "Longitude (degrees)", 0);
    postscriptPlot.Label('y', "Latitude (degrees)", 0);
    postscriptPlot.Puts(mglPoint(0.95, 0.45), "VTEC (m)", "a", 6);
    postscriptPlot.Title("TestTitle", "", 4);

    // Plot panel 2
    postscriptPlot.SubPlot(1,2,1);
    postscriptPlot.Dens(GIVEData);
    postscriptPlot.Axis();
    postscriptPlot.Box();
    postscriptPlot.Colorbar(0, 0.9, 0, 0.8, 1);
    postscriptPlot.Label('x', "Longitude (degrees)", 0);
    postscriptPlot.Label('y', "Latitude (degrees)", 0);
    postscriptPlot.Puts(mglPoint(0.95, 0.45), "GIVE (m)", "a", 6);

    // Write to file
    postscriptPlot.WriteEPS("test.eps");
    }

    int main(int argc, char ** argv)
    {
    unsigned int i = 0;
    unsigned int maxi = atoi(argv);

    while(i < maxi) {
    testingMathGL();
    i++;
    }

    return 0;
    }

    Compile command:

    g++ testingMathGL.cpp -O0 -g3 -Wall -Wextra -Wconversion -Wshadow -lmgl -o
    testingMathGL

    valgrind command:

    valgrind --leak-check=full ./testingMathGL 1

    valgrind output:

    ==6083== Memcheck, a memory error detector
    ==6083== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
    ==6083== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
    ==6083== Command: ./testingMathGL 1
    ==6083==
    ==6083== Mismatched free() / delete / delete
    ==6083== at 0x4A04700: operator delete(void) (vg_replace_malloc.c:368)
    ==6083== by 0x3C4B05BC42: mglGraphPS::~mglGraphPS() (in
    /usr/lib64/libmgl.so.5.0.0)
    ==6083== by 0x401337: testingMathGL() (testingMathGL.cpp:63)
    ==6083== by 0x4013EC: main (testingMathGL.cpp:72)
    ==6083== Address 0x5c3c040 is 0 bytes inside a block of size 2,300,000 alloc'd
    ==6083== at 0x4A05255: realloc (vg_replace_malloc.c:476)
    ==6083== by 0x3C4B05B874: mglGraphPS::add_prim(mglPrim&) (in
    /usr/lib64/libmgl.so.5.0.0)
    ==6083== by 0x3C4B05E06C: mglGraphPS::Glyph(float, float, float, int, long,
    char) (in /usr/lib64/libmgl.so.5.0.0)
    ==6083== by 0x3C4B060D39: mglFont::Puts(unsigned int const
    , float, float,
    float, int, char) (in /usr/lib64/libmgl.so.5.0.0)
    ==6083== by 0x3C4B062460: mglFont::Puts(wchar_t const, int, int, char) (in
    /usr/lib64/libmgl.so.5.0.0)
    ==6083== by 0x3C4B0EB0A1: mglGraphAB::Putsw(mglPoint, wchar_t const
    , char
    const, float, char, float) (in /usr/lib64/libmgl.so.5.0.0)
    ==6083== by 0x3C4B0B2B80: mglGraph::Label(char, char const
    , float, float,
    float) (in /usr/lib64/libmgl.so.5.0.0)
    ==6083== by 0x40123C: testingMathGL() (testingMathGL.cpp:58)
    ==6083== by 0x4013EC: main (testingMathGL.cpp:72)
    ==6083==
    ==6083==
    ==6083== HEAP SUMMARY:
    ==6083== in use at exit: 960,000 bytes in 1 blocks
    ==6083== total heap usage: 350 allocs, 349 frees, 32,632,194 bytes allocated
    ==6083==
    ==6083== 960,000 bytes in 1 blocks are definitely lost in loss record 1 of 1
    ==6083== at 0x4A055EC: operator new(unsigned long) (vg_replace_malloc.c:264)
    ==6083== by 0x3C4B0E8858: mglGraphAB::SetSize(int, int) (in
    /usr/lib64/libmgl.so.5.0.0)
    ==6083== by 0x3C4B0EC198: mglGraphAB::mglGraphAB(int, int) (in
    /usr/lib64/libmgl.so.5.0.0)
    ==6083== by 0x3C4B05BC78: mglGraphPS::mglGraphPS(int, int) (in
    /usr/lib64/libmgl.so.5.0.0)
    ==6083== by 0x400D9A: testingMathGL() (testingMathGL.cpp:12)
    ==6083== by 0x4013EC: main (testingMathGL.cpp:72)
    ==6083==
    ==6083== LEAK SUMMARY:
    ==6083== definitely lost: 960,000 bytes in 1 blocks
    ==6083== indirectly lost: 0 bytes in 0 blocks
    ==6083== possibly lost: 0 bytes in 0 blocks
    ==6083== still reachable: 0 bytes in 0 blocks
    ==6083== suppressed: 0 bytes in 0 blocks
    ==6083==
    ==6083== For counts of detected and suppressed errors, rerun with: -v
    ==6083== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 6 from 6)

    MathGL version:

    $ yum list installed | grep mathgl
    mathgl.i686 1.10.2.1-3.fc13 @updates
    mathgl.x86_64 1.10.2.1-3.fc13 @updates
    mathgl-devel.i686 1.10.2.1-3.fc13 @updates
    mathgl-devel.x86_64 1.10.2.1-3.fc13 @updates

     
  • Nobody/Anonymous

    Hi.
    I've now downloaded and compiled the newest version (1.11.0.1).
    (Which was made a little complicated because I had to compile it myself as a
    32-bit library on a 64-bit machine)

    I ran the test again, and this time there were no errors:

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

    So if anyone else is having memory issues, try updating to a newer build.

     

Log in to post a comment.