Jump to content
Sonjli

Doubt about anonymous method

Recommended Posts

Hi,

I have a small task where I use this technique:

      CreateTask(
         procedure(const Task: IOmniTask)
		var
           lPlugObject: IPluggable;
        begin
...
               lPlugObject := FPlugProtocolFactory(lTaskPlugAndParams);
               // Can I use this?
               lPlugObject.Stoppable := function: Boolean
                  begin
                     result := Task.Terminated or Task.CancellationToken.IsSignalled;
                  end;
               
               lPlugObject.Connect;
               lPlugObject.Run;
...
		end)
...

The PlugObject has an infinite loop inside the Run method, and sometimes it checks the mothod "Stoppable".

PlugObject is a plain object without threads or task, only a "repeat until false" loop.

I need to know if this use of anonymous can create problems with the threading concurrency in some way.

Thanks,

Eddy

Share this post


Link to post

Only testing the code yourself will show but I can smell a potential memory leak due to circular references caused by anonymous methods and capturing of the IOmniTask within itself - nested anonymous methods can be a bit nasty sometimes.

  • Thanks 1

Share this post


Link to post
4 hours ago, Stefan Glienke said:

Only testing the code yourself will show but I can smell a potential memory leak due to circular references caused by anonymous methods and capturing of the IOmniTask within itself - nested anonymous methods can be a bit nasty sometimes.

Yes, in my tests I found some strange behaviours within the omnitask and reference counting, very hard to explain and to test.

But after days of pain I found the problem, and I am pretty sure it is not related to Omni or Spring. I am writing a microservices framework with mqtt using a library with some problems with threading...

Thanks and keep up the good job

Eddy

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×