Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Aranince

macrumors 65816
Original poster
Apr 18, 2007
1,104
0
California
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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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.
 

Aranince

macrumors 65816
Original poster
Apr 18, 2007
1,104
0
California
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.