Struct Memory Leak when using DS

Answered

Comments

8 comments

  • Core Tech

    This is not a bug as data structures are not Garbage Collected, you just created an effective memory leak for yourself.

    We cannot Garbage Collect data structures due to the internal design and they are not represented by a reference, but by an index, which is just a number, so we cannot tell the lifetime of the data structure.

    Russell

    -1
    Comment actions Permalink
  • Adrian Nelson

    In my struct objects I implement a dispose() function that does any manual cleanup of internal structures like arrays or ds_xxx structures. Obviously you then have to remember to call myobject.dispose() 

    It would be nice if GML automatically looked for a specifically named function, maybe even cleanup() so you don't have to remember to call it before finishing with your object.

    0
    Comment actions Permalink
  • Paige Marincak

    @CoreTech I never said it was a bug. I wanted to know what the recommended approach for this situation was because the docs and samples did not provide anything that I could see. Is there a destructor? Is there a hidden method we can override? Do I need to make my own BaseStruct that every single struct extends from and remember to call a "delete" method on it (which is really annoying because instances will just clean up for you when deleted).

    Adrian Nelson Yeah, I'm kinda hoping you don't have to do that

    0
    Comment actions Permalink
  • Piotr Gnys

    The recommended approach is same as outside of structs, that's why there's nothing about it in manual, as all is said in data structures chapter already, and nothing has changed here.

    You need to manage and remove ds manually, to not left it in memory.

    0
    Comment actions Permalink
  • Luigi Piscopo

    I personally just have a Destroy() method where I free all these stuff! I try to focus on the architecture of the structs in a way I only have one Destroy() method to call for freeing memory (or very few of them) so I also have a sort of "manager" structs which take care of manage their kind of structs that have a Destroy() method inside... I try to keep things simple... the fact that these data structures are not garbage collected is a good thing... and you should take advantage of this by creating them at the right time and freeing them when it is needed. This also avoids performance loss due to garbage collected stuff.

    0
    Comment actions Permalink
  • Piotr Gnys

    They already said there will be no destroy method, as that causes problem with garbage collecting - from what I understand, it's because when you write "delete struct_name" it marks it to delete, unlinks, but may still keep in memory until next garbage collecting process take place - so it can be one frame or even several minutes. Also, if you don't use "delete", structure which isn't used anymore will be also deleted after some time. That means, you never know when destroy even would happen.

    However... there could be even called "onGC", which is called on removing by garbage collector, same as "clean up" even exists in objects.

    Sadly, there was lot of posts about feature like this, and YYG always says "no".

    0
    Comment actions Permalink
  • Paige Marincak

    Personally I don't particularly care when the delete actually occurs, the nice thing about GC is that you don't need to think about it. For example, with the CleanUp method on instances. So it's disappointing to hear that we'll have to have a destructor method to call; it feels like an oversight.

    In that case, I do think that the documentation could be clearer with structs for newcomers to memory management. It's honestly a difficult concept for most people, and when they're told the GC will handle everything they may also think it'll handle instances, DS, etc. when really it's only handling primitive types.

    0
    Comment actions Permalink
  • Piotr Gnys

    Yep, lot of people may think that GC works immediately, while it clears data "at some point". Also, still, as I understand that "destroy" or "delete" event would be a wrong name in this case, a "cleanup" should be a good one. Not sure why they don't want it, while for example there is undocumented "tostring" method, as helper...

    0
    Comment actions Permalink

Please sign in to leave a comment.