Saturday, February 25, 2012

What is the difference between free and nil?

Free -  Deallocate the memory previously allocated for the object
nil - Dereference the object

When an object is created like

Query1 := TQuery.Create(nil)

It creates the instance and allocate some memory space to hold the data of Query1

When Free is called - It frees the memory of space already allocated for Query1
When nil is called - It sets the pointer to  nil

Always try to use FreeAndNil instead of Free And Nil separately