Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Parameter's Value Changes Between Stack Frames in std::vector
    text
    copied!<p>I've run into a really strange bug, that I'm hoping someone can explain. I have a simple <code>std::vector&lt;V3x&gt;</code>, where <code>V3x</code> is a 3d vector (the linear algebra kind.) The following code causes a <code>std::length_error</code> exception to be thrown:</p> <pre><code>std::vector&lt;V3x&gt; vertices; int vertexCount = computeVertexCount(); vertices.resize(vertexCount); // throws std::length_error </code></pre> <p>I've verified that <code>computeVertexCount()</code> returns a value of <code>35</code>, which is far far below <code>vector::max_size()</code> so there's no way it's asking for too much memory.</p> <p>I traced the exception down into the definition of <code>std::vector</code>, to the following two functions.</p> <pre><code>void resize(size_type _Newsize, _Ty _Val) { // determine new length, padding with _Val elements as needed if (size() &lt; _Newsize) // NOTE: here, _Newsize - size() = 35 _Insert_n(end(), _Newsize - size(), _Val); else if (_Newsize &lt; size()) erase(begin() + _Newsize, end()); } void _Insert_n(const_iterator _Where, size_type _Count, const _Ty&amp; _Val) { // insert _Count * _Val at _Where // NOTE: here, _Count = 3435973836 ... } </code></pre> <p>So when the <code>_Count</code> parameter is passed between <code>resize()</code> and <code>_Insert_n()</code>, the value changes from 35 to 3435973836. I'm assuming the memory has somehow become corrupted, but I have no idea how that could be.</p> <p>For a little more context in case it's part of the problem, this code sits in a .dll plugin that I'm loading from Softimage XSI.</p> <p>Does anyone know what might cause something like this to happen?</p> <p><strong>EDIT: SOLUTION</strong></p> <p>nobugz, I could kiss you.</p> <p>The size of std::vector was changing inside my .dll, because of <code>_HAS_ITERATOR_DEBUGGING</code> in VS2008. The search led me to <a href="http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/efef799e-5d88-4a95-be39-4cd674be5d14/" rel="nofollow noreferrer">someone with the same problem</a>, and it was fixed by adding the following at the top of my project:</p> <pre><code>// fix stack corruption errors caused by VS2008 #define _HAS_ITERATOR_DEBUGGING 0 #define _SECURE_SCL 0 </code></pre>
 

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