Re: Memleak while writing xml file



2012-02-05 13:39, Hugo van den Brand skrev:
L.S.

I'm using the libxml++-2.6 wrapper in a C++ program that runs a simulation (include with gtkmm-2.4)
At the start of every simulation I write the settings to an xml file.
However, while doing this I noticed that every time I run a new Simulation additional memory is required (about 404 kb).

I use the following code and I wonder if I'm missing something:


short SimulationWriter::write_xmlfile(string i_filename)
{
    {   // Check whether the file can be opened.
        fstream fin;
        fin.open(i_filename.c_str(),ios::out);
        bool not_opened=!(fin.is_open());
        fin.close();
        if (not_opened)
            return 1;; // Failure opening the file for writing.
    }
    xmlpp::Document xmldoc;
    // Add the root node
    xmlpp::Element* pe_root=xmldoc.create_root_node("simulation");
    // Add all members to the simulation node
    add_simulation_members(pe_root);
    // Write the file
    xmldoc.write_to_file_formatted(i_filename,"ISO-8859-1");
}

When I comment from xmlpp::Document ... to the end I don't experience the memory leak.
Am I missing some clean-up code or is there something else happening?

Any help would be really appreciated.

Best wishes,
Hugo van den Brand

No, I don't think you're missing any cleanup code.
I made some tests with libxml++'s example program dom_build (http://git.gnome.org/browse/libxml++/tree/examples/dom_build/main.cc), or actually a slightly modified version of it. valgrind reports

LEAK SUMMARY:
   definitely lost: 0 bytes in 0 blocks
   indirectly lost: 0 bytes in 0 blocks
     possibly lost: 0 bytes in 0 blocks
   still reachable: 7,965 bytes in 105 blocks
        suppressed: 0 bytes in 0 blocks

I use Ubuntu 11.10 and the latest version of libxml2 and libxml++ from the git repository. Don't know if it matters.
I added
    document.write_to_file_formatted(filename, "ISO-8859-1");
to make it more similar to your program, and I let it write the file three times, with document deleted and re-created like in your code. The reported memory leakage did not change.

I added "--leak-check=full --show-reachable=yes" in the call to valgrind. It showed that most of the still reachable memory had been allocated by the call to
  std::locale::global(std::locale(""));

Is it possible that most of your memory leaks are located in your call to
   add_simulation_members(pe_root);?

If you can use valgrind or a similar memory checking program, perhaps you can see where your leaked memory has been allocated. Or check what happens, if you comment out only part of the code. E.g. parts of add_simulation_members(pe_root).

Kjell



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]