Page 2 of 2

Re: Temp Files not being closed and deleted

Posted: 2016-11-03T07:24:32-07:00
by madp
Hello. I'm using Magick++ version ImageMagick 7.0.1-4 Q16 x86_64 under Ubuntu 14.04 LTS and I'm having an issue that when I use Image.read(). It works perfectly the first time, but after the second the file pointer doesn't close (I'm using checking /proc/[PID]/fd to check that the file pointer stills open). Could anyone help me figure out why it's happening?
I'm using it in a loop (sometimes, thousand of images) then I get a "Too many files open" error eventually. It only happens when I'm reading .webp files.

Code: Select all

#include <exception>
#include <iostream>
#include <boost/filesystem.hpp>
#include <Magick++.h>

using namespace std;
namespace fs = boost::filesystem;

int convert(std::string inpath, std::string outpath, std::string libpath) {
    Magick::InitializeMagick(libpath.c_str());
    Magick::Image i;
    try {
        i.read(inpath);
        if (i.isValid()) {
            fs::path p = outpath;
            p.replace_extension(".png");
            i.debug(false);
            i.quality(100);
            i.write(p.string());
        } else {
            cerr << inpath << " is not a valid image.\n";
        }
    } catch (std::exception &e) {
        cerr << "Error: " << e.what() << "\n";
        return 1;
    }
    Magick::TerminateMagick();
    return 0;
}

int main(int argc, char** argv) {
    string srcDir = argv[1];
    string libpath = argv[2];
    fs::directory_iterator srcIt(srcDir), endSrcIt;
    for (; srcIt != endSrcIt; ++srcIt) {
        fs::path filePath = (*srcIt).path();
        try{
            convert(filePath.string(), filePath.string(), libpath);
        } catch(std::exception &e){
            std::cout<< e.what();
            return 1;
        }
        
    }
    return 0;
}