Merge pull request #1490 from npwoods/imgtool_bug_fix

[Imgtool] Fixed a recently introduced bug
This commit is contained in:
Vas Crabb 2016-10-09 14:39:31 +11:00 committed by GitHub
commit bb78203617
2 changed files with 10 additions and 2 deletions

View File

@ -1024,7 +1024,9 @@ imgtoolerr_t imgtool::image::internal_open(const imgtool_module *module, const c
goto done; goto done;
} }
// we've succeeded - set the output image // we've succeeded - set the output image, and record that
// we are "okay to close"
image->m_okay_to_close = true;
outimg = std::move(image); outimg = std::move(image);
done: done:
@ -1072,6 +1074,7 @@ imgtool::image::image(const imgtool_module &module, object_pool *pool, void *ext
: m_module(module) : m_module(module)
, m_pool(pool) , m_pool(pool)
, m_extra_bytes(extra_bytes) , m_extra_bytes(extra_bytes)
, m_okay_to_close(false)
{ {
} }
@ -1082,7 +1085,7 @@ imgtool::image::image(const imgtool_module &module, object_pool *pool, void *ext
imgtool::image::~image() imgtool::image::~image()
{ {
if (module().close) if (m_okay_to_close && module().close)
module().close(this); module().close(this);
pool_free_lib(m_pool); pool_free_lib(m_pool);
} }

View File

@ -123,6 +123,11 @@ namespace imgtool
object_pool *m_pool; object_pool *m_pool;
void *m_extra_bytes; void *m_extra_bytes;
// because of an idiosycracy of how imgtool::image::internal_open() works, we are only "okay to close"
// by invoking the module's close function once internal_open() succeeds. the long term solution is
// better C++ adoption (e.g. - std::unique_ptr<>, std:move() etc)
bool m_okay_to_close;
static imgtoolerr_t internal_open(const imgtool_module *module, const char *fname, static imgtoolerr_t internal_open(const imgtool_module *module, const char *fname,
int read_or_write, util::option_resolution *createopts, imgtool::image::ptr &outimg); int read_or_write, util::option_resolution *createopts, imgtool::image::ptr &outimg);
}; };