double free detected in tcache 2 when initializing a list, crashes upon entry insertion

Issue

This Content is from Stack Overflow. Question asked by Eduardo Meli

Reading from a file, each line is stored in a list as an individual row. Each row contains two standard values and a variable amount stored in a list. Therefore each node has two values and a list. Initialization through operator>> works, but as soon as I try to run the loadfile function it crashes with the error: free(): double free detected in tcache 2 Aborted (core dumped)

Here is the code

#include <iostream>
#include "Resource.h"
#include "list.h"
#include "node.h"

using std::cout;
using rows = List<Resource>;
using row = Node<Resource>;

void loadFile(string idata, rows &res)
{
    ifstream ifs(idata, ifstream::in);
    while (ifs.good())
    {
        Resource s;
        ifs >> s;
        Node<Resource> *temp = new Node<Resource>(s);
        res.insert(temp);
    }
    ifs.close();
}

int main()
{
    rows resList;
    loadFile("data.txt", resList);
    cout << resList.getHead()->getValue();
}

this is the source for the class that stores the values, that will be wrapped in a Node template class.

#ifndef RESOURCE_H
#define RESOURCE_H

#include <iostream>
#include <fstream>
#include <string>
#include "list.h"
#include "node.h"
#include <stdio.h>
#include <algorithm>

using std::cout;
using std::istream;
using std::string;

class Resource
{
private:
    string Name;
    int amt;
    List<string> clientList;

public:
    Resource(string n, int a) : Name(n), amt(a) {}
    Resource(string n) : Resource(n, 0) {}
    Resource() : Resource("", 0) {}

    string getName()
    {
        return this->Name;
    }
    int getAmt()
    {
        return this->amt;
    }
    void setName(string n)
    {
        this->Name = n;
    }
    void setAmt(int a)
    {
        this->amt = a;
    }
    friend ostream &operator<<(ostream &out, const Resource &r)
    {
        out << r.Name << ";" << r.amt;
        List<string> temp = r.clientList;
        for (Node<string> *app = temp.getHead(); app != NULL; app = app->getNext())
        {
            out << ';';
            out << app->getValue();
        }
        return out;
    }
    friend istream &operator>>(istream &in, Resource &r)
    {
        string Name;
        string amt;
        string params;
        string tempVar;
        getline(in, Name, ';');
        getline(in, amt, ';');
        getline(in, params, 'n');
        const int paramsOriginalLength = count(params.begin(), params.end(), ';');
        for (int i = 0; i < paramsOriginalLength; i++)
        {
            r.clientList.insert(params.substr(0, params.find(';')));
            params.erase(0, params.find(';') + 1);
        }
        r.setName(Name);
        r.setAmt(stoi(amt));
        return in;
    }
};

#endif



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?