Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Turns out, the issue was caused by incorrect use of memory, as expected. I'm 99% sure the following example is accurate; it's pretty much pseudo code, so wont compile, obviously.</p> <h3>Update:</h3> <p>Just added a 3rd solution thanks to nusi.</p> <h3>The wrong way (with stack memory):</h3> <pre><code>std::map&lt;int, MyType1&gt; myMap; void firstFunctionRunFromThread1() { MyType1 mt1; mt1.Test = "Test 1"; myMap[0] = mt1; } void onlyFunctionRunFromThread2() { MyType1 mt1 = myMap[0]; // This actually does print "Test 1", so the memory is being accessed. std::cout &lt;&lt; mt1.Test &lt;&lt; endl; /* However, because we're using stack memory here, this value is lost * when we go back to thread #1. */ mt1.Test = "Test 2"; } void secondFunctionFromThread1() { MyType1 mt1 = myMap[0]; // This will actually print out "Test 1", where "Test 2" is expected! std::cout &lt;&lt; mt1.Test &lt;&lt; endl; } </code></pre> <h3>The <em>complicated</em>, correct method (using heap memory):</h3> <p>See also the simple method which uses stack memory.</p> <pre><code>std::map&lt;int, MyType1&gt; myMap; void firstFunctionRunFromThread1() { // Use heap memory so the memory stays allocated. MyType1 *mt1 = new MyType1(); mt1-&gt;Test = "Test 1"; myMap[0] = *mt1; } void onlyFunctionRunFromThread2() { /* Now, get a pointer to the object; we can't use stack memory * here because the values assigned would be lost as soon as * we try and access them from secondFunctionFromThread1() */ MyType1 *mt1 = &amp;myMap[0]; // As expected, this prints "Test 1" std::cout &lt;&lt; mt1-&gt;Test &lt;&lt; endl; /* Now, because we're assigning to memory on the heap, this will * stay assigned until the entire application exits, yay! */ mt1-&gt;Test = "Test 2"; } void secondFunctionFromThread1() { /* Not sure if using heap memory here is neccecary, but we'll do * it anwyay just to play on the safe side... let me know if this * is pointless... */ MyType1 *mt1 = &amp;myMap[0]; // Hurray, this prints "Test 2"!!! :) std::cout &lt;&lt; mt1-&gt;Test &lt;&lt; endl; } </code></pre> <h3>The <em>simple</em>, correct method (using stack memory correctly):</h3> <p>Thanks to nusi for <a href="https://stackoverflow.com/questions/724536/does-memory-stay-allocated-when-a-c-thread-exits/731025#731025">his answer</a>.</p> <pre><code>std::map&lt;int, MyType1&gt; myMap; void firstFunctionRunFromThread1() { MyType1 mt1; mt1.Test = "Test 1"; myMap[0] = mt1; } void onlyFunctionRunFromThread2() { /* Using the &amp; before the variable turns it into a reference, so * instead of using stack memory, we use the original memory. * NOTE: Is this explanation correct? */ MyType1 &amp;mt1 = myMap[0]; // This actually does print "Test 1", so the memory is being accessed. std::cout &lt;&lt; mt1.Test &lt;&lt; endl; // We're assigning to the reference, so this works. mt1.Test = "Test 2"; } void secondFunctionFromThread1() { MyType1 mt1 = myMap[0]; // Prints "Test 1" as expected. std::cout &lt;&lt; mt1.Test &lt;&lt; endl; } </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.
 

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