Jump to content
  • Advertisement
Sign in to follow this  

Trouble-making destructor

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a destructor that operates out of nothing, right in the middle of application. It operates at the end of a function:

Update( some arguments);
Log("End of Update\n");

//Update definition
void Update(...)
//code removed
Log("Exiting Update function \n");

// Here is the place where the distructor gets called 

I use a Log file to track down the errors. The Log file output:
Exiting Update function 
--------WARNING: Sound object not released-------- 
Shuting down sound ClkChildB
--------WARNING: Sound object not released--------
Shuting down sound ClkParentB
End of Update

The warnings are printed by the distructor. Could anyone point out the cause of this behavior ?

Share this post

Link to post
Share on other sites
Post the real code, or at least post code which has been slightly stripped down, but still produces the problem.

You haven't posted you code which is logging your warning messages, either.

You do know that anything that was constructed inside a function is of course destructed when the function exits (except static vars)? Even if you have an early return statment.

Share this post

Link to post
Share on other sites
Uh right, the destructor of an object is called when it goes out of scope.

Most likely you're passing the object by value as a parameter to Update. A new object is created at entry of the function, and is thus destructed when the function ends:

#include <iostream>

struct Object{
int x;
std::cout << "Made 1 object.\n";
std::cout << x << " dies!\n";

void Update(Object foo){
// stuff!

int main(){
Object *bar=new Object();
delete bar;

So what should this code do?

Made 1 object.
42 dies!
42 dies!

The copy's destructor is called at the end of Update(), thus creating the perhaps unexpected duplication of death messages.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!