<div dir="ltr">Thanks Stephen for your detailed explanation. I realize what I was doing with the array example was sloppy, the goal was really to get it working with Vector and push_back() so that I can scale the container as needed. I will try your suggestion later and see if I&#39;m able to get it working.<div>
<br></div><div style>n</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Jun 19, 2013 at 9:45 AM, Stephen Sinclair <span dir="ltr">&lt;<a href="mailto:sinclair@music.mcgill.ca" target="_blank">sinclair@music.mcgill.ca</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Nick,<br>
<div class="im"><br>
On Wed, Jun 19, 2013 at 3:09 PM, Nick Dika &lt;<a href="mailto:nhdika@gmail.com">nhdika@gmail.com</a>&gt; wrote:<br>
&gt; Thanks Stephen. Here&#39;s a simplified version of what I&#39;m doing.<br>
&gt;<br>
&gt; This works:<br>
&gt;<br>
&gt; stk::BlitSaw oscArray[10];<br>
&gt;<br>
&gt; for( int i=0; i &lt; 10; i++ )<br>
&gt; {<br>
&gt; stk::BlitSaw saw;<br>
&gt; saw.setHarmonics(0);<br>
&gt; saw.setFrequency(220);<br>
&gt; oscArray[i] = saw;<br>
&gt; }<br>
<br>
</div>Actually in g++-4.7 at least, this code does not compile.  The BlitSaw<br>
class is unassignable.<br>
<br>
(I&#39;m not 100% sure why, but it seems to be because objects of class<br>
Stk contain a std::ostringstream member which is unassignable.)<br>
<div class="im"><br>
<br>
&gt; This breaks:<br>
&gt;<br>
&gt; std::vector&lt;stk::BlitSaw&gt; oscs;<br>
&gt;<br>
&gt; for( int i=0; i &lt; 10; i++ )<br>
&gt; {<br>
&gt; stk::BlitSaw saw;<br>
&gt; saw.setHarmonics(0);<br>
&gt; saw.setFrequency(220);<br>
&gt; oscs.push_back(saw);<br>
&gt; }<br>
<br>
</div>This is effectively equivalent to the above code so I&#39;m not 100% why<br>
it&#39;s breaking things but probably the object is not being properly<br>
copied within the vector.  In particular, since you have a vector of<br>
instances of BlitSaw, when the internal array in the std::vector is<br>
re-allocated, perhaps the new, copied memory is not properly<br>
initialized 100%, since the object type is not assignable.  Just a<br>
guess, but it&#39;s weird behaviour for sure, but due to undefined<br>
behaviour because this isn&#39;t how you should do what you&#39;re trying to<br>
do.<br>
<div class="im"><br>
&gt;<br>
&gt; the second example causes an unhandled exception/access violation reading<br>
&gt; from a location. As mentioned this appears to happen after<br>
&gt; StkFrames::resize() is called (I don&#39;t fully understand why this would be<br>
&gt; called).<br>
<br>
</div>All Generators, which BlitSaw is one, have an StkFrame called<br>
lastFrames_, and it&#39;s resize() method is called during initialization.<br>
 Not sure why it&#39;s leading to an exception but like your array doesn&#39;t<br>
contain valid BlitSaw instances.<br>
<br>
Anyways, to elaborate, the way you&#39;re initializing things isn&#39;t<br>
fundamentally wrong, so it could indicate some bugs somewhere, but<br>
it&#39;s certainly not the ideal way to work with Stk objects.  You are<br>
creating an array of objects, such that their constructors are called,<br>
and then a loop you are constructing a new object on the stack,<br>
setting its parameters, and then copying that object over top of the<br>
objects already in the array and letting it destruct.  Not the most<br>
efficient approach.<br>
<br>
(Note, your push_back() call also doesn&#39;t compile in g++-4.7.)<br>
<br>
Instead I&#39;d suggest either directly initializing the objects that are<br>
already in the array, or initializing them and then storing their<br>
pointers in the array.  i.e., either:<br>
<br>
#include &lt;iostream&gt;<br>
#include &lt;Stk.h&gt;<br>
#include &lt;BlitSaw.h&gt;<br>
<br>
int main()<br>
{<br>
    stk::BlitSaw oscs[10];<br>
<div class="im"><br>
    for( int i=0; i &lt; 10; i++ )<br>
    {<br>
</div>        stk::BlitSaw *saw = &amp;oscs[i];<br>
        saw-&gt;setHarmonics(0);<br>
        saw-&gt;setFrequency(220);<br>
    }<br>
<br>
    return 0;<br>
}<br>
<br>
<br>
.. or ..<br>
<br>
<br>
#include &lt;iostream&gt;<br>
#include &lt;memory&gt;<br>
#include &lt;Stk.h&gt;<br>
#include &lt;BlitSaw.h&gt;<br>
<br>
int main()<br>
{<br>
    std::vector&lt;std::unique_ptr&lt;stk::BlitSaw&gt;&gt; oscs;<br>
<div class="im"><br>
    for( int i=0; i &lt; 10; i++ )<br>
    {<br>
</div>        std::unique_ptr&lt;stk::BlitSaw&gt; saw(new stk::BlitSaw);<br>
        saw-&gt;setHarmonics(0);<br>
        saw-&gt;setFrequency(220);<br>
        oscs.push_back(std::move(saw));<br>
    }<br>
<br>
    return 0;<br>
<div class="HOEnZb"><div class="h5">}<br>
<br>
<br>
Steve<br>
<br>
_______________________________________________<br>
Stk mailing list<br>
<a href="mailto:Stk@ccrma.stanford.edu">Stk@ccrma.stanford.edu</a><br>
<a href="http://ccrma-mail.stanford.edu/mailman/listinfo/stk" target="_blank">http://ccrma-mail.stanford.edu/mailman/listinfo/stk</a><br>
</div></div></blockquote></div><br></div>