Re: D2005 Code Folding



Kostas Terzides napsal(a):
Well, actually there is a difference. Consider, this buggy function:

Function BuildAStringList:TStringList;
begin
  result:=TStringList.Create;
  result.Add('blah');
  If BooleanBlah then
    Raise Exception.Create('Exception');
end;

and if you would call this as usual:
  AStringList:=BuildAStringList;
  try
    .....
  finally
    AStringLst.Free;
  end;

then you'd have a memory leak. Putting a critical function before try means that we /trust/ this function to properly handle any exception after making any memory allocation (or other important stuff) that means we /expect/ it to behave like this:

Kostas, I think that this is completely different story. When you have a function that creates an instance of object, then it is reasonable to take a care of any exception that function could raise and free the newly created object. The difference here is that we are not interested in catching *any* exceptions that a call to ECS might throw (we don't need to free/release anything here at this point), but instead, after ECS was *successfully* called and therefore critical section acquired, we need to ensure that the corresponding LCS gets called so we have to protect the code between these two calls that could raise an exception.


--
Ivo Bauer
Software Developer
OZM Research, s.r.o.

________________________________________________

ModLink - MODBUS Messaging Components for Delphi
http://www.ozm.cz/ivobauer/modlink/
________________________________________________
.



Relevant Pages

  • Re: D2005 Code Folding
    ... Putting the ECS call *in*front*of* try..finally block will behave much the same as this code snippet and additionally it will eliminate the need for your Boolean variable. ... then you'd have a memory leak. ... Putting a critical function before try means that we /trust/ this function to properly handle any exception after making any memory allocation that means ...
    (borland.public.delphi.non-technical)
  • hunting down a memory leak
    ... the two most likely causes are either a memory leak in the ... ..NET exception classes. ... using (SqlConnection oConn = new SqlConnection(sConnection)) ... SqlCommand oCommand = oXMLCommand.CreateSqlCommand; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Is this good style of C++?
    ... Ioannis Vranos wrote: ... >> If I implements RAII strictly, do you mean that memory leak can be ... >> normal running or exception thrown, objects in the scope will be ... >> not be released by releasing the pointer. ...
    (comp.lang.cpp)
  • Re: many exceptions cause memory leak?
    ... This function allocates an Exception Occurrence from the heap and copies the data from Source into this new occurrence. ... Failure to deallocate the results of the Save_Occurrence function would certainly result in a memory leak. ... Final Water Mark: ...
    (comp.lang.ada)
  • Re: Western Digital MyBook versus Airport Extreme
    ... Chris Ridd wrote: ... to be okay, with the exception perhaps having a memory leak. ...
    (uk.comp.sys.mac)