Destructor Not Getting Called in ViewModel

Topics: Prism v4 - Silverlight 4
Sep 19, 2011 at 9:42 PM

Hi Guys,

 

I have started a project using the Prism Navigation Framework. So far all is going well.
Though experiencing some strange troubles. I decided to add a simple logging line in the destructor
for my viewmodel. Even after closing the window, by setting the _keepalive to false. This is creating
some strange problems for me and my domain context.

It is only one specific type of view that I would like to completely terminate the item.

Is this possible or am I perhaps something a greater picture?

 

Thanks

 

Louis

Developer
Sep 20, 2011 at 6:53 PM

Hi Louis,

Basically, if the view does not get disposed, it means that something is holding an strong reference to the view. There are several reasons because this could happen.

For example, if you register the view as a Singleton, the container will hold an instance (the singleton instance) of the view as long as the container itself is kept alive.

Also, a strong reference to the view could be holded if your are using Interaction Requests, as there is a known issue where using Interaction Requests creates memory leaks, so the view is never destroyed.

If this does not solve your problem, it would be helpful if you could provide us with a repro-sample application portraying this problem, so we could get a better understanding of your scenario and help you find the cause behind this problem.

I hope you find this useful,

Damian Cherubini
http://blogs.southworks.net/dcherubini

Sep 22, 2011 at 8:17 AM

Hi DCherubini,

Thanks for your reply. I tested the code after having taken out the interaction request. I

still only see the destructors getting called once I close the browser. I will work on creating a

sample that I can post, It is just a little hard doing it at the moment with the current code.

As it involves all the normal stuff, like domain contexts and loading etc. So the line count at this

stage is sitting at 2200. But I will indeed create something smaller and send it so it could be looked at.

Sorry for the long delay, was busy installing our new servers the last day or so.

Thanks

Louis

Sep 25, 2011 at 9:53 PM

Hi There,

I have been trying to find the trouble again for the best part of today.

So what I did just now, was to test on another program and see if the same trouble exists.

I used the example application SLPrismv4NavigationUnity. I went into the ItemViewModel

added a destructor code block and just in there I send a message to Debug.WriteLine(), a small test

I further removed the interaction request code and started testing. From this I could see that the destructor is

only called once I close Internet Explorer.

I hope you can help me out with this one. I used the Telerik Just Trace on my application and I cannot see anything

that holds a reference that would cause the class not to be destroyed.

Thanks

Louis Lewis

Developer
Sep 26, 2011 at 6:35 PM

Hi Louis,

It would be helpful if you could provide us with a repro-sample application that portrays this problem considering your findings regarding this subject, so we could get a better understanding of why is it happening and help you find a possible solution for it.

Regards,

Damian Cherubini
http://blogs.southworks.net/dcherubini

Sep 30, 2011 at 8:37 AM

Hi Damian,

Any idea where I can upload the project too?

Thanks

Louis

From: DCherubini [email removed]
Sent: 26 September 2011 07:36 PM
To: louis.lewis@pinelakemarina.co.za
Subject: Re: Destructor Not Getting Called in ViewModel [CompositeWPF:273124]

From: DCherubini

Hi Louis,

It would be helpful if you could provide us with a repro-sample application that portrays this problem considering your findings regarding this subject, so we could get a better understanding of why is it happening and help you find a possible solution for it.

Regards,

Damian Cherubini
http://blogs.southworks.net/dcherubini

Developer
Sep 30, 2011 at 1:52 PM

Hi,

You could upload your sample to SkyDrive, or any similar site for sharing files.

Regards,

Agustin Adami
http://blogs.southworks.net/aadami

Oct 2, 2011 at 8:34 PM

Hi Guys,

I always try to look into matter in depth before I just ask others.

I took a deeper look into the problem I have been facing with the destruction

of classes. I come from a C++ background and I am used to when I use the delete operator

on instantiated items, I can see the destructor called as soon as I call delete on the object.

I kept a watchful eye on the items that I am creating in this Silverlight project.

I then see after I drive the program a bit and go about my tasks in it, I now and then see the line

I have been looking for in the output window of Visual Studio. Just a simple line of text saying item

destructed. When I first saw this I was indeed very happy. But looking into the matter further, I again see that

the amount of items I instantiate are not directly equal to the amount destructed. There is also no way of knowing when an

item is going to be destructed. For example if I quickly open 10 editors and play around a bit and close them all one by one,

I can expect to see two or three being destructed and perhaps over time another one.

I would like to have it that when I push close on a window, I would like it right then to be destructed.

I will upload the default prism navigation with unity project to skydrive for those who do not have it.

The only change I have made to the entire application is adding a destructor to the itemviewviewmodel.

In it I simply write a line to the output window.

Hopefully someone can help see how to better this.

Thanks

Louis