1
votes

I need some help! I'm using a multimap to map the indices of musical notes, so I can access them by a string name of the note, as well as the int value. However, the code works perfectly fine on Windows, but when moving it to OSX (the latest version). I get a "Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)" error anytime insert is used.

Runtime Error

I know I'm not using best method to insert the notes and indices into the multimap, but I intend to rewrite it.

I've tried several method to get it working, but no success. Here the code.

Header

class zelmNote
{
private:
    static bool bNoteMapped;
    static multimap<int, string> noteToName;
    static multimap<string, int> nameToNote;

    static void mapNotes();

public:
    zelmNote();
};

.cpp

multimap<int, string> zelmNote::noteToName;
multimap<string, int> zelmNote::nameToNote;

void zelmNote::mapNotes()
{
    bNoteMapped = true;
    int i = 9;
    string noteName = "";
    /*intToNote.insert(pair<int, string>(8, "G#"));
    noteToInt.insert(pair<string, int>("G#", 8));
    intToNote.insert(pair<int, string>(8, "Ab"));
    noteToInt.insert(pair<string, int>("G#", 8));*/
    //Do natural notes
    for (char c = 'A'; c <= 'G'; c++)
    {
        noteName = ofToString(c);
        noteToName.insert(pair<int, string>(i, noteName));
        nameToNote.insert(pair<string, int>(noteName, i));
        if (i == 11)
        {
            i = 0;
        }
        else if (i == 4)
        {
            i++;
        }
        else if ((i != 11) || (i != 4))
        {
            i += 2;
        }
    }


    //Do sharps
    noteToName.insert(pair<int, string>(0, "B#"));
    nameToNote.insert(pair<string, int>("B#", 0));

    noteToName.insert(pair<int, string>(1, "C#"));
    nameToNote.insert(pair<string, int>("C#", 1));

    noteToName.insert(pair<int, string>(3, "D#"));
    nameToNote.insert(pair<string, int>("D#", 3));

    noteToName.insert(pair<int, string>(4, "D##"));
    nameToNote.insert(pair<string, int>("D##", 4));

    noteToName.insert(pair<int, string>(5, "E#"));
    nameToNote.insert(pair<string, int>("E#", 5));

    noteToName.insert(pair<int, string>(6, "F#"));
    nameToNote.insert(pair<string, int>("F#", 6));

    noteToName.insert(pair<int, string>(8, "G#"));
    nameToNote.insert(pair<string, int>("G#", 8));

    noteToName.insert(pair<int, string>(10, "A#"));
    nameToNote.insert(pair<string, int>("A#", 10));


    //Do flats
    noteToName.insert(pair<int, string>(11, "Cb"));
    nameToNote.insert(pair<string, int>("Cb", 11));

    noteToName.insert(pair<int, string>(0, "Dbb"));
    nameToNote.insert(pair<string, int>("Dbb", 0));

    noteToName.insert(pair<int, string>(1, "Db"));
    nameToNote.insert(pair<string, int>("Db", 1));

    noteToName.insert(pair<int, string>(3, "Eb"));
    nameToNote.insert(pair<string, int>("Eb", 3));

    noteToName.insert(pair<int, string>(4, "Fb"));
    nameToNote.insert(pair<string, int>("Fb", 4));

    noteToName.insert(pair<int, string>(6, "Gb"));
    nameToNote.insert(pair<string, int>("Gb", 6));

    noteToName.insert(pair<int, string>(8, "Ab"));
    nameToNote.insert(pair<string, int>("Ab", 8));

    noteToName.insert(pair<int, string>(10, "Bb"));
    nameToNote.insert(pair<string, int>("Bb", 10));
}

zelmNote::zelmNote()
{
    if (!bNoteMapped)
    {
        mapNotes();
    }
}
Non compiling example. Recommend beating it into an minimal reproducible example. - user4581301
Is this the segment of the code that's causing errors? It looks perfectly good to me. - Kostas
The valuable part of your error has been cropped of. Please consider including it as text. - Captain Giraffe
This code is part of a class zelmNote. Since it is part of a class, you must have created instances of this class somewhere. We have no idea if the instance of zelmNote you are accessing is valid. If it isn't valid, then the members of the zelmNote instance are not valid, including the maps. Thus you need to post where, when, and how you're creating these zelmNote instances. - PaulMcKenzie
Thanks for feedback guys, I was going edit the thread with the full code, but last comment made me realise what I was doing wrong - I was calling the private static mapNotes() from the constructor of a new zelmMap object with boolean checking if it been run before. OSX seem to doesn't like the static being called there, when moving mapNotes() to a public function and calling from the parent class's constructor it seems to work! - Zachary Snow