Page 1 of 1

OpenFileRequester and memory usage?

Posted: Sun Jul 26, 2020 10:53 am
by PartTimeCoder
Hello

So I am tracking down some memory leaks in a rather complex program as I appear to be losing 5/6mb's from the baseline when first loaded and I have tracked it back to a call to OpenFileRequester().

When I first load my program it idles at around 6.5mb's (Task manager) and when I load in a stress test project file it goes up to 30+mb's and I clear the project, free structures, arrays etc I get back down to around 12mb's, for 2 days I been pulling my hair out trying to find where I am loseing 6mb's so I did a step by step and noticed that just calling OpenFileRequester() without even loading any file causes the memory usage to jump 6mb, when I close the requester the memory usage does not fall it stays at a little over 12mb.

Is this usual and expected or an internal PB memory leak?

You do not need a complex project to recreate this, simply open task manager and use the example from the docs, select Open* or Save* and see the memory jump and not fall

Code: Select all

If OpenWindow(0, 100, 200, 300, 200, "PureBasic - Requesters example")

  If CreateMenu(0, WindowID(0))
    MenuTitle("Test")
      MenuItem(0, "Open")
      MenuItem(1, "Save")
      MenuItem(6, "Path")
      MenuBar()
      MenuItem(2, "Choose a color")
      MenuItem(3, "Choose a font")
      MenuBar()
      MenuItem(4, "Simple message")
      MenuBar()
      MenuItem(5, "Quit")
  EndIf
  
  Repeat
    Event = WaitWindowEvent()

    Select Event
    
      Case #PB_Event_Menu  ; A Menu item has been selected
      
        Select EventMenu()
        
          Case 0  ; OpenFileRequester
            File$ = OpenFileRequester("PureBasic - Open", "Pure.txt", "Text (*.txt)|*.txt;*.bat|(PureBasic (*.pb)|*.pb", 0)
            If File$+File$
              MessageRequester("Information", "Selected File: "+File$, 0);
            EndIf

          Case 1  ; SaveFileRequester
            File$ = SaveFileRequester("PureBasic - Save", "Basic.pb", "Text (*.txt)|*.txt|(PureBasic (*.pb)|*.pb", 1)
            If File$
              MessageRequester("Information", "Selected File: "+File$, 0);
            EndIf
          
          Case 2  ; ColorRequester
            Colour = ColorRequester()
            If Colour > -1
              MessageRequester("Info", "Colour choosen: Red: "+Str(Red(Colour))+", Green: "+Str(Green(Colour))+", Blue: "+Str(Blue(Colour)), 0);
            EndIf
          
          Case 3  ; FontRequester
            If FontRequester("Courier", -13, 0)
              MessageRequester("Info", "Selected font: "+SelectedFontName()+Chr(10)+"Font size: "+Str(SelectedFontSize()), 0)
            EndIf
          
          Case 4  ; MessageRequester
            MessageRequester("Information", "Simple Message"+Chr(13)+"Line 2"+Chr(13)+"Line 3", 0)
          
          Case 5  ; Quit
            Quit = 1
            
          Case 6
            Path$ = PathRequester("Choose a path...","C:\")
            If Path$
              MessageRequester("Information", "Selected Path: "+Path$, 0)
            EndIf
            
        EndSelect
      
      
      Case #PB_Event_CloseWindow  ; If the user has pressed on the close button
        Quit = 1
        
    EndSelect

  Until Quit = 1
  
EndIf

End  
Edit: Nevermind, appears to be a windows thing, same results with GetOpenFileName_(), guess I'll live with it.

Re: OpenFileRequester and memory usage?

Posted: Sun Jul 26, 2020 12:50 pm
by mk-soft
There are two other methods to reorganize the memory and thus delete more memory.

1. Purebasic uses a help string buffer internally. To reduce this buffer it helps to test this small line

Code: Select all

dummy.s = LSet("", 1)
2. The GUI of Windows also has a lot of memory. To reorganize it, you only need to minimize the window once.

So you can check even better if there is a memory leak.

3. Structures created with AllocateMemory with strings, arrays, lists, etc. are NOT release this with FreeMemory.
Better use AllocateStructure and FreeStructure here.
ID's for files and other ID's in structures are NOT automatically closed and deleted by FreeMemory or FreeStructure. To each OpenFile belongs also a CloseFile.