Please Help: EXC_BAD_ACCESS error

Discussion in 'Mac Programming' started by stevegreen22, Nov 19, 2012.

  1. macrumors newbie

    Joined:
    Aug 7, 2012
    #1
    Hi all,

    I'm new to C++ and have come across this error when running part of the program.

    I'm overloading the >> operator, reading in a file and creating a list of album objects and adding relevant tracks to the albums.

    The task itself works fine, in that the albums are created with tracks and so on but to get it to work I needed ot use a pointer which seems to tbe the cause of this issue but I don't know enough to solve. I've spent a while unting around online adn have tried using auto ptr and unique ptr but both give me other errors which, yet again I don't know how to navigate.

    Could some shed some light please?

    many thanks!

    code:
    --excuse comments.

    Code:
            
    inline istream& operator>>(istream& is, AlbumCollection& albums){
        
        std::string artistName, albumName, trackName, duration;
        std::string firstline;
        
        //std::auto_ptr<Album> Album_memory_manager(new Album);
        
        //Album tempAlbum;
        Album tempAlbumWithTracks;
        vector<Album>albumVector;
        vector<Track>trackVector;
        //Duration duration;
        
        //*Album_memory_manager = TESTTTTT;
        
        Album* TESTTTTT;
        
        std::auto_ptr<Album> Album_memory_manager(new Album);
        
        //make stream from first line
        stringstream stringstream1(firstline);
        
        //loop through while there is still a line
        while (getline(is, firstline)){
    
            //IF THIS LINE IS NOT A TRACK IT MUST BE AN ALBUM
            if (!(firstline[8] == '-')){
                
                stringstream stringstreamTeast(firstline);
                
                //NEED TO MAKE A NEW ALBUM AND EXTRACT TITLE & ARTIST
                if(getline(stringstreamTeast, artistName, ':')){
                    artistName = artistName;
                    //cout << "\nArtist:: " << artistName << endl;
                    //YES!!!!
                    if(getline(stringstreamTeast, albumName, '\n')){
                        albumName = albumName;
                        cout << endl;//"Album:: " << albumName << endl;
                    }
                    
                    if (TESTTTTT != 0){
                        
                        albumVector.push_back(*TESTTTTT);
                        
                    }
                    TESTTTTT = new Album(artistName, albumName); 
                }
            }
            
            else if (firstline[8] == '-') {          //Not a new album  //could use [3] etc
                //stream for track
                stringstream meh(firstline);
                
                if(getline(meh, albumName, '-')){     //ERROR HERE
      
                    duration = firstline.substr(0,7);
                    trackName = firstline.substr(10, firstline.size());
                    Duration d;
                    stringstream trackConversion(duration);
                    trackConversion >> d;
                    
                    //create new track
                    Track testTrack(trackName, d);
                   // cout << "TEST OUTPUT OF TRACKS " << testTrack<<endl;
                
                    TESTTTTT->addTrack(testTrack);
                    
                    trackVector.push_back(testTrack);
                    
                }
                
            }//end else if
        
        }
        //add final album to vector
            
        albumVector.push_back(*TESTTTTT);
        //delete TESTTTTT;
        //FOR ****S SAKE!!!
        //delete TESTTTTT;
        
        //finally create the collection
        // albums = AlbumCollection(albumVector);
        
        //proves tracks are being added ok!
    //    for (vector<Track>::iterator iter = trackVector.begin(); iter !=trackVector.end(); iter++){
    //        
    //        cout <<"Iteator test for tracks"<< *iter << endl;
    //    }
        
        //proves albums are being added ok
        for (vector<Album>::iterator iter = albumVector.begin(); iter !=albumVector.end(); iter++){
            
            cout <<"Iteator test for albums\n"<< *iter << endl;
        }
            return is;
        
        
    }//end function
    
     
  2. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    In Xcode, set a breakpoint on the first line of main (), then step through the code line by line, checking that every variable has the value that you think it should have, unless you get to the point where it crashes.

    But more important, go into your project settings, turn all the warnings on that you can find, for example warnings for undefined variables, and see what the compiler tells you.
     
  3. thread starter macrumors newbie

    Joined:
    Aug 7, 2012
    #3
    -.-

    pffft,

    I was 100% certain the error was within the code I posted (likely still may be a couple in there) so on all previous debugs I had the break points at various points in there.

    Turns out....

    Code:
    int main(int argc, const char * argv[])
    {
        
    
        //file input
            ifstream file ("/Users/stevengreen22/Desktop/albums.txt");
        
      
            //this will be a collection of albums
            if (file.is_open()){
                cout<< "File Has Been Opened OK"<<endl;
                AlbumCollection ac;
                file >> ac;
    
                cout << "Test....." <<endl;
                
                //THIS CREATES AN ERROR!!!
                
    //            cout << "Album at postion:   " <<ac.getAlbumAtPosition(6);
    //            
    //            Album meh = ac.getAlbumAtPosition(6);
    //            
    //            cout << "Test Album contians:: " << meh;
    //            
    //            Track mehh = meh.getTrackByIndex(1);
    //            
    //            cout << "Track 2" << mehh <<endl;;
                
                
                
                
            } else{
                cout << "File says NO!!!!" <<endl;
            }
    
    the commented 'test' code there was the cause.

    Thanks for advice.

    Not impressed I wasted a day or so just for it to be that simple. -.-
     
  4. thread starter macrumors newbie

    Joined:
    Aug 7, 2012
    #4
    has also just opened a can of worms....
     
  5. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #5
    If you turn warnings on, there is one quite obvious bug in your first post that should get a big fat warning from the compiler.

    The good news is, you'll get better at this.
     

Share This Page