Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To elaborate on @TI's comment...</p> <p>QString is an <a href="http://doc.qt.digia.com/4.7-snapshot/implicit-sharing.html" rel="nofollow">implicitly shared type</a>. Each new copy made of a QString object increases the reference count under the hood, and when the count goes to zero it is destroyed.</p> <p>What probably happened here is that there was an initialization routine which made a QString instance, passed it in as the key, and the map made a copy. (This does not copy the data, merely increases the shared count.) Then the initialization routine destroyed its instance, so the only shared instance left is the one stored in the map with a share count of 1.</p> <p>Later you probably used something like the <a href="http://doc.qt.digia.com/4.7-snapshot/qmap-iterator.html#key" rel="nofollow"><code>QMap::iterator::key()</code></a> to get a const reference to the string key in the map, passed in as <code>userId</code>. That wouldn't create any new instance of a QString to add to the shared count, but rather points to the one the map owns. So when the map lets go of it...it's destroyed and now <code>userId</code> is a dangling reference.</p> <p><em>(Note: You don't say what's in <code>SomeStruct</code>. But if through it an instance of a matching string to the key could be reached which would be destroyed when the map value's <code>SomeStruct</code> is destroyed, then passing in a reference to such a string as <code>userId</code> could cause an analogous problem.)</em></p> <p>One thing implicit sharing throws into the mix is that sometimes it hides bugs of this nature--which would be made more obvious without implicit sharing. Yet it makes the solution "inexpensive": when you extract the key to pass in, copy it into a local variable instance...and pass a const reference to <em>that</em> variable to this routine. That won't actually copy the data, but it will make <code>userId</code> safe because there will be one more shared count keeping it alive.</p> <p>This helps implement the more generally good protocol: passing a reference type to a routine <em>should</em> mean that you can guarantee the lifetime of the referenced object for the entire runtime of the function you are calling. If that's ever in doubt, make a copy and pass a reference to the copy instead.</p> <p><em>(Note: In the future, try using the <a href="http://sscce.org/" rel="nofollow">Simple, Self Contained, Correct Example</a> format with both the add and remove included, it can lead to finding smoking guns on your own more easily. And without it, we can only make educated guesses on the problem...it could be caused by something else in your program entirely!)</em></p>
    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