I am creating a scripting engine that would execute my own scripting language. The engine does a double pass through the script, the first pass parses the file and builds the data structure and the second executes the script. The error I am getting is when I try to read in a entire line from the file using getline(std::ifstream, std::string). The error happens at run time which returns this

S_Language(5035) malloc: *** error for object 0x1000212a0: pointer being freed was not allocated

What would be causing this error. I don't get how it is saying that a pointer is being freed that was not allocated. I declared my string that i wanted the line from the file to be written too.

Here is my code for parsing in the file. By the way this is not a homework assignment this is practice for a competition I am trying to enter.

void Parser::Parse() {
    std::ifstream fin;
    fin.open(this->fileName.c_str(), std::ifstream::in);
    
    if(!fin) {
        // Error
        this->errorString = "There was an error unable to read file!";
        this->error = true;
        return;
    }
    
    std::string programLine;
    std::string label = "";
    int lineCounter = 0;
    
    bool firstLineLabel = false;
    
    Operation *OP;
    std::list<Operation *> *OPlst;
    
    OPlst = new std::list<Operation *>;

    while(!fin.eof()) {
        // Get the current line of the document
        getline(fin, programLine);  // error occurrs here

        if(!firstLineLabel && this->hasLabel(programLine)) {
            this->startLabel = label = this->getLabel(this->positionOfChar('[',programLine)+1,programLine);
            firstLineLabel = true;
        }
        
        if(firstLineLabel) {
            if(this->hasSemiColon(programLine)) {
                this->error = true;
                
                std::stringstream sstr;
                sstr << "missing semicolon on line #";
                sstr << lineCounter;
                
                this->errorString = sstr.str();
                break;
            } else {
                if(this->hasLabel(programLine)) {
                    // push the label and OPlst onto the map
                    labelOPMap[label] = *OPlst;
                    
                    // Create a new Operation List
                    OPlst = new std::list<Operation *>;
                    
                    // get the new label
                    label = this->getLabel(this->positionOfChar('[',programLine),programLine);
                    
                    // Clip the label off the string
                    programLine = programLine.substr(this->positionOfChar(']',programLine)+1);
                }
                
                // Parse the current line of the document
                OP = this->ProcessLine(programLine);
                
                // push the Operation onto the list
                OPlst->push_back(OP);
                
                // Push the text input the list
                this->ProgramLines.push_back(programLine);
                ++lineCounter;
            }
        }
    }
}

Recommended Answers

All 7 Replies

>>std::list<Operation *> *OPlst;
>>OPlst = new std::list<Operation *>;

Why in the world is this a pointer??? It should be just this: std::list<Operation*> OPlst;

Does the error occur on the first line attempted read, on some line in the data file, the last line, or after the last line?

right now I have seen the error occurring on the very first line

There's nothing wrong with the getline itself. Can you step through in debugging, line by line, and really find the point at which the error occurs?

As AD pointed out, your use of pointers is a bit odd, and allocating another list of pointers inside the loop is another problem area.

Vmanes, I have used a debugger and when I step into getline for the first time it throws the error right away

About the list of pointers. I thought I had to do that since I am pushing a list into my map and then I declare a brand new list inside my loop.

You're not checking if your file is indeed open en ready for reading, so change this:

if(!fin) {

to:

if(!fin.is_open() || !fin.good()) {

Then change this:

while(!fin.eof()) {
    // Get the current line of the document
    getline(fin, programLine); // error occurrs here

with this:

while(getline(fin, programLine)) {
    // Get the current line of the document

Getline will return 0 when reaching the end of the file. So no need to use the horrible eof() function.

Niek_e, I made all the changes that you suggested and my program past the file check just fine but still got the same error right when it entered the getline function. I don't think this really matters but my platform and environment I am developing on is Mac 10.6 using xcode. I am not using any platform specific code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.