<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'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"><<a href="mailto:sinclair@music.mcgill.ca" target="_blank">sinclair@music.mcgill.ca</a>></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 <<a href="mailto:nhdika@gmail.com">nhdika@gmail.com</a>> wrote:<br>
> Thanks Stephen. Here's a simplified version of what I'm doing.<br>
><br>
> This works:<br>
><br>
> stk::BlitSaw oscArray[10];<br>
><br>
> for( int i=0; i < 10; i++ )<br>
> {<br>
> stk::BlitSaw saw;<br>
> saw.setHarmonics(0);<br>
> saw.setFrequency(220);<br>
> oscArray[i] = saw;<br>
> }<br>
<br>
</div>Actually in g++-4.7 at least, this code does not compile. The BlitSaw<br>
class is unassignable.<br>
<br>
(I'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>
> This breaks:<br>
><br>
> std::vector<stk::BlitSaw> oscs;<br>
><br>
> for( int i=0; i < 10; i++ )<br>
> {<br>
> stk::BlitSaw saw;<br>
> saw.setHarmonics(0);<br>
> saw.setFrequency(220);<br>
> oscs.push_back(saw);<br>
> }<br>
<br>
</div>This is effectively equivalent to the above code so I'm not 100% why<br>
it'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's weird behaviour for sure, but due to undefined<br>
behaviour because this isn't how you should do what you're trying to<br>
do.<br>
<div class="im"><br>
><br>
> the second example causes an unhandled exception/access violation reading<br>
> from a location. As mentioned this appears to happen after<br>
> StkFrames::resize() is called (I don't fully understand why this would be<br>
> called).<br>
<br>
</div>All Generators, which BlitSaw is one, have an StkFrame called<br>
lastFrames_, and it's resize() method is called during initialization.<br>
Not sure why it's leading to an exception but like your array doesn't<br>
contain valid BlitSaw instances.<br>
<br>
Anyways, to elaborate, the way you're initializing things isn't<br>
fundamentally wrong, so it could indicate some bugs somewhere, but<br>
it'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't compile in g++-4.7.)<br>
<br>
Instead I'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 <iostream><br>
#include <Stk.h><br>
#include <BlitSaw.h><br>
<br>
int main()<br>
{<br>
stk::BlitSaw oscs[10];<br>
<div class="im"><br>
for( int i=0; i < 10; i++ )<br>
{<br>
</div> stk::BlitSaw *saw = &oscs[i];<br>
saw->setHarmonics(0);<br>
saw->setFrequency(220);<br>
}<br>
<br>
return 0;<br>
}<br>
<br>
<br>
.. or ..<br>
<br>
<br>
#include <iostream><br>
#include <memory><br>
#include <Stk.h><br>
#include <BlitSaw.h><br>
<br>
int main()<br>
{<br>
std::vector<std::unique_ptr<stk::BlitSaw>> oscs;<br>
<div class="im"><br>
for( int i=0; i < 10; i++ )<br>
{<br>
</div> std::unique_ptr<stk::BlitSaw> saw(new stk::BlitSaw);<br>
saw->setHarmonics(0);<br>
saw->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>