ZetCode

FreeBasic Delete Keyword

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.

Basic Definition

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.

Deleting a Single Object

This example shows basic usage of Delete with a single object.

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

Deleting an Array

Delete[] is used to deallocate memory for arrays created with New[].

delete_array.bas
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 with UDT Destructors

Delete automatically calls the destructor for user-defined types.

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

Checking for Null Before Delete

It is good practice to check if a pointer is null before attempting deletion.

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

Memory Leak Example

This shows what happens when you forget to use Delete.

delete_leak.bas
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 with Custom Memory Management

Delete can be used with custom allocation schemes.

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

Double Delete Protection

This example demonstrates why you shouldn't delete a pointer twice.

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

Best Practices

  • Pairing: Always match every New with exactly one Delete.
  • Arrays: Use Delete[] for arrays allocated with New[].
  • Null checks: Check for null before deletion when appropriate.
  • Ownership: Clearly document which code owns and must delete pointers.
  • RAII: Consider using RAII patterns to automate memory management.

This tutorial covered the FreeBasic Delete keyword with practical examples showing proper memory deallocation 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.

AltStyle によって変換されたページ (->オリジナル) /