Haskell DLL (using FFI) causes memory leak.

Ольга Филиппская olga.kenobi at gmail.com
Mon Aug 27 14:44:54 UTC 2018


Hello.

*Summary: *memory is consumed without releasing when a Haskell DLL (that
uses FFI) is loaded and unloaded in an endless loop.

*Details*: I'm working on a C++ project relying on a DLL that uses Haskell
code. The DLL was built with GHC 8.0.1 x64, OS is Windows 7. (Newer
versions of GHC - 8.2.1 and later - do not allow you to build such DLLs due
to some bugs: ticket #1 <https://ghc.haskell.org/trac/ghc/ticket/14472#no2>
, ticker #2 <https://ghc.haskell.org/trac/ghc/ticket/14784#no1>). The C++
project was built with MSVC compiler 2015.

We noticed that memory is not released when the library is unloaded. I've
constructed a baseline example to reproduce the bug. It has three files:
the first one is *HaskellSources.hs*(see attachments). It contains one
foreign export function foo. This foreign export function is necessary to
reproduce the bug. The second file is *CWrapper.cpp*. I intentionally
didn't include any Haskell function export because they make no difference
for this example. The last file is the code of the main program (see
*main.cpp*) that loads the library and unloads it at once in an endless
loop.

There are two cases: whether HaskellExports.o is included into the library
or not.
1. HaskellExports.o is not included into the library (see attached build
script* build_no_hs.sh*). Everything works just fine (i.e. amount of memory
consumed by the app is the same before DLL loading and right after
unloading.)
2. HaskellExports.o is included into the library (see *build_w_hs.sh*).
Memory is not released after unloading the DLL, after repeated load/unload
cycles memory consumption keeps growing until finally the application
crashes.

Is this a known problem? Is it tracked in your bugtracker? (Or maybe it is
not a problem at all and I'm doing it wrong).

Thank you for your time.

-- 
*Olga Philippskaya.*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20180827/4f732dc4/attachment.html>
-------------- next part --------------
#define DLLExport extern "C" __declspec(dllexport)

DLLExport void* c_smth (const int num)
{
    return 0;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: build_w_hs.sh
Type: text/x-sh
Size: 121 bytes
Desc: not available
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20180827/4f732dc4/attachment.sh>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: build_no_hs.sh
Type: text/x-sh
Size: 104 bytes
Desc: not available
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20180827/4f732dc4/attachment-0001.sh>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: HaskellExports.hs
Type: application/octet-stream
Size: 223 bytes
Desc: not available
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20180827/4f732dc4/attachment.obj>
-------------- next part --------------
#include <Windows.h>

int main()
{
	for (;;)
	{
		HINSTANCE module = ::LoadLibrary(L"HaskellExports.dll");
		::FreeLibrary(module);
	}
	return 0;
}


More information about the ghc-devs mailing list