StringArray::add segfault


I’m using a StringArray as a member of a ListBoxModel:

class ArrayListBoxModel : public ListBoxModel
    StringArray arr;
    ListBox *lb;
    ArrayListBoxModel(ListBox *lb = nullptr);
    virtual int getNumRows();
    virtual void paintListBoxItem(int rowNum, Graphics &g, int w, int h, bool selected);
    void addRow(const String s, bool update=true); // update only used when adding multiple rows
    void deleteRow(int rowNum, bool update=true);
    void clear();
    void setParent(ListBox *lb);

When calling arr.add(String("Test Item")), there is a segfault at

    /** Appends a new element at the end of the array.

        @param newElement       the new object to add to the array
        @see set, insert, addIfNotAlreadyThere, addSorted, addUsingDefaultSort, addArray
    void add (ParameterType newElement)
        const ScopedLockType lock (getLock());
        data.ensureAllocatedSize (numUsed + 1); // Thread 1 Juce Message Thread: EXC_BAD_ACCESS (code=2, address=0xc)
        new (data.elements + numUsed++) ElementType (newElement);

I’m on Mac; I got JUCE from git a few days ago. (v2.1.1)


looks like its when it’s expanding the array before adding the string. So the string itself doesnt matter here.

im thinking the overall ArrayListBoxModel' is not right. Check how you're instantiating this class. If it's on the stack, try changing it to the heap withwhatever = new ArrayListBoxBoxModel’. If that fixes it, then investigate what was wrong with the stack version.


Moving the StringArray to the stack caused a segfault at

/** Lets you access methods and properties of the object that this ScopedPointer refers to. */
    inline ObjectType* operator->() const noexcept                                  { return object; } // Thread 1 Juce Message Thread: EXC_BAD_ACCESS (code=2, address=0x4)

which makes no sense.


Looks like I forgot to set the ListBox’s model to my ArrayListBoxModel. That fixed it.