Malcolm Clark wrote:
Strangely, Rob's answer doesn't appear to be quite correct. Or am I (again) doing somethin wrong:-(
No, I think everything's right. What I said about string literals is only true _sometimes_, and I'm not sure just what the rules are.
With call to TestForLeak commented out: ODS: Start AllocMemCount: 286 ODS: Start AllocMemSize: 8900 ODS: End AllocMemCount: 287 ODS: End AllocMemSize: 8900
OK. The difference in AllocMemCount makes sense since the first time you check that value, the string result from the Format function hasn't been allocated yet. I have no idea why AllocMemSize doesn't change, though.
TestForLeak uses constant 'a' with dispose(list.Items[i]); ODS: Start AllocMemCount: 286 ODS: Start AllocMemSize: 8900 ODS: End AllocMemCount: 298 ODS: End AllocMemSize: 9032
Yep. Definitely a memory leak.
TestForLeak uses random string with dispose(list.Items[i]); ODS: Start AllocMemCount: 286 ODS: Start AllocMemSize: 8900 ODS: End AllocMemCount: 298 ODS: End AllocMemSize: 9032
Same memory leak. And that's weird since we should expect this leak to be larger than the previous one. (Rather, the previous one should be smaller than this one.) Sometimes, when assigning a string literal to a string variable, Delphi will simply copy the underlying pointer and be done with it. Other times, Delphi will make a new copy of the string. I don't remember how it chooses. As I recall, Borland knows there are cases (such as this one) when the whole string doesn't need to be copied, but there are other cases when the copy is necessary. It might have something to do with global variables or multiple threads.
TestForLeak uses random string with dispose(pnt); ODS: Start AllocMemCount: 286 ODS: Start AllocMemSize: 8900 ODS: End AllocMemCount: 287 ODS: End AllocMemSize: 8900
And the leak has been fixed. -- Rob __________________________________________________ Delphi-Talk mailing list -> [email protected] http://www.elists.org/mailman/listinfo/delphi-talk
