Last modified April 20, 2026
The FreeBasic Delete keyword is used to deallocate memory that
was previously allocated with New. It is essential for proper
memory management in programs that use dynamic memory allocation.
In FreeBasic, Delete is an operator that frees memory allocated
for a single object or an array of objects. It calls the object's destructor
(if any) before releasing the memory back to the system.
Proper use of Delete prevents memory leaks in applications that
use dynamic memory allocation. It should always be paired with a corresponding
New operation.
This example shows basic usage of Delete with a single object.
Type Person name As String age As Integer End Type Dim p As Person Ptr = New Person p->name = "John Doe" p->age = 30 Print p->name; " is "; p->age; " years old" Delete p
Here we create a Person object dynamically using New.
After using the object, we free its memory with Delete. The
pointer p becomes invalid after deletion and should not be used.
Delete[] is used to deallocate memory for arrays created with New[].
Dim numbers As Integer Ptr = New Integer[5] For i As Integer = 0 To 4 numbers[i] = i * 10 Print numbers[i] Next Delete[] numbers
This code allocates an array of 5 integers, fills them with values, and then
frees the memory. Note the use of Delete[] for arrays instead
of regular Delete.
Delete automatically calls the destructor for user-defined types.
Type Resource handle As Integer Declare Destructor() End Type Destructor Resource() Print "Releasing resource with handle"; handle End Destructor Dim res As Resource Ptr = New Resource res->handle = 42 Delete res
When Delete is called on the Resource object, its
destructor is automatically invoked before memory is freed. This is useful
for cleanup operations.
It is good practice to check if a pointer is null before attempting deletion.
Dim ptr As Integer Ptr = 0 If ptr Then Delete ptr Else Print "Pointer is null, no deletion needed" End If
This example demonstrates safe deletion by first checking if the pointer is null. Deleting a null pointer is safe in FreeBasic, but explicit checks make the code's intent clearer.
This shows what happens when you forget to use Delete.
Sub AllocateMemory() Dim buffer As Byte Ptr = New Byte[1024] ' Forgot to Delete buffer End Sub AllocateMemory() Print "Memory was allocated but not freed"
Each call to AllocateMemory() leaks 1KB of memory. In real
applications, such leaks can accumulate and cause performance issues or
crashes. Always pair New with Delete.
Delete can be used with custom allocation schemes.
Type MemoryBlock size As Integer data As Byte Ptr Declare Constructor(s As Integer) Declare Destructor() End Type Constructor MemoryBlock(s As Integer) size = s data = New Byte[size] Print "Allocated"; size; "bytes" End Constructor Destructor MemoryBlock() Delete[] data Print "Freed"; size; "bytes" End Destructor Dim block As MemoryBlock Ptr = New MemoryBlock(1024) Delete block
This example shows a MemoryBlock type that manages its own
memory. The destructor handles cleanup when Delete is called.
This pattern is useful for resource management classes.
This example demonstrates why you shouldn't delete a pointer twice.
Dim value As Integer Ptr = New Integer *value = 42 Delete value ' value = 0 ' Good practice: nullify after deletion ' Uncommenting this would cause undefined behavior: ' Delete value
Deleting a pointer twice leads to undefined behavior and potential crashes. After deletion, either nullify the pointer or avoid using it again. Some systems might appear to handle double deletes, but it is never safe.
New with exactly one Delete.Delete[] for arrays allocated with New[].
This tutorial covered the FreeBasic Delete keyword with practical
examples showing proper memory deallocation techniques.
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.