Skip to content
This repository was archived by the owner on Feb 12, 2025. It is now read-only.
This repository was archived by the owner on Feb 12, 2025. It is now read-only.

GetLastErrorAsString leaks memory #52

@sredna

Description

@sredna

When using the FORMAT_MESSAGE_ALLOCATE_BUFFER flag you are supposed to LocalFree the returned string at some point.

If you are single-threaded you can do something like

LPSTR GetLastErrorAsString()
{
  UINT err = GetLastError();
  static LPSTR str = NULL;
  if (str) LocalFree(str), str = NULL;
  FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|..., err, ..., (LPSTR) &str, ...);
  return str;
}

This will leak one string in total instead of one for each call.

If you are multi-threaded you might want to use TLS or add the strings to a list you free later...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions