2

Im using this class in a larger function which isn't terminating properly.

I've had to resort to commenting out the algorithm one chunk at a time to narrow down where the problem is beginning.

The whole thing works as written but ultimately terminates in error and terminates the main() that is calling it.

Anyways, when I instantiate this class, the problem begins. Im assuming it must be a problem with the destructor, causing the error when the object falls out of scope.

Here is the class definition as well as the constructor/destructor:

class Entry
{
    private:
        int act_count; //number of activities for generating array MUST BE DETERMINED BEFORE INSTANTIATION
        int ex_count; //number of expenditures for generating array

    public:
        Entry(int, int); // constructor
        ~Entry(); // destructor
        string date; // functions like a title
        Activity * act_arr; // pointer to an array of activities
        Expenditure * ex_arr; // pointer to an array of expenditures 
        // list of member functions
};

struct Activity
{
    public:
        string a_name;
        float time;
};

struct Expenditure
{
    public:
        string e_name;
        float price;
};

Constructor:

Entry::Entry(int a_count, int e_count)
{
    // initialization of basic members
    date = day_o_year();
    act_count = a_count;
    ex_count = e_count;

    // allocation of array space
    act_arr = new Activity[act_count];
    ex_arr = new Expenditure[ex_count];
}

Destructor:

Entry::~Entry()
{
    // prevents memory leaks when object falls out of scope and is destroyed
    delete act_arr;
    delete ex_arr;
}

Are there any egregious errors here? I hope this isn't too much code to pique some interest.

Thanks in advance.

MagicGAT
  • 135
  • 7
  • 2
    Does this answer your question? [What is The Rule of Three?](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – Richard Critten Dec 12 '19 at 23:52
  • Im reading through this now, thanks for the link. This wasn't in the list of recommended posts – MagicGAT Dec 12 '19 at 23:58
  • 1
    If you use smart pointers instead of raw pointers you should also read: https://stackoverflow.com/questions/44997955/rule-of-zero-confusion and also the links it contains. – Richard Critten Dec 13 '19 at 00:05
  • @Richard Critten bookmarked for reading this evening. many thanks. – MagicGAT Dec 13 '19 at 00:13
  • Even better than smart pointers and a lot better than original manual allocation would be to use `std::vector` instead. By using appropriates containers, you avoid a lot of code and a lot of potential problems like memory leaks, double deletions which could happen with original code. – Phil1970 Dec 13 '19 at 00:49

1 Answers1

2

For starters, I think you need this (delete[] array):

Entry::~Entry() {
    // prevents memory leaks when object falls out of scope and is destroyed
    delete[] act_arr;
    delete[] ex_arr;
}

But besides that, exactly what do you mean by "isn't terminating properly"?

Q: Do you have a stack trace/core dump?

Q: Have you stepped through code with the debugger?

Q: Do you have a specific error message you can copy/paste into your post?

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • I mean that, `Entry` and the function that calls it works as I intend. It generates the arrays, it takes data into the arrays, it writes the contents of the array to files etc. BUT when the function terminates, it crashes the `main()` that it is part of – MagicGAT Dec 12 '19 at 23:50
  • 1) I need to look up what those concepts are 2) I dont know how to use the debugger 3) The only error message is ' (Exit Code -1)'. I use Eclipse. Cant find a directory of what various exit codes mean. – MagicGAT Dec 12 '19 at 23:52
  • I will do my best to learn about the concepts you mentioned, as well as looking up clarification on how to correctly use `delete`. Thank you very much – MagicGAT Dec 12 '19 at 23:53
  • @MagicGAT **1.** If I get it correctly, your function is not a part of `main()`, it is just called from `main()`. **2.** When a function works correctly and crashes on return, a likely reason is an overwritten stack. Investigate for possible buffer oferflows in your function. – CiaPan Dec 12 '19 at 23:55
  • @Ciapan Yes, that is correct, it is called from main(). Ok I will look there at the stack and buffer overflows. Thank you. – MagicGAT Dec 12 '19 at 23:57
  • @paulsm4 The problem was indeed the lack of square brackets. Nice that it was ultimately a simple fix but, this was still a good wake up call to investigate some of these 'deeper' concepts and finally learn how to use debug mode lol. Thanks to all for the suggestions – MagicGAT Dec 13 '19 at 00:06
  • 1
    Glad it worked out! Here's a short tutorial on the Eclipse CDT debugger (it sounds like that's probably what you're using): https://help.eclipse.org/2019-09/index.jsp?topic=%2Forg.eclipse.cdt.doc.user%2Fgetting_started%2Fcdt_w_debug.htm. And I'm guessing you already noticed the link on `delete[]`: https://en.cppreference.com/w/cpp/memory/new/operator_delete – paulsm4 Dec 13 '19 at 07:00