Fix mismatched malloc/delete leak

From the Valgrind report:

```
Mismatched free() / delete / delete []
   at 0x4838EAB: operator delete(void*) (vg_replace_malloc.c:576)
   by 0xBC5C87: std::default_delete<cJSON>::operator()(cJSON*) const (unique_ptr.h:81)
   by 0xBC4ACA: std::unique_ptr<cJSON, std::default_delete<cJSON> >::~unique_ptr() (unique_ptr.h:274)
   by 0xBBB755: (anonymous namespace)::getAliases(Glib::ustring const&) (dcp.cc:422)
   by 0xBC1CCA: rtengine::DCPStore::init(Glib::ustring const&, bool) (dcp.cc:1846)
   by 0xC3ED4F: rtengine::init(rtengine::Settings const*, Glib::ustring, Glib::ustring, bool) [clone ._omp_fn.0] (init.cc:81)
   by 0x89743FF: GOMP_parallel_sections (sections.c:158)
   by 0xC3E906: rtengine::init(rtengine::Settings const*, Glib::ustring, Glib::ustring, bool) (init.cc:52)
   by 0x9CE10E: Options::load(bool) (options.cc:2358)
   by 0x982CD6: main (main.cc:603)
 Address 0xd62d700 is 0 bytes inside a block of size 64 alloc'd
   at 0x483777F: malloc (vg_replace_malloc.c:299)
   by 0xE97390: cJSON_New_Item (cJSON.c:205)
   by 0xE98718: cJSON_ParseWithOpts (cJSON.c:1020)
   by 0xE9886F: cJSON_Parse (cJSON.c:1083)
   by 0xBBB4D3: (anonymous namespace)::getAliases(Glib::ustring const&) (dcp.cc:422)
   by 0xBC1CCA: rtengine::DCPStore::init(Glib::ustring const&, bool) (dcp.cc:1846)
   by 0xC3ED4F: rtengine::init(rtengine::Settings const*, Glib::ustring, Glib::ustring, bool) [clone ._omp_fn.0] (init.cc:81)
   by 0x89743FF: GOMP_parallel_sections (sections.c:158)
   by 0xC3E906: rtengine::init(rtengine::Settings const*, Glib::ustring, Glib::ustring, bool) (init.cc:52)
   by 0x9CE10E: Options::load(bool) (options.cc:2358)
   by 0x982CD6: main (main.cc:603)
```
This commit is contained in:
George Hilliard
2018-11-02 00:54:02 -05:00
parent c362c05661
commit 77eccdf13d

View File

@@ -393,6 +393,13 @@ double xyCoordToTemperature(const std::array<double, 2>& white_xy)
return res;
}
struct cJSON_deleter {
template<typename T>
void operator()(T *t) {
cJSON_Delete(const_cast<typename std::remove_const<T>::type*>(t));
}
};
std::map<std::string, std::string> getAliases(const Glib::ustring& profile_dir)
{
const std::unique_ptr<std::FILE, std::function<void (std::FILE*)>> file(
@@ -419,7 +426,7 @@ std::map<std::string, std::string> getAliases(const Glib::ustring& profile_dir)
buffer[read] = 0;
cJSON_Minify(buffer.get());
const std::unique_ptr<cJSON> root(cJSON_Parse(buffer.get()));
const std::unique_ptr<cJSON, cJSON_deleter> root(cJSON_Parse(buffer.get()));
if (!root || !root->child) {
if (settings->verbose) {
std::cout << "Could not parse 'camera_model_aliases.json' file." << std::endl;