Why is destructor called twice




















Or you can explicitly create your own copy constructor and handle the situation there. There are, of course, other options available, too: you can stop deleting the pointer in the destructor. But why then this second destructor tries to kill the original filtmp and not its copy which was created by compiler Implicitly. Now it looks like both filtmp and its implicit copy inside signal has same pointer what in turn appears to be same object and not copy?

To understand that, we need some background on pointers. When you define a pointer member in your class, all that is actually stored there is a usually 32 bit integer value that points to the real object in memory. So, when the default copy constructor encounters a pointer , it will copy that number.

It will not attempt to create a copy of the object that the pointer refers to. Then, when your destructor calls delete it no longer works on that number; it goes directly to the object in question, and calls it's destructor. The destructor of class A is called 3 times if you include the one called when function main exit.

Look back at my code, i have a getchar function call before the main function returns. March 17th, , AM 6. March 17th, , AM 7. You're right, with copy constructor implemented in class A, the number of constructor plus copy constructor will match the number of destructor.

But you haven't answered my question yet: without the copy constructor implemented, class A destructor is called twice when "myFunction" return. I do understand that the compiler will provide a default copy constructor when one is not found. Please comment. March 17th, , AM 8. You said. Please use vbBulletin codes. March 17th, , AM 9. March 17th, , AM Join Date Apr Posts 27, The destructor for the copy is being called, but you never know where that destructor call comes from because you commented out the copy constructor.

Actually i commented out the copy constructor on purpose. Can you explain? My head aches. Don't you understand that the number of calls to constructors and destructors is always the same? You have N objects of type T, you have N calls to constructors could be more for type T and N calls to the destructor only one for each type. I can't help but wonder about your intent with pushing an address. Let's say you were reading records from a file into a temporary line of text.

You could avoid the constructor and pass only a pointer to the temporary text, but you wouldn't be keeping unique copies of the data that you'd read. In the end you'd have a pile of pointers all pointing to something that is no longer there. Be careful with what you are doing, and try to avoid premature optimization. Perhaps you could provide a little more of the 'big picture'; but in the end be careful. We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.

Why is destructor called twice? Last edited on Mar 6, at pm UTC. It's because you're explicitly calling the destructor - an unbelievably stupid move.



0コメント

  • 1000 / 1000