Click here to Skip to main content
15,914,165 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello,

I am getting a problem in cpp.I am just doing like this

C#
~user(){
                current = (account *) first;
                while(current)
                {
                        account *next = current->next;
                        current->~account();
                        current = next;
                }
}



and in the destructor of account i am doing only

cout<<" Destructor of account :: Deleting memory ";
delete this;

Please tell me what i am doing wrong in this.
This is pring Destructor of account :: Deleting memory continouslly.

Thanks in advance.
Posted

1 solution

Thanks.
It is solved.
I have just change one line only and that is
instead of doing current->~account(); i am doing delete(current);
and in destroctor i am doing nothing.
 
Share this answer
 
Comments
[no name] 24-Dec-13 3:00am    
It may have been better to delete the question when you figured it out. This isn't facebook and no one cares about what happens in every minute of your day. Posts like this just clog up the forum.
BaldevS 24-Dec-13 5:20am    
ok..then another question for you..how to delete any question?
I really dont know this.
Timberbird 24-Dec-13 7:09am    
Yes, you shouldn't call destructor explicitly. And one more thing about your code

delete this;

Don't do that. Never. That was causing infinite print of your "Deleting memory" message, since deleting an object calls the destructor implicitly. And remember - you should never delete an object from one of its own methods (unless it's a class method, but that's not the case), not only destructor. Every time you write a line

delete this;

, you are dooming your program to experience a hard to discover, unexpected critical errors.

When delete call is finished, control is returned to the method which called it... or at least where this method should be. But now it's not there, since the object is already removed! Well, in most cases that's not the problem, in fact method implementation is still there. But if this block of memory has already been overwritten, your program executes a random block of code it finds in place of a former method. Results? Crash in most cases
BaldevS 24-Dec-13 8:45am    
I really appreciate your help but if i wont write "delete(current) " then also it will call my defined destructor or not?
If not then why because after going out of scope the pointer should call the destructor.
Or i need to implement smart pointer for that.

Thanks for this much of explanation for my mistake.
Timberbird 26-Dec-13 6:43am    
Sorry, I must have not written it clearly. Everything's ok with code delete [some variable], it's totally legit way to remove an object/whatever when it's no longer needed. But you must never try to delete this, since it leads to the consequences explaied above.
If you want to remove the dynamically created object when it goes out of scope, do it explicitly by calling delete [variable]. If you wand to create an object dynamically and be sure it is removed automatically when going out of scope, not bothering yourself with delete statement, use auto_ptr, for example (I prefer to keep track of my variables myself, so I don't use it much, but it's certainly a valid option).

As to the destructor: when you write delete [object], object's destructor is called automatically. You don't have to call it from your code explicitly, like object->~class(), delete this or whatever

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900