8 Replies Last post: Jul 24, 2012 2:44 AM by Eric Gurney  
Calin Pirtea Newbie 26 posts since
Sep 23, 2011
Currently Being Moderated

May 14, 2012 12:08 PM

Why is resharper suggesting "implicitly captured closure"

 

 

        [Test]

        public void TestPositions()

        {

            // setup

            var credentials = new byte[] {1, 2, 3};

            var positions = new List<Position>

                                {

                                    new Position {PositionNumber = 21},

                                    new Position {PositionNumber = 22}

                                };

 

            m_container.MockTerminalInfo.Setup(te => te.GetStuff(positions)).Returns(new IPSubnet("254.254.99.0/24"));

            m_container.MockTerminalInfo.Setup(te => te.GetDifferentStuff(positions, credentials, someLogicalField)).Returns(new IPSubnet("254.254.99.0/24"));


       }

 

       Bool someLogicalField = true;

 

 

Resharper gives the warning:

"Implicitly captured closure: credentials and this"

 

Can someone please explain this?

Also why is it a warning intead of maybe suggestion/hint?

 

Cheers,

Calin

Werner Beroux Newbie 3 posts since
Nov 7, 2011
Currently Being Moderated
Jun 21, 2012 4:43 PM in response to: Calin Pirtea
Re: Why is resharper suggesting "implicitly captured closure"

I'm also interested in this.

Maximilian Raditya Newbie 63 posts since
Dec 31, 2005
Currently Being Moderated
Jun 21, 2012 5:54 PM in response to: Calin Pirtea
Re: Why is resharper suggesting "implicitly captured closure"

Me too.

 

I'm interested in what consequences on doing so. And further, it would cause troubles, then how to mitigate this issue.

 

So far, the message itself doesn't tell much about it.

Greg Law Novice 180 posts since
May 2, 2005
Currently Being Moderated
Jun 25, 2012 2:09 AM in response to: Calin Pirtea
Re: Why is resharper suggesting "implicitly captured closure"

If you decompile this code with dotPeek (enable "Show compiler-generated

code" option before decompiling the method), you'll see that it calls the

Lambda expressions using a backing class to hold all of the parameters:

 

private sealed class c__DisplayClass8 containing the credentials array,

the positions list, and this. Of course, the above work-around is somewhat

ugly so take it with a grain of salt.

 

The interesting thing here is that if the positions list is in the outer

scope, the compiler goes back to the original behavior of using a single

backing class to store the parameters for both Lambda expressions.

 

"Calin Pirtea"  wrote in message

news:31622729.43081336982924120.JavaMail.devnet@confluence.jetbrains.net...

 

 

 

        Test

        public void TestPositions()

        {

            // setup

            var credentials = new byte[] {1, 2, 3};

            var positions = new List

te.GetDifferentStuff(positions, credentials, someLogicalField)).Returns(new

IPSubnet("254.254.99.0/24"));

 

       }

 

       Bool someLogicalField = true;

 

 

Resharper gives the warning:

"Implicitly captured closure: credentials and this"

 

Can someone please explain this?

Also why is it a warning intead of maybe suggestion/hint?

 

Cheers,

Calin

 

---

Original message URL: http://devnet.jetbrains.net/message/5459460#5459460

 

 

Andrey Serebryansky JetBrains 2,900 posts since
Jan 12, 2005
Currently Being Moderated
Jun 26, 2012 12:57 PM in response to: Calin Pirtea
Re: Why is resharper suggesting "implicitly captured closure"

Hello Calin

     Yes, ReSharper warns you that this lambda implicitly captures references to 'credentials' and 'this', so if this lambda is "long-living", it can hold these objects and respective memory for long time. It's especially important to make sure that large objects do not get implicitly captured. Thank you!

 

 

Andrey Serebryansky

Senior Support Engineer

JetBrains, Inc

http://www.jetbrains.com

"Develop with pleasure!"

Werner Beroux Newbie 3 posts since
Nov 7, 2011
Currently Being Moderated
Jun 26, 2012 12:43 PM in response to: Andrey Serebryansky
Re: Why is resharper suggesting "implicitly captured closure"

That's a good explanation. It could be a good idea to include a link to this answer from the warning (especially since there is no automatic correction offered).

Drew Noakes Novice 191 posts since
Aug 21, 2002
Currently Being Moderated
Jul 20, 2012 5:57 PM in response to: Werner Beroux
Re: Why is resharper suggesting "implicitly captured closure"

Agree. Would be good to have one of the "Why is ReSharper suggesting this?" links on the context menu.

 

Thanks for the explanation. Look forward to seeing how useful this new inspection turns out to be.

Eric Gurney Newbie 1 posts since
Jul 24, 2012
Currently Being Moderated
Jul 24, 2012 2:44 AM in response to: Calin Pirtea
Re: Why is resharper suggesting "implicitly captured closure"

So what is the best way to suppress in a mocking scenerio where there will be many implicitly captured closures while leaving it active for the rest of the project.

Or is there a way to modify the code so there isn't an implicitly captured closure?

 

Thanks,

Eric

More Like This

  • Retrieving data ...