3 out of 4 rated this helpful - Rate this topic

LocalFree function

Applies to: desktop apps only

Frees the specified local memory object and invalidates its handle.

Note  The local functions have greater overhead and provide fewer features than other memory management functions. New applications should use the heap functions unless documentation states that a local function should be used. For more information, see Global and Local Functions.

Syntax

HLOCAL WINAPI LocalFree(
  __in  HLOCAL hMem
);

Parameters

hMem [in]

A handle to the local memory object. This handle is returned by either the LocalAlloc or LocalReAlloc function. It is not safe to free memory allocated with GlobalAlloc.

Return value

If the function succeeds, the return value is NULL.

If the function fails, the return value is equal to a handle to the local memory object. To get extended error information, call GetLastError.

Remarks

If the process tries to examine or modify the memory after it has been freed, heap corruption may occur or an access violation exception (EXCEPTION_ACCESS_VIOLATION) may be generated.

If the hMem parameter is NULL, LocalFree ignores the parameter and returns NULL.

The LocalFree function will free a locked memory object. A locked memory object has a lock count greater than zero. The LocalLock function locks a local memory object and increments the lock count by one. The LocalUnlock function unlocks it and decrements the lock count by one. To get the lock count of a local memory object, use the LocalFlags function.

If an application is running under a debug version of the system, LocalFree will issue a message that tells you that a locked object is being freed. If you are debugging the application, LocalFree will enter a breakpoint just before freeing a locked object. This allows you to verify the intended behavior, then continue execution.

Examples

For an example, see LocalAlloc.

Requirements

Minimum supported client

Windows XP

Minimum supported server

Windows Server 2003

Header

Winbase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

See also

Global and Local Functions
GlobalFree
LocalAlloc
LocalFlags
LocalLock
LocalReAlloc
LocalUnlock
Memory Management Functions

 

 

Send comments about this topic to Microsoft

Build date: 2/7/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Free Pascal 2.4.4

Free Pascal, Windows API, LocalFree
Free Pascal 2.4.4
extracted from Windows unit "Windows.ppu"

function LocalFree
  (
    hMem : LongWord
  )
  : LongWord;
  stdcall;
C# syntax
[DllImport("kernel32.dll", SetLastError=true, ExactSpelling=true)]
internal static extern IntPtr LocalFree(IntPtr hMem);
vb.net syntax
<DllImport("kernel32.dll", SetLastError:=True, ExactSpelling:=True)> _
Public Shared Function LocalFree(ByVal hMem As IntPtr) As IntPtr
End Function
Do not freeing a memory object twice

Freeing a memory object twice may cause subsequent calls to LocalAlloc to allocated memory multiple times:

Private Sub Main()
Dim xLong1 As Long, xLong2 As Long
Do
xLong1 = LocalAlloc(0&, 200&)
xLong2 = LocalAlloc(0&, 200&)
If xLong2 = xLong1 Then
Debug.Print "Received the same memory twice!"
End If
DoEvents
Call LocalFree(xLong1)
Call LocalFree(xLong1) ' Should be xLong2 to fix
Loop
End Sub