{"action":"opened","issue":{"number":10,"created_at":"2011-10-21T18:02:30Z","pull_request":{"diff_url":null,"patch_url":null,"html_url":null},"body":"Hello there.\r\n\r\nI've recently figured out that SBCL and .NET runtimes, while working in one process, are not particularly fond of each other, and i'm interested if there's anything anyone can do about that.\r\n\r\nConsider the following code (C++/CLI)\r\n\r\n```c++\r\n/*\r\n * set LIBPATH=%LIBPATH%%windir%\\Microsoft.NET\\Framework\\v4.0.30319\\WPF;\r\n * cl /EHac /clr /TP /LD Window.cpp\r\n */\r\n#using <System.dll>\r\n#using <WindowsBase.dll>\r\n#using <PresentationCore.dll>\r\n#using <PresentationFramework.dll>\r\n\r\n#include <vcclr.h>\r\n\r\nextern \"C\" __declspec(dllexport) int _stdcall OpenWindow()\r\n{\r\n    try\r\n    {        \r\n        gcroot<System::Windows::Window ^> window\r\n            = gcnew System::Windows::Window;\r\n        window->ShowDialog();\r\n    } catch(System::Exception ^ e)\r\n    {\r\n        System::Console::Error->WriteLine(e);\r\n        return 0;\r\n    }\r\n    return 1;\r\n}\r\n```\r\n\r\nHere's an example of usage of this function for SBCL:\r\n\r\n```lisp\r\n(eval-when (:compile-toplevel :load-toplevel :execute)\r\n  (sb-alien:load-shared-object \"ole32.dll\")\r\n  (sb-alien:load-shared-object \"Window.dll\"))\r\n\r\n(sb-alien:define-alien-routine\r\n    (\"CoInitializeEx\" %co-initialize-ex)\r\n    (sb-alien:signed 32)\r\n  (reserved sb-sys:system-area-pointer)\r\n  (mode (sb-alien:unsigned 32)))\r\n\r\n(sb-alien:define-alien-routine\r\n    (\"CoUninitialize\" %co-uninitialize)\r\n    (sb-alien:signed 32))\r\n\r\n(sb-alien:define-alien-routine\r\n    (\"_OpenWindow@0\" %open-window)\r\n    (sb-alien:signed 32))\r\n\r\n(defun make-com-thread (function &key (mode :sta))\r\n  (let ((function (lambda ()\r\n                    (%co-initialize-ex\r\n                      (sb-sys:int-sap 0)\r\n                      (ecase mode\r\n                        (:sta 2)\r\n                        (:mta 0)))\r\n                    (unwind-protect\r\n                        (funcall function)\r\n                      (%co-uninitialize)))))\r\n    (sb-thread:make-thread function)))\r\n\r\n(defun open-window (&optional new-thread)\r\n  (if new-thread\r\n    (make-com-thread\r\n        (lambda () (%open-window))\r\n      :mode :sta)\r\n    (progn (unless (zerop (%co-initialize-ex (sb-sys:int-sap 0) 2))\r\n             (error \"Unable to initialize COM STA\"))\r\n           (%open-window)\r\n           (%co-uninitialize))))\r\n\r\n```\r\n\r\nWhile this function(_OpenWindow_) works perfectly when called from either C, unmanaged C++ or .NET, when called from lisp, it causes .NET runtime(and consequently, entire lisp process) to crash with ''Process terminated due to StackOverflowException\" error message.\r\n\r\nI suppose the reason of crashes is somehow related to cooperation(or rather, contention) of lisp and .NET garbage collectors in STA environment because:\r\n\r\n* When this function is called from MTA, it causes no crashes(it catches _InvalidOperationException_, complains about invalid apartment state to stderr an quits).\r\n* When this function is modified to simply create a window, but not to show it, there is no crash until lisp garbage collector is run(until you call _sb-ext:gc_, for example)\r\n\r\nI've also tried calling this function from Clozure CL. Again, it doesn't work, but instead of simply terminating the process, CCL runtime enters low-level debugger and complains about an exception on foreign stack.\r\n\r\nAlthough resolving this issue is not a matter of urgent need, it would allow better integration of SBCL and Windows platform by opening .NET BCL and libraries for lisp.","comments":0,"title":"Lisp <--> .NET interoperability","updated_at":"2011-10-21T18:02:30Z","url":"https://api.github.com/repos/akovalenko/sbcl-win32-threads/issues/10","id":2017924,"assignee":null,"milestone":null,"closed_at":null,"user":{"gravatar_id":"24ae9e4167b505fec266cfe3479d9b03","avatar_url":"https://secure.gravatar.com/avatar/24ae9e4167b505fec266cfe3479d9b03?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","url":"https://api.github.com/users/Lovesan","id":227641,"login":"Lovesan"},"html_url":"https://github.com/akovalenko/sbcl-win32-threads/issues/10","labels":[],"state":"open"},"legacy":{"number":10,"action":"opened","issue":2017924}}