This is a migrated thread and some comments may be shown as answers.

VirtualQueryableCollectionView<T> and CA2000: Dispose objects before losing scope

1 Answer 67 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Remco
Top achievements
Rank 1
Remco asked on 16 Feb 2012, 11:01 AM
Hello,

I create a VirtualQueryableCollectionView<T> in my view model's constructor and store a reference in a private instance field. Code analysis flags 'CA2000: Dispose objects before losing scope' for the line in the constructor where the VirtualQueryableCollectionView<T> is created and assigned to the private instance field. The full message is:

CA2000 : Microsoft.Reliability : In method 'EntriesViewModel.EntriesViewModel(ILogbookViewerContext, IAppService, ExceptionManager)', object '<>g__initLocal0' is not disposed along all exception paths. Call System.IDisposable.Dispose on object '<>g__initLocal0' before all references to it are out of scope.

So then I noticed that VirtualQueryableCollectionView<T> implements IDisposable in its base class QueryableCollectionView. Although strange that it mentions an object <>g__initLocal0 instead of VirtualQueryableCollectionView<T>. I would have also expected 'CA1001: Types that own disposable fields should be disposable' to be flagged instead of 'CA2000: Dispose objects before losing scope' with the full message

CA1001 : Microsoft.Design : Implement IDisposable on 'EntriesViewModel' because it creates members of the following IDisposable types: 'VirtualQueryableCollectionView<T>'. If 'EntriesViewModel' has previously shipped, adding new members that implement IDisposable to this type is considered a breaking change to existing consumers.

But that's not what I got. In any case I implemented IDisposable in my view model to dispose of the VirtualQueryableCollectionView<T>. Unfortunately that did not make the 'CA2000: Dispose objects before losing scope' go away. Anybody any ideas?

Remco

1 Answer, 1 is accepted

Sort by
0
Remco
Top achievements
Rank 1
answered on 09 Mar 2012, 02:54 PM
I found out this is because I use an object initializer to set some properties on the VirtualQueryableCollectioView when I create it:
this.Entries = new VirtualQueryableCollectionView<EntryPresentationModel>() { LoadSize = this.appService.LoadSize, VirtualItemCount = this.appService.LoadSize };

Code analysis does not complain when I do:
this.Entries = new VirtualQueryableCollectionView<EntryPresentationModel>();
this.Entries.LoadSize = this.appService.LoadSize;
this.Entries.VirtualItemCount = this.appService.LoadSize;

Isn't that strange?
Tags
General Discussions
Asked by
Remco
Top achievements
Rank 1
Answers by
Remco
Top achievements
Rank 1
Share this question
or