Note that there are some explanatory texts on larger screens.

plurals
  1. POstd::map compare function and NULL
    primarykey
    data
    text
    <p>I've written a compare function for a std::map so I can have custom key types.</p> <pre><code>std::map&lt;GGString *, GGObject *, GGDictionaryMapCompare&gt; _map; </code></pre> <p>...</p> <pre><code>class GGDictionaryMapCompare { public: bool operator()(GGString * lhs, GGString * rhs) { return strcmp(lhs-&gt;str(), rhs-&gt;str()) &lt; 0; } }; </code></pre> <p>Code that adds elements:</p> <pre><code>GGObject *GGDictionary::addKeyObject(GGString *theKey, GGObject *theObject) { if (theKey == NULL || theObject == NULL) return NULL; _map.insert(std::pair&lt;GGString *, GGObject *&gt;(theKey, theObject)); return theObject; } </code></pre> <p>Code that is causing the crash:</p> <pre><code>GGObject *GGDictionary::objectForKey(GGString *theKey) { if (theKey == NULL) return NULL; std::map&lt;GGString *, GGObject *, GGDictionaryMapCompare&gt;::iterator ii = _map.find(theKey); if (ii == _map.end()) return NULL; return GGAutoRelease(ii-&gt;second); } </code></pre> <p>Stack trace:</p> <pre><code>#0 0x00009f15 in GGString::str() #1 0x0004a4c4 in GGDictionaryMapCompare::operator()(GGString*, GGString*) #2 0x0004a3d3 in std::_Rb_tree&lt;GGString*, std::pair&lt;GGString* const, GGObject*&gt;, std::_Select1st&lt;std::pair&lt;GGString* const, GGObject*&gt; &gt;, GGDictionaryMapCompare, std::allocator&lt;std::pair&lt;GGString* const, GGObject*&gt; &gt; &gt;::find(GGString* const&amp;) #3 0x00049b04 in std::map&lt;GGString*, GGObject*, GGDictionaryMapCompare, std::allocator&lt;std::pair&lt;GGString* const, GGObject*&gt; &gt; &gt;::find(GGString* const&amp;) #4 0x00048ec9 in GGDictionary::objectForKey(GGString*) </code></pre> <p>The issue is that the lhs is coming in NULL. I never insert a NULL into the map, so this should not be happening. Any idea why? Or am I just doing the compare function wrong? I can protect against getting NULL, but it seems like something is wrong and I don't want to cure a symptom and not the problem.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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