Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pause does not unregister handlers #73

Open
TobBrandt opened this issue Nov 21, 2014 · 1 comment · May be fixed by #173
Open

pause does not unregister handlers #73

TobBrandt opened this issue Nov 21, 2014 · 1 comment · May be fixed by #173

Comments

@TobBrandt
Copy link

The documentation of pause says

Pause an event network. Immediately stop producing output and unregister all event handlers for inputs.

But the code doesn't unregister anything, it just sets a flag to ignore input.

This means, that the lifetime of a network depends on the lifetime of any AddHandler it is registered with.
For example, the memory usage of the following program will grow linearly, because addHandler keeps a reference to every network:

{-# LANGUAGE RankNTypes #-}
module Main where

import Reactive.Banana
import Reactive.Banana.Frameworks
import Control.Monad
import Data.IORef

numNetworks = 800000

main = do
    (addHandler, fire) <- newAddHandler
    progRef <- newIORef 0
    printProgress 0
    forM_ [1 .. numNetworks] $ \n -> do
        oldProg <- readIORef progRef
        let newProg = round (fromIntegral n / fromIntegral numNetworks * 100)
        when (newProg > oldProg) (printProgress newProg >> writeIORef progRef newProg)
        net <- makeNetwork addHandler
        actuate net
        pause net
    getLine

makeNetwork :: AddHandler () -> IO EventNetwork
makeNetwork addHandler = do
    let
        desc :: forall t. Frameworks t => Moment t ()
        desc = do
            e <- fromAddHandler addHandler
            return ()
    compile desc

printProgress :: Int -> IO ()
printProgress n = putStrLn $ "[" ++ replicate n '.' ++ replicate (100-n) ' ' ++ "]"

Maybe having a dispose function would be a good idea? Or use a weak reference for registered handlers?

@HeinrichApfelmus
Copy link
Owner

That's a good point, thanks! I will only fix the documentation for now, but I will address the issue in a future version.

HeinrichApfelmus added a commit that referenced this issue Dec 21, 2014
Kritzefitz added a commit to Kritzefitz/reactive-banana that referenced this issue May 2, 2018
Kritzefitz added a commit to Kritzefitz/reactive-banana that referenced this issue May 2, 2018
Kritzefitz added a commit to Kritzefitz/reactive-banana that referenced this issue May 3, 2018
Kritzefitz added a commit to Kritzefitz/reactive-banana that referenced this issue May 2, 2021
Kritzefitz added a commit to Kritzefitz/reactive-banana that referenced this issue May 3, 2021
Kritzefitz added a commit to Kritzefitz/reactive-banana that referenced this issue May 6, 2021
Kritzefitz added a commit to Kritzefitz/reactive-banana that referenced this issue May 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants