Possible to force Delphi threadvar Memory to be Freed?

后端 未结 3 460
生来不讨喜
生来不讨喜 2021-01-17 12:01

I have been chasing down what appears to be a memory leak in a DLL built in Delphi 2007 for Win32. The memory for the threadvar variables is not freed if the threads still e

3条回答
  •  长发绾君心
    2021-01-17 12:51

    Note that it is clearly specified in the Help that you have to take care of freeing yourself your threadvars.
    You should do so as soon as you know you won't need them anymore.

    From Help:

    Dynamic variables that are ordinarily managed by the compiler (long strings, wide strings, dynamic arrays, variants, and interfaces) can be declared with threadvar, but the compiler does not automatically free the heap-allocated memory created by each thread of execution. If you use these data types in thread variables, it is your responsibility to dispose of their memory from within the thread, before the thread terminates. For example,

    threadvar S: AnsiString;
    S := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
      ...
    S := '';  // free the memory used by S
    

    Note: Use of such constructs is discouraged.
    You can free a variant by setting it to Unassigned and an interface or dynamic array by setting it to nil.

提交回复
热议问题