Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>After your edit, the solution seem clear:</strong></p> <p>Your first line in main() is :</p> <pre><code>mLogger-&gt;WriteEntry("Registering commands...\0"); </code></pre> <p><strong>Here mLogger is a pointer that has never been initialized.</strong> This is "undefined behaviour", meaning anything can appen, often bad things.</p> <p>To fix this you can either make it a "normal" variable, not a pointer or create a Logger instance using <code>new</code> (either at the declaration or as the first line in main).</p> <p>I suggest you to not use a pointer to be sure the logger is always there and is automatically destroyed.</p> <p>By the way, it seems like you want to create every instance of objects on the heap using pointers. It's not recommanded if it's not necessary. You should use pointers ONLY if you want to explicitely state the creation (using <code>new</code>) and destruction (using <code>delete</code>) of the instance object. If you just need it in a specific scope, don't use a pointer. You might come from another language like Java or C# where all objects are referenced. If so, you should start learning C++ like a different language to avoid such kind of problem. You should learn about RAII and other C++ scpecific paradigm that you cannot learn in those languages. If you come from C you should too take it as a different language. That might help you avoid complex problems like the one you showed here. May I suggest you read some C++ pointer, references and RAII related questions on stackoverflow.</p> <hr> <p>First, you don't need to create the std::list on the heap. You should just use it as a normal member of the class.</p> <pre><code>class Logger { private: list&lt;string&gt; mEntries; // no need to use a pointer public: Logger() // initialization is automatic, no need to do anything { } ~Logger() // clearing and destruction is automatic too, no need to do anything { } //... }; </code></pre> <p>Next, <code>entryData</code> don't exist in this code so I guess you wanted to use <code>entry</code>. If it's not a typo then you're not providing the definition of entryData that is certainly the source of your problem.</p> <p>In fact I would have written your class that way instead:</p> <pre><code>class Logger { private: list&lt;string&gt; mEntries; public: // no need for constructor and destructor, use the default ones // Public Methods void WriteEntry(const string&amp; entry) // use a const reference to avoid unnecessary copy (even with optimization like NRVO) { mEntries.push_back( entry ); // here the list will create a node with a string inside, so this is exactly like calling the copy constructor } void DisplayEntries() { cout &lt;&lt; endl &lt;&lt; "**********************" &lt;&lt; endl &lt;&lt; "* Logger Entries *" &lt;&lt; endl &lt;&lt; "**********************" &lt;&lt; endl &lt;&lt; endl; for(list&lt;string&gt;::iterator it = mEntries.begin(); it != mEntries.end(); ++it) // if you want to avoid unnecessary copies, use ++it instead of it++ { cout &lt;&lt; *it &lt;&lt; endl; } } }; </code></pre> <p>What's certain is that your segfault is from usage outside of this class.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload