{"issue":{"title":"Perfect the callback system (callback lifetime, callback removal)","milestone":null,"user":{"id":99166,"login":"Burgestrand","url":"https://api.github.com/users/Burgestrand","avatar_url":"https://secure.gravatar.com/avatar/b67b73b5b1fd84119ec788b1c3df02ad?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"b67b73b5b1fd84119ec788b1c3df02ad"},"created_at":"2011-12-15T22:32:12Z","labels":[{"name":"Investigate","url":"https://api.github.com/repos/Burgestrand/Hallon/labels/Investigate","color":"640394"},{"name":"Refactor","url":"https://api.github.com/repos/Burgestrand/Hallon/labels/Refactor","color":"e102d8"}],"state":"open","updated_at":"2011-12-16T15:31:48Z","id":2573550,"html_url":"https://github.com/Burgestrand/Hallon/issues/61","pull_request":{"patch_url":null,"html_url":null,"diff_url":null},"closed_at":null,"body":"As of currently, callbacks are attached to their associated pointer. There is this complicated mechanism with ObjectSpace.define_finalizer for the callback structs, but it’s not really needed now as the callbacks can only be garbage collected once their pointer is garbage collected anyway (which means that the callback structs are also removed automatically).\r\n\r\nThere’s a big issue here with callbacks due to the nature of how asynchronous callbacks from non-ruby threads must be handled ([also see one of my blog posts on how it’s done](http://burgestrand.se/articles/asynchronous-callbacks-in-ruby-c-extensions.html)). I have some documentation of this inside `spotify.rb` as well.\r\n\r\nThe issue is this:\r\n\r\n- (thread #1) creating image pointer\r\n- (thread #1) adding callbacks to image pointer\r\n- (thread #3) background thread decides image is loaded, calls callback\r\n- (thread #3) ffi receives callback, waits to get ruby GIL\r\n- (thread #1) removes callbacks from image\r\n- (thread #1) garbage collects image, libspotify removes image from memory\r\n- (thread #3) ffi receives GIL, calls callback over to ruby\r\n- (thread #2) receives image loaded callback (with invalid image pointer, as it is garbage collected)\r\n- (thread #2) tries to call image_loaded_callback (that is now GC’d), ruby crashes — *… now, assuming image_loaded_callback does exist (even though it wont) and ruby does not crash:*\r\n- (thread #2) image_loaded_callback returns\r\n- (thread #3) ffi returns callback return value\r\n- (thread #3) image loaded callback returns in spotify background thread… WHAT NOW? image is gone anyway!\r\n\r\nEssentially, for any callback that’s called asynchronously (and not through, say, session_process_events), *ANY* registered callback proc must *NEVER* be garbage collected. Ever. Because you could always have a callback in the background thread just waiting to execute once the main thread runs out of work to do, and then I’m royally fucked.\r\n\r\nNow, I have a theory. The libspotify documentation has few functions (4? all related to audio) that’s explicitly documented to be called from “an internal callback thread”. If these are the only ones, the current approach is possibly good enough if I make the 4 asynchronous callbacks constant and non-changing. Not too bad a compromise.\r\n\r\nIf not, there’s a chance Hallon will crash with strange memory errors of calling call or trigger on Ruby::VM or pretty much any object that’s unlucky enough to end up in the spot the now garbage-collected callback proc used to reside in. I‘ll leave this issue open for a while.\r\n\r\nSee #60 and ed0016aa77b3f54c8c01cd690b512fd4f3aba06d — both are very relevant.\r\n\r\n(PS: one could use constant callbacks for everything, and use some sort of finalizer logic to call the callback on the proper objects from the callback [assuming the objects register themselves as interested], but that’s for another day)","number":61,"url":"https://api.github.com/repos/Burgestrand/Hallon/issues/61","comments":2,"assignee":null},"comment":{"user":{"id":99166,"login":"Burgestrand","url":"https://api.github.com/users/Burgestrand","avatar_url":"https://secure.gravatar.com/avatar/b67b73b5b1fd84119ec788b1c3df02ad?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"b67b73b5b1fd84119ec788b1c3df02ad"},"created_at":"2011-12-16T15:31:48Z","updated_at":"2011-12-16T15:31:48Z","id":3178489,"body":"Grr. The reasons callbacks work now is that they’re never garbage collected. Meh! THIS AINT OVER!","url":"https://api.github.com/repos/Burgestrand/Hallon/issues/comments/3178489"},"action":"created"}