{"action":"opened","issue":{"url":"https://api.github.com/repos/Phreak87/LeptonicaSharp/issues/58","repository_url":"https://api.github.com/repos/Phreak87/LeptonicaSharp","labels_url":"https://api.github.com/repos/Phreak87/LeptonicaSharp/issues/58/labels{/name}","comments_url":"https://api.github.com/repos/Phreak87/LeptonicaSharp/issues/58/comments","events_url":"https://api.github.com/repos/Phreak87/LeptonicaSharp/issues/58/events","html_url":"https://github.com/Phreak87/LeptonicaSharp/issues/58","id":389932127,"node_id":"MDU6SXNzdWUzODk5MzIxMjc=","number":58,"title":"Dispose of Object with Marshalling","user":{"login":"fdncred","id":343840,"node_id":"MDQ6VXNlcjM0Mzg0MA==","avatar_url":"https://avatars2.githubusercontent.com/u/343840?v=4","gravatar_id":"","url":"https://api.github.com/users/fdncred","html_url":"https://github.com/fdncred","followers_url":"https://api.github.com/users/fdncred/followers","following_url":"https://api.github.com/users/fdncred/following{/other_user}","gists_url":"https://api.github.com/users/fdncred/gists{/gist_id}","starred_url":"https://api.github.com/users/fdncred/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fdncred/subscriptions","organizations_url":"https://api.github.com/users/fdncred/orgs","repos_url":"https://api.github.com/users/fdncred/repos","events_url":"https://api.github.com/users/fdncred/events{/privacy}","received_events_url":"https://api.github.com/users/fdncred/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-12-11T20:06:42Z","updated_at":"2018-12-11T20:06:42Z","closed_at":null,"author_association":"COLLABORATOR","body":"Generally speaking there could be a problem with the way everything is marshalled.\r\n\r\nTake this Dispose of PixColormap for example.\r\n```cs\r\npublic void Dispose()\r\n{\r\n    foreach (KeyValuePair<string, object> Entry in Caching) {\r\n        var disp = Entry.Value as IDisposable;\r\n        if (disp != null) { disp.Dispose(); }\r\n    }\r\n\r\n    Caching.Clear();\r\n    Caching = null;\r\n    Marshal.DestroyStructure(Pointer, typeof(Marshal_PixColormap));\r\n    Array.Clear(this.Array_Bytes, 0, Array_Bytes.Length);\r\n    this.Array_Bytes = null;\r\n    Array.Clear(Array_Color, 0, Array_Color.Length);\r\n    GC.Collect();\r\n    Pointer = IntPtr.Zero;\r\n    System.GC.SuppressFinalize(this);\r\n}\r\n```\r\nThe GC.Collect() is just for testing but the Array.Clear() should clear the array to all 0's. It does not because every time Array_Bytes is accessed it re-marshals everything and re-establishes the Array_Bytes.\r\nI had to rewrite Array_Bytes as this below because there is no setter so one can't set it to null.\r\n```cs\r\nprivate byte[] array_bytes;\r\npublic byte[] Array_Bytes\r\n{\r\n\tget {\r\n\t\tif ((Values == null))\r\n\t\t{ return null; }\r\n\r\n            array_bytes = new byte[(n * 4)];\r\n\t\tMarshal.Copy(array, array_bytes, 0, array_bytes.Length);\r\n\t\treturn array_bytes;\r\n\t}\r\n    set { array_bytes = value; }\r\n}\r\n```\r\nAs a comparison, debug this code and you'll see the difference.\r\n```cs\r\nvar ta = new byte[1000];\r\nfor (int i = 0; i < ta.Length; i++)\r\n{\r\n    ta[i] = (byte)(i + 100);\r\n}\r\nArray.Clear(ta, 0, ta.Length);\r\nta = null;\r\nGC.Collect();\r\n```\r\nIf there's no way to free memory this library will have many problems."}}
{"actor":{"display_login":"fdncred"}}