Title: Incorrect marshalling of string in Py_SetPythonHome · Issue #179 · pythonnet/pythonnet · GitHub
Open Graph Title: Incorrect marshalling of string in Py_SetPythonHome · Issue #179 · pythonnet/pythonnet
X Title: Incorrect marshalling of string in Py_SetPythonHome · Issue #179 · pythonnet/pythonnet
Description: I noticed that when PythonEngine.PythonHome was set and a subsequent Initialize() call was made my .Net application would close unexpectedly. It looks like the call to Py_SetPythonHome made in runtime.cs is implicitly marshalling the str...
Open Graph Description: I noticed that when PythonEngine.PythonHome was set and a subsequent Initialize() call was made my .Net application would close unexpectedly. It looks like the call to Py_SetPythonHome made in runt...
X Description: I noticed that when PythonEngine.PythonHome was set and a subsequent Initialize() call was made my .Net application would close unexpectedly. It looks like the call to Py_SetPythonHome made in runt...
Opengraph URL: https://github.com/pythonnet/pythonnet/issues/179
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Incorrect marshalling of string in Py_SetPythonHome","articleBody":"I noticed that when `PythonEngine.PythonHome` was set and a subsequent `Initialize()` call was made my .Net application would close unexpectedly.\n\nIt looks like the call to `Py_SetPythonHome` made in runtime.cs is implicitly marshalling the string to a char array for the unmanaged code. My suspicion is that the Python sourcecode is keeping a record of the pointer rather than making a copy of the data. The managed runtime will free the memory allocated to char array as soon as the function finishes executing leaving a dangling pointer. This then leads to the unexpected `Initialize()` behaviour.\n\n```\n[DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl,\n ExactSpelling = true, CharSet = CharSet.Ansi)]\ninternal unsafe static extern void\nPy_SetPythonHome(string home);\n```\n\nThe solution appears to be to explicitly marshal the string and hang onto the pointer until it is no longer needed, i.e.\n\n```\n[DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl,\n ExactSpelling = true, CharSet = CharSet.Ansi)]\ninternal unsafe static extern void\nPy_SetPythonHome(IntPtr home);\n```\n\n```\nIntPtr str = Marshal.StringToHGlobalAnsi(@\"C:\\Python27\");\nPy_SetPythonHome(str);\n// do stuff\nMarshal.FreeHGlobal(str);\n```\n\nRunning Win7, .Net4.5 and Python 2.7.11.\n","author":{"url":"https://github.com/leomeuk","@type":"Person","name":"leomeuk"},"datePublished":"2016-03-08T21:09:23.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/179/pythonnet/issues/179"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:3e1eceb8-037e-db63-357d-b19e6adf2aa3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BCDA:3D45D4:6055DBC:7C28686:697668B5 |
| html-safe-nonce | 8d82f1a4281871311ad85fc1b6706171d33a42b2a12f8709b541bb3339a3e243 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQ0RBOjNENDVENDo2MDU1REJDOjdDMjg2ODY6Njk3NjY4QjUiLCJ2aXNpdG9yX2lkIjoiODU1NTc2NjY2NDM0NTgzOTc5NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | d2482d172e16b593e0f3313d66331706235a79bf4e945c19f510297fb51c1fa0 |
| hovercard-subject-tag | issue:139393797 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/pythonnet/pythonnet/179/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ef1223072c82350803730d487906dfcb4e2b0a5a6a897144ef2e04242029a74f/pythonnet/pythonnet/issues/179 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ef1223072c82350803730d487906dfcb4e2b0a5a6a897144ef2e04242029a74f/pythonnet/pythonnet/issues/179 |
| og:image:alt | I noticed that when PythonEngine.PythonHome was set and a subsequent Initialize() call was made my .Net application would close unexpectedly. It looks like the call to Py_SetPythonHome made in runt... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | leomeuk |
| hostname | github.com |
| expected-hostname | github.com |
| None | 032152924a283b83384255d9489e7b93b54ba01da8d380b05ecd3953b3212411 |
| turbo-cache-control | no-preview |
| go-import | github.com/pythonnet/pythonnet git https://github.com/pythonnet/pythonnet.git |
| octolytics-dimension-user_id | 6050430 |
| octolytics-dimension-user_login | pythonnet |
| octolytics-dimension-repository_id | 14748123 |
| octolytics-dimension-repository_nwo | pythonnet/pythonnet |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 14748123 |
| octolytics-dimension-repository_network_root_nwo | pythonnet/pythonnet |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 5b577f6be6482e336e3c30e8daefa30144947b17 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width