What’s a destroyer for?

Asked

Viewed 742 times

8

In some languages, classes have methods destructors.

In the ones I’ve seen, he’s declared a builder with the sign ~ in front. Something like:

public class Foo
{
    public ~Foo()
    {
        //Fazer algo
    }
}

What are destructive methods for? What is the need to create them?

  • Related or duplicated: http://answall.com/q/9078/101

  • @I think the answer that was chosen does not answer my question. The other one even responds, but it might be a good idea to wait for other answers. Don’t you think?

  • @Jéfersonbueno and my answer? There it is duplicated.

  • It is for people who have reached your question to see that there is a related (or complementary) subject and can already amend the reading of the other topic. In addition, it is "featured" in the links below "Linked". Making this link easier.

  • @bigown in general, answer. Although she is specific to C#, you can have a notion. If you want I can mark it as duplicate.

  • @Math understood. Thank you.

Show 2 more comments

2 answers

3


Methods Destrutores are used to release memory dynamically allocated by the class, to delete references to it, when it does not exist.

In Programming Languages you have the Garbage Collector, the use of destructive methods is not necessary, since the Garbage Collector takes care of doing this.

The need to create them is in cases where language does not have the Garbage Collector, and it is necessary to destroy the class after its use, so that it does not occupy mémoria.

In languages that have the Garbage Collector, use is only necessary when using unmanaged resources.

The most common types of unmanaged resources are objects involving operating system resources such as files, windows, network connections, or database connections. Microsoft

In such cases the Garbage Collector does not know how to release and clear unmanaged resource.

Not all languages make use of the signal ~ to designate a destructor.

In PHP for example is used __:

void __destruct ( void )

Python

def __del__(self):

0

They are special methods that contain clear code for the object. You cannot call them explicitly in your code, as they are implicitly called by GC. In C# they have the same name as the class name preceded by the ~sign. Example:

class MyClass{

~ MyClass(){
.....
  }
}

In VB.NET, destroyers are implemented, replacing the Finalize method of the System.Object class.

Why use?

When Voce is writing objects that manipulate resources not managed[To destroy unmanaged resources]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.