Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My guess is that your <code>ConnectionHandler</code> object is being destroyed somewhere. Also, you have defined <code>ConnectionHandler::start</code> in a silly way.</p> <p>First, <code>ConnectionHandler::start</code> should be defined this way:</p> <pre><code>void ConnectionHandler::start(ConnectionHandler * pThis){ pThis-&gt;main(); } </code></pre> <p>The C++11 <code>::std::thread</code> class is perfectly capable of preserving the type of the function argument so there is no need to resort to <code>void *</code>.</p> <p>Secondly, add in this code:</p> <pre><code>void ConnectionHandler::~ConnectionHandler(){ const void * const meptr = this; this-&gt;connectionList_mutex.lock(); ::std::cout &lt;&lt; "ConnectionHandler being destroyed at " &lt;&lt; meptr &lt;&lt; ::std::endl; this-&gt;connectionList_mutex.unlock(); } </code></pre> <p>And change the constructor to read:</p> <pre><code>ConnectionHandler::ConnectionHandler(){ const void * const meptr = this; ::std::cout &lt;&lt; "ConnectionHandler being created at " &lt;&lt; meptr &lt;&lt; ::std::endl; std::thread t(&amp;this-&gt;start, this); t.detach(); } </code></pre> <p>This will show you when the <code>ConnectionHandler</code> object is being destroyed. And my guess is that your code is destroying it while your detached thread is still running.</p> <p>The <code>meptr</code> thing is because <code>operator &lt;&lt;</code> has an overload for <code>void *</code> that prints out the pointer value. Printing out the pointer value for <code>this</code> will allow you to match up calls to the constructor and destructor if you're creating multiple <code>ConnectionHandler</code> objects.</p> <p><em>Edit:</em> Since it turned out I was correct, here is how I would recommend you write the play ConnectionHandler class:</p> <pre><code>#include &lt;iostream&gt; #include &lt;atomic&gt; #include &lt;thread&gt; #include &lt;chrono&gt; #include &lt;mutex&gt; class ConnectionHandler { public: ConnectionHandler(); ~ConnectionHandler(); ConnectionHandler(const ConnectionHandler &amp;) = delete; const ConnectionHandler &amp;operator =(const ConnectionHandler &amp;) = delete; int addNewSocket(); private: int main(); static void start(ConnectionHandler * pThis); ::std::mutex connectionList_mutex; volatile ::std::atomic_bool thread_shutdown; ::std::thread thread; }; ConnectionHandler::ConnectionHandler() : thread_shutdown(false), thread(&amp;this-&gt;start, this) { } ConnectionHandler::~ConnectionHandler() { thread_shutdown.store(true); thread.join(); } void ConnectionHandler::start(ConnectionHandler * pThis){ pThis-&gt;main(); } int ConnectionHandler::addNewSocket(){ ::std::lock_guard&lt; ::std::mutex&gt; lock(connectionList_mutex); ::std::cout &lt;&lt; "test1" &lt;&lt; ::std::endl; return 0; } int ConnectionHandler::main(){ while(!thread_shutdown.load()){ ::std::lock_guard&lt; ::std::mutex&gt; lock(connectionList_mutex); ::std::cout &lt;&lt; "test2" &lt;&lt; ::std::endl; ::std::this_thread::sleep_for(::std::chrono::milliseconds(100)); } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COFirst: Thanks a lot, you guys here at stackoverflow are great! Hopefully someday i will have enough skill to help people, like you do. Second: Now that i know that the ConnectionHandler is destroyed right after it is created i am starting to investigate this, without your kind hint i would probably have just given up this project, because i have been trying to fix the error for a week now :)
      singulars
    2. COOk, i found the fault: I have created the ConnectionHandler within a function and as soon as the function ended the instance has been destroyed. In connection to this[link](http://stackoverflow.com/questions/6403055/object-destruction-in-c) i have figured out, that i have to create a pointer that points to a handler created with the "new" operator (ConnectionHandler* newHandler = new ConnectionHandler;). In this case (as i unsterstood) the pointer is destroyed, but the instance of the ConnectionHandler stays in memory. Again: Thanks for your kind help!
      singulars
    3. CO@sh4kesbeer: That is still likely the wrong solution. You need to think about which thread 'owns' the ConnectionManager object. In this case, the only real choice is the detached thread since that thread needs the object as long as it lives and you have no way of shutting it down. This means you need to have a way to 'ask' the detached thread to destroy your object and shut itself down. Creating it with `new` sort of works, but it creates a memory leak and is overall a messy solution.
      singulars
 

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