0

I want to use method chaining in C++, but I am worried about memory leakage--I don't know how the garbage collection is handling the intermediary instances returned by the method calls.

Please note, I am intentionally not returning (this) because it is a requirement of the program.

//important to note that in the constructor of Polynomial, I allocate a new 

DoublyLinkedList instance as a private member
class Polynomial {
    private:
    DoublyLinkedList * poly;
    //other stuff, constructors and destructor
};

//this is the primary constructor I am using. Notice that I allocated one of its members in the heap
Polynomial::Polynomial(int array [], int size) {
    poly = new DoublyLinkedList;
    //fill in with the array and size etc.
}

Polynomial::~Polynomial() {
    //deallocate the allocated members
}

//------ ADD --------
Polynomial Polynomial::add(Polynomial* polyB){  //does addition by default
    //perform add operations and save results into an array called "result" for passing into a Polynomial constructor

    return Polynomial(result, maxSize); //will return a NEW instance
}
//example prototypes which return a Polynomial instance (not a pointer to a new instance)
Polynomial Polynomial::add(Polynomial * polyB);
Polynomial Polynomial::subtract(Polynomial * polyB);
Polynomial Polynomial::multiply(Polynomial * polyB);

DoublyLinkedList::DataType polyArrayA [] = {1,3,4};
DoublyLinkedList::DataType polyArrayB [] = {5, 5, 7};
Polynomial p1 = Polynomial(polyArrayA, 3);
Polynomial p2 = Polynomial(polyArrayB, 3);

Polynomial p3 = p1.add(&p1).multiply(&p1).subtract(&p2);

p3.print();

Knowing that garbage collection handles statically declared variables, I decided that the methods could return "Polynomial" versus returning a pointer. The thing is that the Polynomial is a newly instantiated instance (created in the method) and it has a dynamically allocated member--so I need to ensure it's deconstructor is being called.

After each method is called, a new (AFAIK) "statically" declared instance is created and further methods are called using the new instance. But what happens to the intermediate instance?

Klik
  • 1,757
  • 1
  • 21
  • 38
  • Not really sure what you mean by not returning this as its a requirement of the program; that's the way to do it. However, you should be aware that this is extremely non-idiomatic in C++. Method chaining generally returns a reference (*this). If you want to return a new instance, it's generally preferable to write a function add or operator+ that's not a member, for various reasons. Also, the word "static" does not mean what you think it does in C++. – Nir Friedman Jun 18 '15 at 00:21
  • Exactly, since method chaining generally returns (*this), but mine does not, I was wondering if it's feasible. I wrote this with specific constraints and the method chaining was an experiment for me; I see now that the destructors are indeed called and that's what was the most important question to me. – Klik Jun 19 '15 at 02:52

1 Answers1

4

There is no garbage collection in C++, you need to clean up the memory, you can write a destructor.

Polynomial::~Polynomial() {
    delete poly;
}

Anything you new you must also delete or you are leaking memory. Also, if this is how you are creating your poly member variable, it should be of type

DoublyLinkedList* poly;

Know that in C++ you should avoid new and delete where it isn't needed, and prefer to use RAII semantics.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Yes, thanks--that was a typo. I do have a destructor written, but my main concern is that I'm not sure if it's being called. I'll include an example of the add function so you get a better picture. Also, perhaps garbage collection is not the proper term? I thought that's how you described how the program automatically gets rid of static variables after leaving the scope. – Klik Jun 17 '15 at 19:57