Again regarding the parse(istream &in) method in "src/XCSP3CoreParser.cc".
In this method, you directly manage dynamic memory, allocating a buffer
char *buffer = new char[bufSize];
and deleting it at the end
delete[] buffer;
Problem: if, parsing the file, an exception is thrown (maybe catched, but also rethrown) the buffer isn't deleted and you get a memory leak.
General C++ suggestion: avoid direct dynamic memory managment like the plague. Instead, use STL containers and, when you really have to allocate/deallocate memory, manage it through smart pointers.
Specific suggestion: transform buffer in a std::unique_ptr
std::unique_ptr<char[]> buffer { new char[bufSize] };
and cancel the final delete[] buffer; instruction.
Using a std::unique_ptr, also in case of exception the destructor of buffer is called, the related memory is deallocated and the memory leak is avoided.
Again regarding the
parse(istream &in)
method in "src/XCSP3CoreParser.cc".In this method, you directly manage dynamic memory, allocating a buffer
and deleting it at the end
Problem: if, parsing the file, an exception is thrown (maybe catched, but also rethrown) the
buffer
isn't deleted and you get a memory leak.General C++ suggestion: avoid direct dynamic memory managment like the plague. Instead, use STL containers and, when you really have to allocate/deallocate memory, manage it through smart pointers.
Specific suggestion: transform
buffer
in astd::unique_ptr
call the
get()
method when you need the pointerand cancel the final
delete[] buffer;
instruction.Using a
std::unique_ptr
, also in case of exception the destructor ofbuffer
is called, the related memory is deallocated and the memory leak is avoided.