Loading file cause crash

Discussion in 'Mac Programming' started by Aranince, Sep 29, 2008.

  1. Aranince macrumors 65816

    Joined:
    Apr 18, 2007
    Location:
    California
    #1
    I'm trying to read lines of a text file, and its crashing during the third iteration. There are 3 lines in the text file.

    Code:
            while(!file.eof())
            {
                std::getline(file, line);
                result[id] = new Task(line); // Crashing here on the third iteration
                id++;            
            }
    The debugger says that there is a segmentation fault, which I assume means that I'm trying to access something out-of-range. The problem is, result is an std::map...so it shouldn't have a set range.
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    I don't know that what you've given is enough for us to help.

    So result is:
    Code:
    std::map<int,Task>
    ?

    Or is id some other sort of object?

    What is the value of line at the crash?

    What does the constructor:
    Code:
    Task(string&)
    do?

    Can you give us a backtrace from the point of the segfault? (bt is the command in gdb)

    I ran this using a map of int to string and all went as planned, so I'd be willing to blame Task, or maybe id if it's some goofy type.

    -Lee

    Edit: Thought of another way this could go awry. Is result a map<type,type> or map<type,type> *? If the latter, then [] is being applied as an array subscript operator instead of the operator[] method on map, which would definitely make things go very wrong.
     
  3. Aranince thread starter macrumors 65816

    Joined:
    Apr 18, 2007
    Location:
    California
    #3
    The problem was that 'line' was empty. So before I create a new object, I just need to be sure that the input string isn't empty.
     

Share This Page