Commit 6ee8c45e authored by Martin Marinov's avatar Martin Marinov

Fix a bug where the Journal can be turned on before the stream pointer is...

Fix a bug where the Journal can be turned on before the stream pointer is initialized, resulting in a failure to output and memory leaks
parent 433c0a1d
......@@ -360,10 +360,10 @@ public:
add_underscore();
if (!lower_vowel(c)) // skip vowels in the interior of a word
obj.push_back(std::tolower(c, loc ));
obj.push_back(std::tolower(c, loc));
}
else
add_underscore(); // start of a namespace / class
add_underscore(); // start of a namespace / class
}
return obj;
}
......@@ -793,13 +793,29 @@ Base::IOutputStream& Stream::os()
return *impl_;
}
StreamPtr strm_ptr; // global stream pointer
// plain data, initialized during constant initialization before everything else
Stream* StreamPtr::ptr_ = nullptr;
StreamPtr::~StreamPtr()
{
delete ptr_;
ptr_ = nullptr;
}
void StreamPtr::reset(Stream* _ptr)
{
delete ptr_;
ptr_ = _ptr;
}
static StreamPtr strm_ptr; // static stream pointer to call the destructor
bool set_on(
const bool _on, const char* const _cmpn_name, const char* const _base_path)
{
if (on() == _on)
return true;
if (!_on)
{
strm_ptr.reset(nullptr);
......@@ -811,7 +827,7 @@ bool set_on(
strm_ptr.reset(new Stream(_cmpn_name, _base_path));
}
catch (...)
{ // things went wrong , so just return false, strm_ptr should be nullptr
{ // things went wrong, so just return false, strm_ptr should be nullptr
return false;
}
return true;
......
......@@ -236,10 +236,24 @@ private:
const char* const _fnct, const Data& _rtrn, const DataVector& _args);
};
typedef std::unique_ptr<Stream> StreamPtr;
/*!
Customized pointer that ensures only a single object is ever retained and
prevents overrides when enabling the journal on application startup within the
static data initialization phase.
\note Exposed for efficiency only (inline access), please do *not* use it.
*/
class StreamPtr
{
public:
~StreamPtr();
// the global stream pointer is exposed for efficiency only, do *not* use it
extern StreamPtr strm_ptr;
static bool valid() { return ptr_ != nullptr; }
static Stream& stream() { return *ptr_; }
static void reset(Stream* _ptr);
private:
static Stream* ptr_;
};
/*!
Obtain the value of the environment variable toupper(cmpn_name)_JOURNAL_BASE
......@@ -266,10 +280,10 @@ bool set_on(const bool _on, //!< turn on or off
Check if the journal is on or off, as efficient as possible as it is the only
function which is called when the journal is turned off!
*/
inline bool on() { return strm_ptr != nullptr; }
inline bool on() { return StreamPtr::valid(); }
//! Access the stream directly, this is not recommended in general
inline Stream& stream() { return *strm_ptr; }
inline Stream& stream() { return StreamPtr::stream(); }
//! Get the journal output path, or an empty string if the journal is off
inline String output_path() { return on() ? stream().output_path() : String(); }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment