{"action":"closed","issue":{"url":"https://api.github.com/repos/llvm-hs/llvm-hs/issues/42","repository_url":"https://api.github.com/repos/llvm-hs/llvm-hs","labels_url":"https://api.github.com/repos/llvm-hs/llvm-hs/issues/42/labels{/name}","comments_url":"https://api.github.com/repos/llvm-hs/llvm-hs/issues/42/comments","events_url":"https://api.github.com/repos/llvm-hs/llvm-hs/issues/42/events","html_url":"https://github.com/llvm-hs/llvm-hs/issues/42","id":214575451,"number":42,"title":"malloc/free error in 'writeObjectToFile'","user":{"login":"tmcdonell","id":728113,"avatar_url":"https://avatars3.githubusercontent.com/u/728113?v=3","gravatar_id":"","url":"https://api.github.com/users/tmcdonell","html_url":"https://github.com/tmcdonell","followers_url":"https://api.github.com/users/tmcdonell/followers","following_url":"https://api.github.com/users/tmcdonell/following{/other_user}","gists_url":"https://api.github.com/users/tmcdonell/gists{/gist_id}","starred_url":"https://api.github.com/users/tmcdonell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tmcdonell/subscriptions","organizations_url":"https://api.github.com/users/tmcdonell/orgs","repos_url":"https://api.github.com/users/tmcdonell/repos","events_url":"https://api.github.com/users/tmcdonell/events{/privacy}","received_events_url":"https://api.github.com/users/tmcdonell/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2017-03-16T01:41:54Z","updated_at":"2017-03-17T12:11:18Z","closed_at":"2017-03-17T12:11:18Z","body":"I get the following error when using `writeObjectToFile`:\r\n```\r\nghc(78432,0x700000187000) malloc: *** error for object 0x70000017ec31: pointer being freed was not allocated\r\n*** set a breakpoint in malloc_error_break to debug\r\n```\r\nThe byte string version `moduleObject` works fine though.\r\n\r\nHere is a test program:\r\n\r\n```haskell\r\n{-# LANGUAGE FlexibleInstances    #-}\r\n{-# LANGUAGE TypeSynonymInstances #-}\r\n\r\nmodule Main where\r\n\r\nimport LLVM.Context\r\nimport LLVM.Diagnostic\r\nimport LLVM.Module\r\nimport LLVM.Target\r\n\r\nimport Control.Exception\r\nimport Control.Monad\r\nimport Control.Monad.Trans.Except\r\nimport System.Directory\r\nimport System.IO\r\nimport qualified Data.ByteString as B\r\n\r\n\r\nll :: String\r\nll = unlines\r\n  [ \"define i32 @main(i32 %argc, i8** %argv) {\"\r\n  , \"  ret i32 0\"\r\n  , \"}\"\r\n  ]\r\n\r\nmain :: IO ()\r\nmain =\r\n  print =<< test\r\n\r\ntest :: IO Bool\r\ntest =\r\n  withContext $ \\context ->\r\n  withTempFile \"foo\" $ \\obj_file ->\r\n  failInIO $ withHostTargetMachine $ \\machine ->\r\n  failInIO $ withModuleFromLLVMAssembly context ll $ \\mdl -> do\r\n    obj   <- failInIO $ moduleObject machine mdl\r\n\r\n    -- ghc(78432,0x700000187000) malloc: *** error for object 0x70000017ec31: pointer being freed was not allocated\r\n    -- *** set a breakpoint in malloc_error_break to debug\r\n    _     <- failInIO $ writeObjectToFile machine (File obj_file) mdl\r\n    obj'  <- B.readFile obj_file\r\n\r\n    return (obj == obj')\r\n\r\n\r\nwithTempFile :: FilePath -> (FilePath -> IO a) -> IO a\r\nwithTempFile template action = do\r\n  tmp_dir <- getTemporaryDirectory\r\n  bracket\r\n    (do (f,h) <- openBinaryTempFile tmp_dir template\r\n        hClose h  -- we only want a unique temporary filename\r\n        return f)\r\n    removeFile\r\n    action\r\n\r\nfailInIO :: FailInIO f => ExceptT f IO a -> IO a\r\nfailInIO = either (fail . errorToString) return <=< runExceptT\r\n\r\nclass FailInIO f where\r\n  errorToString :: f -> String\r\n\r\ninstance FailInIO String where\r\n  errorToString = id\r\n\r\ninstance FailInIO (Either String Diagnostic) where\r\n  errorToString = either id diagnosticDisplay\r\n```\r\n\r\nFWIW this works in `llvm-general-3.5`, but not in the 3.8 or 3.9 branches."}}
{"actor":{"display_login":"cocreature"}}