Archived

This topic is now archived and is closed to further replies.

By reference return & memory leak

This topic is 5802 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I''ve got this code from my "Intro to CG" course and i''m trying to get rid of all these memory leaks the teacher left in the code like temporary object never deleted or chained list with no defined destructor. My question is conserning code that looks alot alike this next piece of code:

Some_Vector_Class & Some_Vector_Class::get_definition()
{
Some_Vector_Class * Ret;

// Let say that x, y, z are membre variables

Ret = new Some_Vector_Class(x, y, z);

return (*Ret);
}

By calling repetively the get_definition() like follows, am I creating a memory leak ? Does the "by reference" return type (i.e.: the "&" symbol in the function declaration) make any difference ? Here it goes :

Some_Vector_Class * a_vector = new Some_Vector_Class();
(*a_vector) = another_vector->get_definition();

// And later...

delete a_vector;

The guy is also teaching OOP, but by the lack of any delete keyword in his code, you got to ask yourself about his competence. Thanks, Mind

Share on other sites
oh...
1. I don''t know why it is ::get_definition()... instead of a better ::allocate().
2. Yes, it does leak...... unless the new is overloaded and mimic some garbage collection.
3. I think it is better to return by pointer instead of reference.
Have a nice day.

Share on other sites
Thanks Derek. Now if I could only tell that Ph.D that he should go back to cleaning floors and cutting grass without getting kicked out of his class

Mind

Share on other sites
By calling repetively the get_definition() like follows, am I creating a memory leak ?

Yes.

Does the "by reference" return type (i.e.: the "&" symbol in the function declaration) make any difference ?

None.

Or rather, it only makes things worse. Since you don''t get a pointer a) you don''t have the address (assignment will make a copy of the data, losing the address) b) Users of that function will never suspect they have to delete the return value.

The guy is also teaching OOP, but by the lack of any delete keyword in his code, you got to ask yourself about his competence.

Indeed.

Either return a pointer and document the function so that the users know they have to call delete on it, rather than delete[], free() or platform-dependent memory functions (for shared-memory, memory-mapped files ...). Or give up dynamic allocation and return by value.

Alternatively, use a smart-pointer class as the return type (boost::shared_ptr is an example, see Boost link in sig).

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share on other sites
That''s pretty evil. It should probably be

  const Some_Vector_Class & Some_Vector_Class::get_definition() const {  return *this; }

or

  Some_Vector_Class Some_Vector_Class::get_definition() const {  return *this; }

which would get rid of the leak but doesn''t make alot of sense since you would already have the variable in the firstplace to use the function. Very strange.

Share on other sites
Or maybe it was a sad attempt to do this:

    const Some_Vector_Class & Some_Vector_Class::operator = (const Some_Vector_Class &vect) {  x = vect.x;  y = vect.y;  z = vect.z;  return *this; }

[edited by - smart_idiot on December 3, 2002 1:40:16 AM]

Share on other sites
quote:
Original post by Fruny
Alternatively, use a smart-pointer class as the return type (boost::shared_ptr is an example, see Boost link in sig).

There is also a smart pointer called auto_ptr hidden somewhere in the C++ libs, so you may not need another library.

Peace,
Doc

Share on other sites
quote:
Original post by Doc
There is also a smart pointer called auto_ptr hidden somewhere in the C++ libs, so you may not need another library.

auto_ptr has ownershipt semantics which makes it inappropriate in many contexts, shared_ptr is reference counted and generally does The Right Thing.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share on other sites
quote:
Original post by Fruny
auto_ptr has ownershipt semantics which makes it inappropriate in many contexts,

But fine in this one, and it introduces no overhead of which to speak.

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 10
• 13
• Forum Statistics

• Total Topics
632983
• Total Posts
3009708
• Who's Online (See full list)

There are no registered users currently online

×

Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!