Possible false positive from EnsureLocalDisposalRule

39 views
Skip to first unread message

Bevan

unread,
May 9, 2009, 7:08:06 AM5/9/09
to Gendarme
I don't understand why the EnsureLocalDisposalRule is flagging some of
my code.

Here's one of my routines as an example:

public static IEnumerable<T> ReplaceSelected<T>(
this IEnumerable<T> source,
Predicate<T> selector,
Converter<T, T> replacment)
{
foreach(var item in source)
{
if (selector(item))
{
yield return replacment(item);
}
else
{
yield return item;
}
}
}


It's my understanding that foreach guarantees the disposal of the
Enumerator after use (see http://msdn.microsoft.com/en-us/library/aa288257(VS.71).aspx
for one statement of this).

Am I missing anything here, or is this a false positive from
EnsureLocalDisposalRule?

Thanks,
Bevan.

Cedric Vivier

unread,
May 20, 2009, 12:54:17 AM5/20/09
to gend...@googlegroups.com
Hey,

Sorry for late reply.
I cannot reproduce (ie. the code below compiled with gmcs trunk does not issue a defect when analyzed with gendarme trunk), anyone can?

Bevan, did you compile your code with gmcs or csc?
If you still see this defect can you send here the IL dump of this method.

Cheers,

Cedric Vivier

unread,
Jul 28, 2009, 5:21:37 AM7/28/09
to gend...@googlegroups.com, bevan...@gmail.com
Hey Bevan,


On Wed, May 20, 2009 at 12:54 PM, Cedric Vivier <ced...@neonux.com> wrote:
On Sat, May 9, 2009 at 7:08 PM, Bevan <bevan...@gmail.com> wrote:
It's my understanding that foreach guarantees the disposal of the
Enumerator after use (see http://msdn.microsoft.com/en-us/library/aa288257(VS.71).aspx
for one statement of this).

I stumbled upon a similar false positive some time ago and finally have had some time today to investigate about it.
The root issue is that the rule did not handle value type disposables.

The false positive I noticed was on a simple foreach loop on a List<T>, which conveniently enough (but not for the rule) returns a value-type enumerator (the disposable type in the context of the rule).

I could not reproduce your testcase on gmcs because it generates a reference generator whereas csc generates a value type generator (at least with your testcase).

This is now fixed in trunk as of rev. 138805

Thanks,

Reply all
Reply to author
Forward
0 new messages