# Strange bug on Array::add() after migrating to 1.52

**URL:** <https://forum.juce.com/t/strange-bug-on-array-add-after-migrating-to-1-52/6336>\
**Category:** General JUCE discussion\
**Created:** [February 1, 2011, 10:27am UTC](https://forum.juce.com/t/strange-bug-on-array-add-after-migrating-to-1-52/6336 "2011-02-01T10:27:33Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![swar](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/swar/32/12935_2.png) [@swar](https://forum.juce.com/u/swar)\
**Post date:** [February 1, 2011, 10:27am UTC](https://forum.juce.com/t/strange-bug-on-array-add-after-migrating-to-1-52/6336/1 "2011-02-01T10:27:33Z")

</div>

Hi,

I’m having a strange bug after migrating to version 1.52. I have tracked it down to juce\_VST\_Wrapper.cpp, at the line:

```
    activePlugins.add (this);

```

in the constructor. Surprisingly, the activePlugins array does not store the JuceVSTWrapper instance passed. Instead I think it creates an uninitialized entry. As a result, an assertion fails later on in the process.

What could possibly be wrong?

For information, I’m including here the add method from the 1.52 framework:

```auto
void add (ParameterType newElement)
	{
		const ScopedLockType lock (getLock());
		data.ensureAllocatedSize (numUsed + 1);
		new (data.elements + numUsed++) ElementType (newElement);
	}
```

Thanks for any help!

Mariano

---

<div class="post-metadata">

**Author:** ![X-Ryl669](https://avatars.discourse-cdn.com/v4/letter/x/a5b964/32.png) [@X-Ryl669](https://forum.juce.com/u/X-Ryl669)\
**Post date:** [February 1, 2011, 10:48am UTC](https://forum.juce.com/t/strange-bug-on-array-add-after-migrating-to-1-52/6336/2 "2011-02-01T10:48:44Z")

</div>

This is making a copy of the object passed in. If you don’t have a copy constructor, then you better use OwnedArray instead.

---

<div class="post-metadata">

**Author:** ![swar](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/swar/32/12935_2.png) [@swar](https://forum.juce.com/u/swar)\
**Post date:** [February 1, 2011, 11:07am UTC](https://forum.juce.com/t/strange-bug-on-array-add-after-migrating-to-1-52/6336/3 "2011-02-01T11:07:09Z")

</div>

Thanks, but that’s Juce framework code. I don’t think anything with that is wrong; it would have shown elsewhere, otherwise. But then there could be a wrong setting that’s preventing a proper copy to be created.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 1, 2011, 11:38am UTC](https://forum.juce.com/t/strange-bug-on-array-add-after-migrating-to-1-52/6336/4 "2011-02-01T11:38:11Z")

</div>

Nope, that code looks perfectly ok to me. It’s just an Array \<void\*\>, so it’s just storing a pointer, nothing fancy. I think you’re probably misinterpreting some other kind of problem, e.g. a plugin being deleted twice for some reason.

---

<div class="post-metadata">

**Author:** ![swar](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/swar/32/12935_2.png) [@swar](https://forum.juce.com/u/swar)\
**Post date:** [February 1, 2011, 12:02pm UTC](https://forum.juce.com/t/strange-bug-on-array-add-after-migrating-to-1-52/6336/5 "2011-02-01T12:02:37Z")

</div>

What I don’t understand is that I go step by step in the add method, and I can see that the data member is allocated one entry after data.ensureAllocatedSize, but then that pointer is never replaced. Is that the correct behaviour?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 1, 2011, 2:12pm UTC](https://forum.juce.com/t/strange-bug-on-array-add-after-migrating-to-1-52/6336/6 "2011-02-01T14:12:52Z")

</div>

I don’t know exactly what you’re seeing, but the C++ is definitely correct - it’d be a compiler error if it failed. But I use Array\<void\*\> in lots of places, and if it didn’t work, it’d break a whole load of stuff.

---

<div class="post-metadata">

**Author:** ![swar](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/swar/32/12935_2.png) [@swar](https://forum.juce.com/u/swar)\
**Post date:** [February 1, 2011, 3:32pm UTC](https://forum.juce.com/t/strange-bug-on-array-add-after-migrating-to-1-52/6336/7 "2011-02-01T15:32:07Z")

</div>

I’m really lost here. I have just temporarily (will of course remove it afterwards) updated the Juce\_VST\_Wrapper.cpp by adding jassert (activePlugins.contains (this)) after the add:

> [@](#):
>
> ```
> isSynth ((JucePlugin_IsSynth) != 0);
> noTail (((JucePlugin_SilenceInProducesSilenceOut) != 0) && (JucePlugin_TailLengthSeconds <= 0));
> setInitialDelay (filter->getLatencySamples());
> programsAreChunks (true);
> 
> activePlugins.add (this);
> jassert (activePlugins.contains (this));
> 
> ```

And the assertion fails… I don’t understand how that can happen, specially in the same member.

I know it must be a configuration problem somewhere, but it would really help me if you could give me a direction, because I’m going crazy.

---

<div class="post-metadata">

**Author:** ![X-Ryl669](https://avatars.discourse-cdn.com/v4/letter/x/a5b964/32.png) [@X-Ryl669](https://forum.juce.com/u/X-Ryl669)\
**Post date:** [February 1, 2011, 3:42pm UTC](https://forum.juce.com/t/strange-bug-on-array-add-after-migrating-to-1-52/6336/8 "2011-02-01T15:42:17Z")

</div>

Make sure you don’t mix amalgamated files with some other source files (rebuild everything, clean all project, try not to use amalgamated headers / cpp files).

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 1, 2011, 4:15pm UTC](https://forum.juce.com/t/strange-bug-on-array-add-after-migrating-to-1-52/6336/9 "2011-02-01T16:15:13Z")

</div>

The main problem you might hit when you’re using the VST wrapper code is that the VST files also include the juce header files, so you _must_ make sure that they are picking up the exact same version of juce headers that the rest of your app is using, or there’ll be all kinds of nasty linkage trouble.

---

<div class="post-metadata">

**Author:** ![swar](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/swar/32/12935_2.png) [@swar](https://forum.juce.com/u/swar)\
**Post date:** [February 2, 2011, 7:40am UTC](https://forum.juce.com/t/strange-bug-on-array-add-after-migrating-to-1-52/6336/10 "2011-02-02T07:40:13Z")

</div>

I have really cleansed up any references to other Juce files in my code, but no luck. Currently I can see that what fails is in:

> [@](#):
>
> ```
> bool contains (ParameterType elementToLookFor) const
> {
> const ScopedLockType lock (getLock());
> const ElementType* e = data.elements.getData();
> const ElementType* const end = e + numUsed;
> 
> while (e != end)
> {
> if (elementToLookFor == *e)
> return true;
> 
> ++e;
> }
> 
> ```

The _end_ pointer gets set to the same value as _e_. It’s not incremented. As a result, we are never entering the loop. How can this happen? I’m not that familiar with pointer operations. Am I linking with a wrong library?

---

<div class="post-metadata">

**Author:** ![ckk](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@ckk](https://forum.juce.com/u/ckk)\
**Post date:** [February 2, 2011, 8:26am UTC](https://forum.juce.com/t/strange-bug-on-array-add-after-migrating-to-1-52/6336/11 "2011-02-02T08:26:31Z")

</div>

The pointers e and end are equal if numUsed is 0, so the problem isn’t in contains(). As said above, I don’t think the problem lies in the Array’s code. You call contains() directly after add() where numUsed is increased, so if the code would be executed as it should numUsed wouldn’t be 0 (unless it’s -1 before the add() which would hang apps calling contains() before they add anything)

Chris

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [February 2, 2011, 8:34am UTC](https://forum.juce.com/t/strange-bug-on-array-add-after-migrating-to-1-52/6336/12 "2011-02-02T08:34:39Z")

</div>

When there are zero elements in the array, this is the right behaviour.  
did you check extras\audio plugins\wrapper\juce\_PluginHeaders.h for cross reference to juce\_amalgamated.h?

---

<div class="post-metadata">

**Author:** ![swar](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/swar/32/12935_2.png) [@swar](https://forum.juce.com/u/swar)\
**Post date:** [February 2, 2011, 8:53am UTC](https://forum.juce.com/t/strange-bug-on-array-add-after-migrating-to-1-52/6336/13 "2011-02-02T08:53:30Z")

</div>

Sorry, forgot to mention that I can see numUsed has the value of 1 in the debugger. That’s why it feels so wrong.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 2, 2011, 10:09am UTC](https://forum.juce.com/t/strange-bug-on-array-add-after-migrating-to-1-52/6336/14 "2011-02-02T10:09:48Z")

</div>

Seriously: there’s no bug in those Array methods. No question about that.

You need to think more laterally… E.g. is your destructor getting a re-entrant callback from something that happens during the shutdownJuce\_GUI() function (i.e. after the plugin has been removed from the list but before its destructor has completed) ?

---

<div class="post-metadata">

**Author:** ![swar](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/swar/32/12935_2.png) [@swar](https://forum.juce.com/u/swar)\
**Post date:** [February 2, 2011, 5:48pm UTC](https://forum.juce.com/t/strange-bug-on-array-add-after-migrating-to-1-52/6336/15 "2011-02-02T17:48:27Z")

</div>

I have never doubted the Array class and its methods. Sorry if the title of my thread is misleading. I’m just trying to understand at which exact place the framework “derails” so as to have a hint of what could be the reason the assert is triggered. This pointer incrementation is puzzling, so I thought it could result from something like:

a) debugger info not reliable  
b) memory not allocated and as such the pointer is not incremented  
c) pointer size being different (though that would be something;-)

Anyway, I understand that it may be pointless to concentrate on that. I’ll probably recreate a new brand plugin through The Jucer and copy my existing code progressively into that. But I would have loved to find out what is wrong with my current project. It was working perfectly under Juce 1.51.

And to answer your question, I never reach any shutdownJuce\_GUI method, as the assert fails before that.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 2, 2011, 5:54pm UTC](https://forum.juce.com/t/strange-bug-on-array-add-after-migrating-to-1-52/6336/16 "2011-02-02T17:54:21Z")

</div>

Well, I’ve love to know what’s going on if you track it down, but yes, you’re definitely wasting your time looking for bugs in the array. Sounds to me like something’s got corrupted and the debugger is a bit confused.

---

<div class="post-metadata">

**Author:** ![swar](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/swar/32/12935_2.png) [@swar](https://forum.juce.com/u/swar)\
**Post date:** [February 4, 2011, 9:29am UTC](https://forum.juce.com/t/strange-bug-on-array-add-after-migrating-to-1-52/6336/17 "2011-02-04T09:29:30Z")

</div>

Finally, after painful debugging, I managed to track down the problem to a combination of keeping a non-static pointer to AudioPluginFormatManager in my singleton manager class and having JUCE\_CHECK\_MEMORY\_LEAKS (temporarily) disabled. After defining my pointer as static and reenabling the leaks check flag, it now seems to work fine (still touching wood;-).

I just need to solve a CallbackMessage leak problem now, but that should be a walk in the park compared to the problem faced before.

Thanks a lot for your help!

Mariano

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 4, 2011, 9:32am UTC](https://forum.juce.com/t/strange-bug-on-array-add-after-migrating-to-1-52/6336/18 "2011-02-04T09:32:45Z")

</div>

CallbackMessages can sometimes leak when they’re still queued for delivery in the system message queue when the plugin has been unloaded. Nothing we can do about that, really, so if it’s only one or two objects, it’s probably not worth worrying about.
