Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Monotouch equivalent of dealloc?

I am looking at an example online that contains this code in objective-c

    -(void)dealloc {
    [activeController viewWillDisappear:NO];
    [activeController.view removeFromSuperview];
    [activeController viewDidDisappear:NO];

    [activeController release];
    [super dealloc];
}

I assume the MT equivalent would be Dispose, am I correct?

I won't need to call the:

    [activeController release];
    [super dealloc];

methods as they will be Garbage collected on Monotouch, is this also correct?

like image 699
Chris Kooken Avatar asked Oct 26 '10 15:10

Chris Kooken


2 Answers

MonoTouch is garbage collected, so you do not need to worry about doing the deallocation yourself.

That being said, there are times when you are aware that you are keeping some large resources in memory and you want to assist the system by disposing the resources right away instead of waiting for the garbage collector to kick in.

This is when calling Dispose comes in handy: it releases the resources associated before the garbage collector has to. This is particularly important for large objects, like images, as images are stored on the unmanaged heap, while object references are stored in the managed heap.

What this means is that if you have a 8 megabyte image: 8 megabytes are stored in the unmanaged heap (managed by Objective-C) and 1 pointer (4 bytes) in the managed heap. As far as Mono's Garbage Collector is concerned, you are using 4 bytes, not 8 megs.

So it is times like this when you can assist the system by calling dispose: you know that the innocently looking "myImage" variable actually points to a large blob of memory.

like image 175
miguel.de.icaza Avatar answered Sep 28 '22 08:09

miguel.de.icaza


Monotouch is garbage collected. Before an object is garbage collected, the destructor for the object is called.

Here's Microsoft's page about C# destructors. I don't know if there's more relevant documentation for destructors in Monotouch.

like image 38
Greg Avatar answered Sep 28 '22 10:09

Greg