Skip to content

[To Document][Known Issue]False Positive Memory Leaks caused by System.LoadResString (Resource String Caching) #373

@tregubovav-dev

Description

@tregubovav-dev

Description:
I'm creating this issue to document a known cause of false-positive memory leaks in DUnitX when testing code that utilizes resourcestring.

The Issue:
DUnitX reports a memory leak for any test method that triggers the first use of a specific resourcestring.

Root Cause:
The Delphi RTL function System.LoadResString(ResStringRec: PResStringRec): string maintains an internal global cache of loaded resource strings.

  1. When a resourcestring is accessed for the first time (e.g., via Exception.CreateRes or direct usage), the RTL loads it from the resource section and adds it to this global cache.
  2. This allocation persists for the lifetime of the application.
  3. Since DUnitX calculates leaks by comparing memory usage before and after a test method, this one-time cache allocation is flagged as a leak for that specific test.

Reproduction:
Any test that causes a unique resourcestring to be loaded will trigger this.

  • Example: Calling raise EOsslError.CreateRes(@resSomeError) inside a test.

Workaround / Solution:
To avoid these false positives, you must ensure the resource strings are cached before the test execution phase begins.

  • Action: Pre-load all relevant resource strings in the [SetupFixture] method of your test unit.
  • Alternative: Mark specific tests with [IgnoreMemoryLeaks], though this may hide real leaks.

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