I have a magazine Ipad app that dynamically adds/removes webviews from a scrollview to keep the memory usage low. For example, I have 3 view containers, and in each one I can remove existing children…
foreach (UIView subv in targetView.Subviews) {
foreach (UIView subsubv in subv.Subviews) {
subsubv.RemoveFromSuperview();
subsubv.Dispose();
}
subv.RemoveFromSuperview();
subv.Dispose();
}
and then add a new Web subview component (or scrollview with webviews) to the container.
I added the second level of children deletion, but there is probably another level in some cases. The emulator is perfect, but on the IPad, the memory usage just increases until it crashes with a low memory exception.
so…..
i) Does monotouch destroy a UIView with all nested children automatically?
ii) Instruments is not showing any memory leaks, what is the best way to debug this?
thanks.
If there are no more references to your UIView then it should get GC’d. A quick way to find out if your object was collected is to create a class that inherits from UIView (or whatever class you’re using) then create a finalizer/destructor for the class. In the finalizer do a console write.
When you run your program make sure you see this being written to the console when you expect the object to be collected. In my experience, the simulator collects the objects almost as instantly as the object looses it’s last reference. On the device it may not be as quick.