![]() |
| ||
| Delphi SetLength dilemma Hello, does the SetLength function in Delphi called with the second argument 0 free the memory used for the array? Example: varand if this doesn't work, how can I free memory for array... Thanks |
| ||
| Re: Delphi SetLength dilemma All objects in Delphi, including 'string', are allocated on the heap. When you resize a dynamic array the unused objects are automatically destructed for built-in types only -- so in this case, yes. When you set the length of a dynamic array to zero the dynamic array object itself is also freed and its value becomes nil. However, deallocating an object does not necessarily mean that Delphi returns memory to the system. Hope this helps. |
| ||
| Re: Delphi SetLength dilemma So if I want be sure with deallocating memory, should I use something like FreeMem(Names) ? |
| ||
| Re: Delphi SetLength dilemma No. That will cause a system exception. You have very little control over exactly how much memory is used by your application. I believe there are compiler directives to specify the maximum heap size, but I've never had occasion to use them under Win32. |
| ||
| Re: Delphi SetLength dilemma I tried to call FreeMem(Names) after I set it with SetLength(Names,0), no exception. I think that after I set length of array to zero, system will automaticaly frees the memory used for this array. |
| ||
| Re: Delphi SetLength dilemma No exception because after SetLength(0) the variable is already nil, and FreeMem has a built-in safeguard against freeing nil pointers. |
| All times are GMT -4. The time now is 7:36 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC