ZetCode

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.

deallocate_basic.bas
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.

deallocate_array.bas
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.

deallocate_function.bas
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.

deallocate_check.bas
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.

deallocate_structure.bas
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.

deallocate_vs_erase.bas
' 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.

deallocate_leak.bas
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

This tutorial covered the FreeBasic Deallocate keyword with practical examples showing proper memory management techniques.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.