Search code examples
c++arraysobjectfor-loopcycle

c++ Cycle cuts out randomly?


First I have this class:

class Recept
{
    private:
       int serves;
       string* ingredient_name;
       int* ingredient_number;
       float difficulty;

    public:
       Recept(int a=0, string* b = NULL, float c = 0.0, int* d = NULL)
       {
          serves = a;
          ingredient_name = b;
          difficulty = c;
          ingredient_number = d;

       }

    ~Recept()
    {
        delete ingredient_name;
        delete ingredient_number;
    }
};

An object to store all the available recipes:

Recept* AvailableRecipes;

And this function to initialize this object. The only thing main() does is call this function.

void OpenRecipes()
{

    SetCurrentDirectory("\Recipes");
    system("dir /b > a.txt");

    ifstream filelist;
    filelist.open("a.txt");
    stringstream newstrstr;
    newstrstr << filelist.rdbuf();
    string seged = newstrstr.str();

    filelist.clear();
    filelist.seekg(0, ios::beg);

    newstrstr.str(std::string());
    AvailableRecipes = new Recept[count_words(seged)-1];

    string filename;
    int counter = 0;

    cout << "Total number of iterations needed: " << count_words(seged) << endl;

    for(int i = 0; i < count_words(seged) ; i++)
    {
        cout << "i: " << i << endl;
        filelist >> filename;

        if(filename != "a.txt")
        {
            stringstream newstrstr;
            ifstream input;

            input.open(filename.c_str());
            newstrstr << input.rdbuf();


            string seged2 = newstrstr.str();
            int ingredient_num[(count_words(seged2) - 2) / 2];
            string ingredient_name[(count_words(seged2) - 2) / 2];
            float difficulty;
            int serving;
            input.clear();
            input.seekg(0, ios::beg);

            input >> serving >> difficulty;

            int IntContain;
            string StringContain;

            for (int j = 0; j < sizeof(ingredient_num)/sizeof(ingredient_num[0]); j++)
            {
                input >> IntContain >> StringContain;
                ingredient_num[j] = IntContain;
                ingredient_name[j] = StringContain;


           }

            Recept a = Recept(serving, ingredient_name, difficulty, ingredient_num);

            AvailableRecipes[counter] = a;

           counter++;


            newstrstr.str(std::string());

            input.close();

            cout << "No error so far" << endl;


        }

    }

}

Basically this function is supposed to: -Read filenames from subfolder /Recipes

-Store filenames in "a.txt" in this same folder.

-Open the files 1 by 1, and create a Recipe object based from the text in them.

-Add Recipe object to AvailableRecipes object array.

The problem is, the cycle cuts out seemingly randomly, for some reason. I'd like to know why, and how I could fix it :s

Example output:

Total number of iterations needed: 4
i: 0
No error so far
i: 1
i: 2

Process returned -1073741819 (0xC0000005)   execution time : 1.312 s
Press any key to continue.

//In this example, iteration 0 was working with a valid file ( !="a.txt) ,iteration 1 was dealing with "a.txt, and iteration 2 was another valid file.

I'm a rookie, very much so, so please be nice :/ Using CodeBlocks, with minGW on win64


Solution

  • There are two problems already in these two lines

            Recept a = Recept(serving, ingredient_name, difficulty, ingredient_num);
    
            AvailableRecipes[counter] = a;
    

    First you create a which stores pointers to the local variables ingredient_name and ingredient_num. These pointers will be dangling as soon as you leave the if-statement where they are declared.

    Then on the next line, you create a copy of a in the AvailableRecipes array. Now you have two copies of each pointer.

    Eventually, all of this will end up in the destructor of Recept, trying to delete the pointers. Not only are they dangling, but you also have several copies of each. Double fault!