{"action":"created","issue":{"url":"https://api.github.com/repos/Slicer/Slicer/issues/435","labels_url":"https://api.github.com/repos/Slicer/Slicer/issues/435/labels{/name}","comments_url":"https://api.github.com/repos/Slicer/Slicer/issues/435/comments","events_url":"https://api.github.com/repos/Slicer/Slicer/issues/435/events","html_url":"https://github.com/Slicer/Slicer/pull/435","id":122154092,"number":435,"title":"BUG: Fixed crash when calling widgetRepresentation().self()","user":{"login":"lassoan","id":307929,"avatar_url":"https://avatars.githubusercontent.com/u/307929?v=3","gravatar_id":"","url":"https://api.github.com/users/lassoan","html_url":"https://github.com/lassoan","followers_url":"https://api.github.com/users/lassoan/followers","following_url":"https://api.github.com/users/lassoan/following{/other_user}","gists_url":"https://api.github.com/users/lassoan/gists{/gist_id}","starred_url":"https://api.github.com/users/lassoan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lassoan/subscriptions","organizations_url":"https://api.github.com/users/lassoan/orgs","repos_url":"https://api.github.com/users/lassoan/repos","events_url":"https://api.github.com/users/lassoan/events{/privacy}","received_events_url":"https://api.github.com/users/lassoan/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-12-14T23:13:41Z","updated_at":"2015-12-15T00:22:25Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/Slicer/Slicer/pulls/435","html_url":"https://github.com/Slicer/Slicer/pull/435","diff_url":"https://github.com/Slicer/Slicer/pull/435.diff","patch_url":"https://github.com/Slicer/Slicer/pull/435.patch"},"body":"How to reproduce:\r\n\r\n1. Go to endoscopy module (just an example, the behavior is the same for every scripted modules)\r\n\r\n2. Copy-paste this into the Python console:\r\n\r\n```\r\nslicer.modules.endoscopy.widgetRepresentation().self()\r\nslicer.modules.endoscopy.widgetRepresentation().self()\r\nslicer.modules.endoscopy.widgetRepresentation().self()\r\nslicer.modules.endoscopy.widgetRepresentation().self()\r\nslicer.modules.endoscopy.widgetRepresentation().self()\r\nslicer.modules.endoscopy.widgetRepresentation().self()\r\nslicer.modules.endoscopy.widgetRepresentation().self()\r\nslicer.modules.endoscopy.widgetRepresentation().self()\r\nslicer.modules.endoscopy.widgetRepresentation().self()\r\nslicer.modules.endoscopy.widgetRepresentation().self()\r\nslicer.modules.endoscopy.widgetRepresentation().self()\r\nslicer.modules.endoscopy.widgetRepresentation().self()\r\nslicer.modules.endoscopy.widgetRepresentation().self()\r\nslicer.modules.endoscopy.widgetRepresentation().self()\r\nslicer.modules.endoscopy.widgetRepresentation().self()\r\nslicer.modules.endoscopy.widgetRepresentation().self()\r\nslicer.modules.endoscopy.widgetRepresentation().self()\r\nslicer.modules.endoscopy.widgetRepresentation().self()\r\nslicer.modules.endoscopy.widgetRepresentation().self()\r\nslicer.modules.endoscopy.widgetRepresentation().self()\r\n```\r\n=> memory is corrupted now and Slicer will crash very soon (e.g., if the above code copy-pasted again or any other operation is performed)\r\n\r\nProblem:\r\nEach time pythonSelf() is used to return an reference to Python, the reference count is decreased by one (when the temporary Python object is deleted).\r\n\r\nSee how the reference count changes using this code snippet:\r\n\r\n```\r\nimport sys\r\nfor i in range(30):\r\n  w = slicer.modules.endoscopy.widgetRepresentation().self()\r\n  print(\"Reference count of widget object: \"+str(sys.getrefcount(w)))\r\n```\r\n\r\nSolution:\r\nWhen returning a reference to Python, the reference count has to be increased by one.\r\n(for example, the same is done in CTK-build\\PythonQt\\tests\\PythonQtTests.h)\r\n\r\nThis might (hopefully) also solve the frequent crash of Slicer on application exit due to double-delete of PythonQt widgets."},"comment":{"url":"https://api.github.com/repos/Slicer/Slicer/issues/comments/164602418","html_url":"https://github.com/Slicer/Slicer/pull/435#issuecomment-164602418","issue_url":"https://api.github.com/repos/Slicer/Slicer/issues/435","id":164602418,"user":{"login":"jcfr","id":219043,"avatar_url":"https://avatars.githubusercontent.com/u/219043?v=3","gravatar_id":"","url":"https://api.github.com/users/jcfr","html_url":"https://github.com/jcfr","followers_url":"https://api.github.com/users/jcfr/followers","following_url":"https://api.github.com/users/jcfr/following{/other_user}","gists_url":"https://api.github.com/users/jcfr/gists{/gist_id}","starred_url":"https://api.github.com/users/jcfr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jcfr/subscriptions","organizations_url":"https://api.github.com/users/jcfr/orgs","repos_url":"https://api.github.com/users/jcfr/repos","events_url":"https://api.github.com/users/jcfr/events{/privacy}","received_events_url":"https://api.github.com/users/jcfr/received_events","type":"User","site_admin":false},"created_at":"2015-12-15T00:22:25Z","updated_at":"2015-12-15T00:22:25Z","body":"@lassoan Thanks for the patch "}}