Advertisement

C++ constructors: Am I creating a memory leak by doing this?

Started by October 24, 2002 11:36 AM
4 comments, last by Zorodius 22 years ago
I''ve recently begun to really take the plunge and start using more OOP in my code. I have a function that takes, as an argument, a pointer to an object - in this case, of type Sphere. In this particular case, though, I have some information that is adequate to describe a sphere, but not an actual instance of the class Sphere. So, for convenience''s sake, I''ve added a constructor to my Sphere class that just takes the basic information...

Sphere(triple nCenter, float nRadius){position = nCenter; SetRadius(nRadius);} 
Now, using this constructor, I can invoke my function that takes a Sphere * thusly:

void functionOne(triple a, float b)
{
functionTwo( &Sphere(a, b));
}

void functionTwo(Sphere * sp)
{
... (other code here that uses sp) ...
} 
This seems to work just fine, but I feel a little weird passing a pointer to something that doesn''t actually exist, per se, so I just wanted to ask here: Does what I''m doing create a memory leak? Are there any problems with doing it this way? It would seem to make sense that the object I''m creating in functionOne probably terminates when the function ends, and so I should be wary not to do anything with it in functionTwo that would be contingent on it existing once the function was done. Is this assumption accurate? Thanks!
You don't need to vote for the "lesser of two evils"! Learn about Instant Runoff Voting, the simple cure for a broken democracy!
Hi, can you post all the code that relates to this problem so i can clearly see what you mean. If you created the sphere object in a function without the use of new or maloc(), then there wont be a memory leak by passing a pointer to it. However, if you did use the new operator or the maloc() function, then you need to use delete or free() respectivly. The bottom line, you dont need to free pointer that pointer if you didnt dynamicly allocate its memory.

If that is too confusing, say what you dont understand, and I will try and explain better. Also say if i misunderstood you.

good luck,
-J
Advertisement
You are creating a temporary object (explicit, variable-less call to a constructor), which exists only until functionTwo returns. Using the pointer past that point (once functionTwo has returned) is a dangling pointer error (pointer to an object that no longer exist), but not a memory leak (heap-allocated object that exists without a pointer to it). Thus, you are safe unless you actually store the pointer.

As a matter of style, you should redesign functionTwo to take a reference instead of a pointer. That will save you some trouble :

void functionOne(triple a, float b){  // The object doesn''t exist yet.  functionTwo( Sphere(a, b) ); // Temporary Sphere object.  // The object doesn''t exist anymore.}void functionTwo(Sphere& sp){  // now sp can be manipulated as a real object  // keeping in mind that it will disappear soon.}  


This style is typically found when using the Standard C++ algorithms, passing temporary function objects as algorithm parameter.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
void functionOne(triple a, float b)
{
functionTwo( &Sphere(a, b));
}

Ok, I''m just getting started with C++, so could you explain this one to me? This statement doesn''t seem possible. How can you pass the address of an object template to another function? Don''t you need to create an instance of that object first? Wouldn''t you have to write it as:

functionTwo(new Sphere(a,b));

However, since functionOne would not have a copy of the address, functionTwo would be responsible for freeing the memory, and usually you want to do it in one function, to avoid a leak. Maybe you could do it like:

Sphere* mySphere = new Sphere(a,b);
functionTwo(mySphere);
...
delete mySphere;

Maybe I''ve got this all wrong. If you could explain it to me, that would be helpful. Thanks.

  #include <iostream>class Foo{public:   Foo() { std::cout << "Foo contructor" << std::endl; }  ~Foo() { std::cout << "Foo destructor" << std::endl; }};void Bar( Foo* ptr ){   std::cout << "In function Bar(Foo*)" << std::endl;}int main(){   std::cout << "In main() before Bar(Foo*)" << endl;   Bar( &Foo() );   std::cout << "In main() after Bar(Foo*)" << endl;};  

Output is (should be) :
In main() before Bar(Foo*)Foo contructorIn function Bar(Foo*)Foo destructorIn main() after Bar(Foo*) 


The fact that Zorodiuse is passing a pointer to the object, or the object itself doesn''t change anything.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

void functionOne(triple a, float b)
{
functionTwo( &Sphere(a, b));
}

Ok, I''m just getting started with C++, so could you explain this one to me? This statement doesn''t seem possible. How can you pass the address of an object template to another function? Don''t you need to create an instance of that object first?


Here, Sphere(a,b) is creating an instance of the class. It''s creating a temporary, local object rather than the new-created version which would permanently allocate storage space until deleted.

Take a look at Fruny''s post, noting where the object does and doesn''t exist .. basically it''s created just as functionTwo is called and deleted when the function returns.

You''re right that if he used the ''new'' method he would have to delete it after the function call, but since it''s a ''local'' object it will get cleaned up when it goes out of scope.

Also note what Fruny says about storing the pointer .. if functionTwo() stored the point for later use, you''d get a memory access violation when you tried to access it (or just corrupt data). In that case, you''d have to use new and be careful to clean up (delete) after yourself.

Er .. hope that made some sense


This topic is closed to new replies.

Advertisement