Re: Memory Leak

Shawn Garbett <[email protected]>
Newsgroups gmane.comp.lang.haskell.hopengl
Message-ID <[email protected]>
On Jan 27, 2009, at 1:07 PM, Shawn Garbett wrote:

> I'm pulling my hair out fixing a memory leak in an OpenGL program.  
> Don't know if this is related to OpenGL, but I've not gotten any  
> takers from IRC. I know the exact line of code that's causing it.
>

<snip>

I've gotten it down to this:

import Data.IORef
import Graphics.Rendering.OpenGL.GL.StateVar

-- THIS VERSION DOESN'T LEAK
--iter :: IORef Integer -> IO ()
--iter ref = do
--  i <- get ref
--  ref $=! ((i+1) `rem` 12)
--  iter ref
--
--main = do
--    ref <- newIORef (0::Integer)
--    iter ref

-- THIS VERSION DOESN'T LEAK
--data State = State { stuff :: IORef Integer }
--
--iter :: State -> IO ()
--iter state = do
--  i <- get $ stuff state
--  (stuff state) $=! ((i + 1) `rem` 12)
--  iter state
--
--main = do
--  ref <- newIORef (0::Integer)
--  iter State{stuff=ref}

-- LEAKS LIKE A SIEVE
data Leak = Leak {x :: Integer}
data State = State { stuff :: IORef Leak }

iter :: State -> IO ()
iter state = do
   l <- get $ stuff state
   (stuff state) $=! l{x=((x l) + 1 `rem` 12)}
   iter state

main :: IO ()
main = do
   l <- newIORef $ Leak{x=0}
   let s = State{stuff = l}
   iter s

> Shawn Garbett <shawn.p.garbett > at < vanderbilt.edu>
> Vanderbilt Cancer Biology
> 220 Pierce Ave, PRB 715AA
> Nashville, TN 37232
> Office: 615.936.1975
> Cell: 615.397.8737
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.