Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 19, 2012, 04:38 PM   #1
stevegreen22
macrumors newbie
 
Join Date: Aug 2012
Please Help: EXC_BAD_ACCESS error

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
stevegreen22 is offline   0 Reply With Quote
Old Nov 19, 2012, 05:18 PM   #2
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
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.
gnasher729 is offline   0 Reply With Quote
Old Nov 19, 2012, 05:39 PM   #3
stevegreen22
Thread Starter
macrumors newbie
 
Join Date: Aug 2012
-.-

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. -.-
stevegreen22 is offline   0 Reply With Quote
Old Nov 19, 2012, 05:59 PM   #4
stevegreen22
Thread Starter
macrumors newbie
 
Join Date: Aug 2012
has also just opened a can of worms....
stevegreen22 is offline   0 Reply With Quote
Old Nov 19, 2012, 07:16 PM   #5
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by stevegreen22 View Post
has also just opened a can of worms....
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.
gnasher729 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Resolved: EXC_BAD_ACCESS problem larswik iPhone/iPad Programming 0 Jan 27, 2014 09:33 PM
Best easy way to debug EXC_BAD_ACCESS code 1 moonman239 iPhone/iPad Programming 25 Nov 7, 2013 12:18 AM
EXC_BAD_ACCESS - Trying to retrieve string from NSArray moonman239 iPhone/iPad Programming 12 Sep 9, 2013 08:06 AM
Objective-C noob: EXC_BAD_ACCESS with simple print method? dancks Mac Programming 14 Apr 27, 2013 05:47 PM
Getting crazy also with EXC_BAD_ACCESS jivaro Mac Programming 5 Jun 1, 2012 09:49 PM

Forum Jump

All times are GMT -5. The time now is 10:39 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC