KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Always deallocate memory from a programmatically created list
PRODUCT: 4D | VERSION: 17 | PLATFORM: Mac & Win
Published On: November 15, 2018

Programatically creating a list can be very useful way to load large amount of data into a hierarchical list without statically creating items one at a time using the built-in Toolbox editor. However, one caveat to be aware of is that programatically created list will not automatically deallocate memory upon exiting the form. This behavior can be dangerous as a memory leak can be unintentionally created as the form containing the hierachical list is opened repeatedly. Take for example an output form with an input form containing a hierarchical list without deallocating list memory. The form method of the input form is provided below.

// Input form method example with memory leak
Case of
   : (Form event=On Load)
      C_LONGINT(peopleList)
      C_OBJECT($peopleSelection;$person)
  
      peopleList:=New list
      $peopleSelection:=ds.People.all()
  
      For each ($person;$peopleSelection)
         APPEND TO LIST(peopleList;$person.firstName;$person.ID)
      End for each
End case


This example can be dangerous as every time a record is accessed or modified, the input form is opened and memory is created for the hierarchical list. With this implementation, the app may eventually crash due to lack of free memory. To prevent this scenario, simply use the CLEAR LIST command to deallocate the list memory. The wildcard parameter * can also be used to clear any sublists. In this example, simply add it in the On Unload event of the input form method.

// Input form method with CLEAR LIST
Case of
   : (Form event=On Load)
      C_LONGINT(peopleList)
      C_OBJECT($peopleSelection;$person)
  
      peopleList:=New list
      $peopleSelection:=ds.People.all()
  
      For each ($person;$peopleSelection)
         APPEND TO LIST(peopleList;$person.firstName;$person.ID)
      End for each
  
   // deallocate list memory when form is unloaded
   : (Form event=On Unload)
      CLEAR LIST(peopleList;*)
  
End case