{"action":"created","issue":{"url":"https://api.github.com/repos/magic-lang/rock/issues/48","repository_url":"https://api.github.com/repos/magic-lang/rock","labels_url":"https://api.github.com/repos/magic-lang/rock/issues/48/labels{/name}","comments_url":"https://api.github.com/repos/magic-lang/rock/issues/48/comments","events_url":"https://api.github.com/repos/magic-lang/rock/issues/48/events","html_url":"https://github.com/magic-lang/rock/issues/48","id":141227541,"number":48,"title":"Generic values are allocated on the stack during construction","user":{"login":"shamanas","id":265903,"avatar_url":"https://avatars.githubusercontent.com/u/265903?v=3","gravatar_id":"","url":"https://api.github.com/users/shamanas","html_url":"https://github.com/shamanas","followers_url":"https://api.github.com/users/shamanas/followers","following_url":"https://api.github.com/users/shamanas/following{/other_user}","gists_url":"https://api.github.com/users/shamanas/gists{/gist_id}","starred_url":"https://api.github.com/users/shamanas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shamanas/subscriptions","organizations_url":"https://api.github.com/users/shamanas/orgs","repos_url":"https://api.github.com/users/shamanas/repos","events_url":"https://api.github.com/users/shamanas/events{/privacy}","received_events_url":"https://api.github.com/users/shamanas/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2016-03-16T10:14:11Z","updated_at":"2016-05-12T13:26:59Z","closed_at":null,"body":"This issue is discussed in [#41](https://github.com/magic-lang/rock/pull/41)  \r\n\r\nBasically, in code like this:  \r\n\r\n```ooc\r\nFoo: class <T> {\r\n    val: T\r\n    init: func (=val)\r\n}\r\n```\r\n\r\nThe following C code is generated:  \r\n\r\n```c\r\nvoid Foo___defaults___impl(FooClass* this) {\r\n    types__Class___defaults___impl((types__Class*) this);\r\n    this->val = Memory__alloca(this->T->size);\r\n}\r\n```  \r\n\r\nThe result is that the generic value is actually a dangling pointer.  \r\nTo solve this, one can currently do:  \r\n\r\n```ooc\r\nFoo: class <T> {\r\n    val: __onheap__ T\r\n    init: func (=val)\r\n}\r\n```\r\n\r\nAnd then manually free the generic value.  \r\nAs discussed in that PR, I believe the best course of action would be to automatically assume `__onheap__` for \"naked\" (non pointer) generic values and autogenerate a free function that deletes them and calls the user defined `__destroy__` function (if a free function is not user defined)."},"comment":{"url":"https://api.github.com/repos/magic-lang/rock/issues/comments/218756395","html_url":"https://github.com/magic-lang/rock/issues/48#issuecomment-218756395","issue_url":"https://api.github.com/repos/magic-lang/rock/issues/48","id":218756395,"user":{"login":"shamanas","id":265903,"avatar_url":"https://avatars.githubusercontent.com/u/265903?v=3","gravatar_id":"","url":"https://api.github.com/users/shamanas","html_url":"https://github.com/shamanas","followers_url":"https://api.github.com/users/shamanas/followers","following_url":"https://api.github.com/users/shamanas/following{/other_user}","gists_url":"https://api.github.com/users/shamanas/gists{/gist_id}","starred_url":"https://api.github.com/users/shamanas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shamanas/subscriptions","organizations_url":"https://api.github.com/users/shamanas/orgs","repos_url":"https://api.github.com/users/shamanas/repos","events_url":"https://api.github.com/users/shamanas/events{/privacy}","received_events_url":"https://api.github.com/users/shamanas/received_events","type":"User","site_admin":false},"created_at":"2016-05-12T13:26:59Z","updated_at":"2016-05-12T13:26:59Z","body":"@marcusnaslund  \r\n\r\nSure, but generics are supposed to be baked into the language and automatically managed.  \r\nFor example, seeing the following:  \r\n\r\n```ooc\r\n\r\nMyCell: class <T> {\r\n    val: T\r\n    init: func (=val)\r\n}\r\n```\r\n\r\nI think it is reasonable to assume that the generic value will be cleaned up when calling `free`, since you never actually allocate `val` manually. "}}