Best practice to avoid memory leaks

I have been battling with a memory leak that causes my desktop application to crash from time to time - usually when changing from one window to another in OSX.
I have stopped using self.close in windows and using self.hide instead. That seems to have helped.

Is there a definitive write-up on best practice? If not, I would appreciate any helpful comments forum members feel worth sharing.

Topics might include when and where to close database access, better solutions to closing windows…

Thanks in advance

take a look at beatrix article :
https://forum.xojo.com/58953-blog-article-on-how-to-get-started-with-instruments

i have a debug menu , i open a window with listbox and label and show some useful info.

  • have a look at WeakAddressOf and WeakRef.
    if this data looks good than perhaps the leak is somewhere else.
Sub Open() Handles Open
  Listbox1.RemoveAllRows
  Var lastObjectIndex As Integer = Runtime.ObjectCount - 1
  Var objectRefs As Integer = 0
  For i As Integer = 0 To lastObjectIndex
    Listbox1.AddRow(Runtime.ObjectID(i).ToString)
    Listbox1.CellValueAt(ListBox1.LastAddedRowIndex, 1) = Runtime.ObjectClass(i)
    Listbox1.CellValueAt(ListBox1.LastAddedRowIndex, 2) = Runtime.ObjectRefs(i).ToString
    objectRefs = objectRefs + Runtime.ObjectRefs(i)
  Next
  Label1.Value = Str(Runtime.MemoryUsed)+ " bytes total amount of memory used by the allocated objects " +objectRefs.ToString
  
End Sub

I have one article planned for memory leaks. In short:

  • Select Leaks instead of TimeProfiler.
  • Let Instruments run for a while.
  • Now dig into the memory leaks.

Hi Beatrix,
Thanks for your article. It helped me to work out a couple of things I have been doing wrong.
Now I just have to refactor the code…