<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>Am 22.10.2011 um 09:08 schrieb Frank Church:</div><br class="Apple-interchange-newline"><blockquote type="cite"><br><br><div class="gmail_quote">On 22 October 2011 07:20, Felipe Monteiro de Carvalho <span dir="ltr"><<a href="mailto:felipemonteiro.carvalho@gmail.com">felipemonteiro.carvalho@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
I understand Assigned as being the same as <> nil, so Assigned(Object)<br>
= Object <> nil<br>
<br>
I vaguely remember that it could be safer in some corner case, but I<br>
don't remember ever meting that.<br>
<br>
Free is how you release the memory allocated for a object. Free calls<br>
Destroy. Never call Destroy manually. When you implement the<br>
destructor you always implement Destroy, never change Free.<br>
<br>
Nil is not a routine, it is a value, it means that the object is<br>
empty, it does not exist / is not allocated. Nil in existing<br>
implementations that I know is represented by the value zero.<br>
<br>
The typical life-cycle of a object is:<br>
<br>
MyObject := TMyObject.Create;<br>
try<br>
MyObject.DoSomething();<br>
finally<br>
MyObject.Free;<br>
end;<br>
<br>
To implement this object you should implement Create, DoSomething and Destroy.<br>
<font color="#888888"><br>
--<br>
Felipe Monteiro de Carvalho<br>
_______________________________________________<br>
fpc-pascal maillist - <a href="mailto:fpc-pascal@lists.freepascal.org">fpc-pascal@lists.freepascal.org</a><br>
<a href="http://lists.freepascal.org/mailman/listinfo/fpc-pascal" target="_blank">http://lists.freepascal.org/mailman/listinfo/fpc-pascal</a><br>
</font></blockquote></div><br>This code is the source of my woes. SCStrings and FBreakStrings are part of an object. They are repeatedly used in a loop and I need to free the memory after the loop runs or free the space taken up by their strings<br>
<br><blockquote style="margin-top: 0pt; margin-right: 0pt; margin-bottom: 0pt; margin-left: 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex; position: static; z-index: auto; " class="gmail_quote">type<br> TRuntimeMonitor = class(TThread)<br> private<br> Frequency: Integer;<br> IniFile: TMemIniFile;<br>
SCStrings: TStringList;<br> FBreakStrings: TStringList;<br> procedure DispatchOutput;<br> procedure DisplayRawOutput;<br> protected<br> procedure Execute; override;<br> public<br> constructor Create(CreateSuspended: Boolean);<br>
end;<br></blockquote><br><blockquote style="margin-top: 0pt; margin-right: 0pt; margin-bottom: 0pt; margin-left: 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex; position: static; z-index: auto; " class="gmail_quote"><b> </b> procedure InitVars;<br> begin<br> if not Assigned(SCStrings) then<br>
SCStrings := TStringList.Create;<br> if not Assigned(FBreakStrings) then<br> FBreakStrings := TStringList.Create;<br><br> IniFile := TMemIniFile.Create('zxtyu');<br> end;<br> <br><br> procedure FreeVars;<br>
begin<br> IniFile.Free;<br> if Assigned(SCStrings) then<br> SCStrings.Free;<br> if Assigned(FBreakStrings) then<br> FBreakStrings.Free;<br> if Assigned(FBreakStrings) then<br> debugln('FBreakStrings is still assigned');<br>
<br> end;<br></blockquote><b><br><br></b>InitVars and FreeVars run in the Execute procedure of the thread<b><br><br clear="all"></b>When the loop runs again Assigned in InitVars is false so as soon as those FBreakStrings and SCStrings are accessed within the loop a SIGSEGV occurs. So what I want to know is whether Assigned remains true when Free is executed.<br>
<br>In the mean time instead of using Free in FreeVars I will set the text property to the blank string to release the memory used by the strings.<br></blockquote><div><br></div><div>In TRuntimeMonitor.Create() you should add</div><div><br></div><div>SCStrings := nil;<br>FBreakStrings := nil;</div><div><br></div><div>and in FreeVars() you should replace</div><div><br></div><div> if Assigned(SCStrings) then<br> SCStrings.Free;<br> if Assigned(FBreakStrings) then<br> FBreakStrings.Free;<br></div><div><br></div><div>by</div><div><br></div><div>FreeAndNil(SCStrings);</div><div>FreeAndNil(FBreakStrings);</div><div><br></div><div>You should also add a Destroy() with the FreeAndNil() lines to ensure that everything will be destroyed when freeing TRuntimeMonitor even if FreeVars() were not called or didn't finish 100% for what ever reason.</div><div><br></div><div>FreeAndNil() is one important function that was missing in your list. ;-)</div><div>FreeAndNil(Object) does</div><div>if Assigned(Object) then begin<br> Object.Free;</div><div> Object := nil;</div><div>end;</div><div><br></div><div>For locale objects Felipes example should be used.</div><div><br></div><div>Regards</div><div><br></div><div>Michael</div></div></body></html>