Destructor calls?

Cant find a good name for the title :/

I have a memory leak in an OpenGL application i am creating. I cant seem to find that memory leak.

My question is, will this code be a memory leak:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class A{
	A& doSometime(){
		A *a = new A();
		return *a; //Destructor wont be called, right?

	}

};

void someFunction(){
	A a = doSomethine();		
}//..but destructor will be called here, or?

int main(){
	for(int i = 0; i<99999999;i++)
		someFunction();
	return 0;
}


What i am not sure about is if I return an Object created with new like in the function doSomething above, when will the destructor be called, or do I need to call it my self.


*EDIT* changed the code abit, had placed main() and someFunction() in the class, and forgot an = *EDIT*
Last edited on
If you allocate something with new, it's destructor will not be called until you delete it.

Returning a reference to a new object is probably not a good idea. Passing ownership of objects makes it hard to keep straight who is responsible for deleting the object. It makes leaks far more likely.

What might be happening is you might be making copies of this new object. Those copies would be destructed (since they weren't new'd), but the original object will not be destructed.

Actually... it's very simple. Every new must have a matching delete. If you don't delete it, you get memory leaks.
So i should change:
1
2
3
4
5
A& doSometime(){
		A *a = new A();
		return *a; //Destructor wont be called, right?

	}

to
1
2
3
4
5
A doSometime(){
		A a = new A();
		return a;

	}

But then i guess the copy constructor will be called and after that the locally created object will be destructed?
to be allocated.
Again you have to write a destructor to these resources..
Has been allocated a place.

surely this must be a triple.
Destructor
copy constructor
assignment operator.

Ohh the last code should ofc be:
1
2
3
4
5
A doSometime(){
	A a();
	return a;

}


new returns a pointer:

1
2
3
4
5
6
7
8
9
10
11
A* doSomething()
{
    return new A;
}

int main()
{
    A* a = doSomething();
    // ...
    delete a; // no memory leak
}
1
2
3
4
5
A doSometime(){
	A a;  // no parenthesis here
	return a;

}


This is what I recommend.

flipe's suggestion will also work, but IMO is sloppy and errorprone becaues it passes ownership.

EDIT: no offense, filipe. I didn't meant to call your code sloppy -- I just think passing ownership is sloppy and should be discouraged.
Last edited on
Oh, I wasn't suggesting that you actually used it, just showing that new returns a pointer and that dynamically allocated objects must be manipulated by pointer only.

EDIT: none taken, I completely agree :)
Last edited on
A have changed my code to
1
2
3
4
5
A doSometime(){
	A a;  // no parenthesis here
	return a;

}

The memoryleak is now gone. Thanks all for quick and good replies.

One thing though, if speed is preferred, filipes way is bit faster right?
Not necessarily. The compiler can often optimize out object copies, so there might not be any speed difference.

If you want to avoid passing ownership and you want to be sure there's no copy, you can do this instead:

1
2
3
4
5
6
7
8
9
10
void doSomething(A& a)
{
  // make whatever changes to a here that are necessary
}

int main()
{
  A a;
  doSomething(a);  // this will modify a
}
Topic archived. No new replies allowed.