I tried to test for memory leaks of the high level GTK3 bindings.
    
    
    import gtk, glib, gobject
    
    proc initWithArgv*() =
      var
        cmdLine{.importc.}: cstringArray
        cmdCount{.importc.}: cint
      gtk.gtk_init(cmdCount, cmdLine)
    
    proc xadd*(self: Container; widget: Widget) =
      discard
    
    proc main() =
      initWithArgv()
      var window: Window = newWindow()
      var
        box = newBox(Orientation.vertical, 0)
        button2 = newButton("Wrapper")
      window.add(box)
      #box.xadd(button2)
      window.showAll
      button2 = nil
      GC_fullCollect()
      gtk.gtkMain()
    
    main()
    
    

The surprising observation is: With line #box.xadd(button2) commented out I get 
message "finalizeGObject" immediately after program startup as expected. 
Expected because newButton() calls new() with a finalizer proc, and when I set 
the button2 ref to nil and call GC_fullCollect() I would expect that finalizer 
to be called. But when I uncomment #box.xadd(button2), so that call is 
performed, then the finalizer is not called! Diff for the c code is
    
    
    diff /tmp/home/stefan/nimgir/nim_gi/t1.c t1.c
    201c201,203
    <   WidgetcolonObjectType__lBhQvK9csISM5sR8bgaX2Yw* T3_;
    ---
    >   ContainercolonObjectType__U9bNkp3ITqibAfZmdNgz6Fg* T3_;
    >   WidgetcolonObjectType__lBhQvK9csISM5sR8bgaX2Yw* T4_;
    >   WidgetcolonObjectType__lBhQvK9csISM5sR8bgaX2Yw* T5_;
    216a219,224
    >   nimln_(20, "t1.nim");
    >   T3_ = (ContainercolonObjectType__U9bNkp3ITqibAfZmdNgz6Fg*)0;
    >   T3_ = &box->Sup;
    >   T4_ = (WidgetcolonObjectType__lBhQvK9csISM5sR8bgaX2Yw*)0;
    >   T4_ = &button2->Sup.Sup.Sup;
    >   xadd_CSJDajTMVqA4FK5tE4Bzgg(T3_, T4_);
    218,220c226,228
    <   T3_ = (WidgetcolonObjectType__lBhQvK9csISM5sR8bgaX2Yw*)0;
    <   T3_ = &window->Sup.Sup.Sup;
    <   showAll_lXhLwQ78AtCsaeIEOagh9bA(T3_);
    ---
    >   T5_ = (WidgetcolonObjectType__lBhQvK9csISM5sR8bgaX2Yw*)0;
    >   T5_ = &window->Sup.Sup.Sup;
    >   showAll_lXhLwQ78AtCsaeIEOagh9bA(T5_);
    
    

Is that the desired behaviour? I don't know how GC is performed in detail, may 
the temporary variables in the C code prevent GC?

Reply via email to