FreeBasic Deallocate Keyword
last modified June 16, 2025
The FreeBasic Deallocate
keyword is used to free dynamically
allocated memory. It works with pointers created using Allocate
or Callocate
. Proper memory management prevents memory leaks.
Basic Definition
Deallocate
releases memory previously allocated to a pointer.
It marks the memory as available for other uses. Failing to deallocate
memory leads to memory leaks in programs.
The keyword takes a pointer variable as its argument. After deallocation, the pointer becomes invalid and should not be used. Always set pointers to null after deallocation for safety.
Basic Deallocation
This example shows the simplest usage of the Deallocate
keyword.
Dim p As Integer Ptr p = Allocate(SizeOf(Integer)) *p = 42 Print "Value before deallocation: "; *p Deallocate(p) p = 0 ' Good practice to null the pointer ' *p would now cause an error if accessed Print "Memory has been freed"
Here we allocate memory for an integer, store a value, then deallocate it. The pointer is set to null afterward. Attempting to use the pointer after deallocation would cause undefined behavior.
Deallocating Arrays
Dynamic arrays must be properly deallocated to prevent memory leaks.
Dim arr As Integer Ptr arr = Allocate(10 * SizeOf(Integer)) For i As Integer = 0 To 9 arr[i] = i * 10 Print arr[i]; " "; Next Print Deallocate(arr) arr = 0
This code allocates memory for 10 integers, fills them with values, then frees the memory. Array-style access works with pointers in FreeBasic. Always deallocate arrays when they're no longer needed.
Deallocate in Functions
Memory allocated in functions should be properly managed to avoid leaks.
Function CreateBuffer(size As Integer) As Byte Ptr Return Allocate(size) End Function Dim buffer As Byte Ptr = CreateBuffer(100) Print "Buffer created with size 100" ' Use the buffer here... Deallocate(buffer) buffer = 0 Print "Buffer freed"
This demonstrates memory management across functions. The function allocates memory which the caller must eventually deallocate. Document clearly who owns memory and is responsible for freeing it.
Checking Before Deallocation
It's good practice to check pointers before deallocation.
Dim ptr As String Ptr = 0 ' Only allocate if needed If SomeCondition Then ptr = Allocate(100) End If ' Safely deallocate If ptr Then Deallocate(ptr) ptr = 0 End If
This example shows defensive programming with pointers. We check if the pointer is non-null before deallocation. This prevents crashes from double-free or null pointer deallocation attempts.
Deallocating Structures
Complex structures with nested allocations require careful deallocation.
Type Person name As String Ptr age As Integer Ptr End Type Dim p As Person Ptr = Allocate(SizeOf(Person)) p->name = Allocate(50) p->age = Allocate(SizeOf(Integer)) ' Use the structure... Deallocate(p->name) Deallocate(p->age) Deallocate(p) p = 0
When deallocating structures with pointer members, free the members first. Then free the structure itself. This prevents memory leaks in complex data structures with multiple allocations.
Deallocate vs Erase
Deallocate
differs from Erase
for dynamic arrays.
' Dynamic array using Redim Dim array() As Integer Redim array(0 To 9) ' Pointer-based array Dim parray As Integer Ptr = Allocate(10 * SizeOf(Integer)) ' Free them differently Erase array ' For dynamic arrays Deallocate(parray) ' For pointer arrays parray = 0
This shows the difference between deallocating pointer arrays and erasing
dynamic arrays. Erase
is for dynamic arrays created with
Dim
and Redim
, while Deallocate
is
for pointer-based allocations.
Memory Leak Detection
Proper deallocation helps prevent memory leaks in long-running programs.
Sub ProcessData() Dim data As Byte Ptr = Allocate(1024) ' ... use data ... Deallocate(data) ' Comment this to create a leak data = 0 End Sub For i As Integer = 1 To 100 ProcessData() Next Print "Memory operations complete"
This demonstrates how forgetting to deallocate causes leaks. Each call allocates memory; without deallocation, memory usage grows. Always free memory in the same scope where it's allocated when possible.
Best Practices
- Pair allocations: Always match Allocate with Deallocate.
- Null pointers: Set pointers to null after deallocation.
- Ownership: Clearly document who owns allocated memory.
- Scope: Free memory in the same scope where allocated.
- Checks: Verify pointers before deallocation.
This tutorial covered the FreeBasic Deallocate
keyword with
practical examples showing proper memory management techniques.
Author
List all FreeBasic Tutorials.