From: Mikko Rasa Date: Thu, 27 Sep 2012 22:24:13 +0000 (+0300) Subject: Create writers dynamically and delete them before deleting output X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=06fe7b7a0c0af0bab3df79cebe87f8ff93b31b00;p=libs%2Fdatafile.git Create writers dynamically and delete them before deleting output This ensures that the IO objects are destroyed in the correct order, and filters won't try to access deleted objects. --- diff --git a/tool/tool.cpp b/tool/tool.cpp index d0499cb..59c5cbc 100644 --- a/tool/tool.cpp +++ b/tool/tool.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include "compiler.h" #include "tool.h" @@ -44,8 +43,7 @@ int DataTool::main() void DataTool::do_transfer() { IO::Base *out = open_output(out_fn); - DataFile::Writer writer(*out); - set_writer_parameters(writer); + DataFile::Writer *writer = create_writer(*out); for(vector::const_iterator i=in_fns.begin(); i!=in_fns.end(); ++i) { @@ -56,22 +54,22 @@ void DataTool::do_transfer() { DataFile::Statement st = parser.parse(); if(st.valid) - writer.write(st); + writer->write(st); } delete in; } + delete writer; delete out; } void DataTool::do_compile() { IO::Base *out = open_output(out_fn); - DataFile::Writer writer(*out); - set_writer_parameters(writer); + DataFile::Writer *writer = create_writer(*out); - Compiler compiler(writer); + Compiler compiler(*writer); for(vector::const_iterator i=in_fns.begin(); i!=in_fns.end(); ++i) { IO::Base *in = open_input(*i); @@ -80,6 +78,7 @@ void DataTool::do_compile() delete in; } + delete writer; delete out; } @@ -99,12 +98,14 @@ IO::Base *DataTool::open_input(const string &fn) return new IO::BufferedFile(fn, IO::M_READ); } -void DataTool::set_writer_parameters(DataFile::Writer &writer) +DataFile::Writer *DataTool::create_writer(IO::Base &out) { + DataFile::Writer *writer = new DataFile::Writer(out); if(compress) - writer.set_compressed(); + writer->set_compressed(); if(binary) - writer.set_binary(true); + writer->set_binary(true); if(float_size) - writer.set_float_precision(float_size); + writer->set_float_precision(float_size); + return writer; } diff --git a/tool/tool.h b/tool/tool.h index 2cfcf15..0c5a77a 100644 --- a/tool/tool.h +++ b/tool/tool.h @@ -3,6 +3,7 @@ #include #include +#include class DataTool: public Msp::RegisteredApplication { @@ -23,7 +24,8 @@ private: void do_compile(); Msp::IO::Base *open_output(const std::string &); Msp::IO::Base *open_input(const std::string &); - void set_writer_parameters(Msp::DataFile::Writer &); +public: + Msp::DataFile::Writer *create_writer(Msp::IO::Base &); }; #endif