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
8 comments

I'm also interested in this.

0

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.

0

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...



       
        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

0

So coming back to why is Resharper warning against it, is it because you could potentially keep a reference to some vars without knowing it? That already happens via the lamba method itself.

I'm trying to understand why is this a warning. What are the bad things that can happen? Maybe an example when this is a problem...

Cheers,
Calin

0

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!"

1

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).

1

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.

1

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

0

Please sign in to leave a comment.