Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

On "Out of Memory" error, do something else? 2

Status
Not open for further replies.

ug505

Programmer
Jan 2, 2010
52
US
Hi again,

I've never handled error messages in Delphi so it's new to me.

My program uses a lot of RAM. Is there a way to do FreeAndNil() when I receive the "Out of Memory" error? Or if not, is there a way to end the program on this error? Such as Halt() or Close()?
 
One big thing you need to know how to use is the TRY block. There's two ways you can use it...

Code:
Q:= TADOQuery.Create(nil);
try
  Q.Connection:= MyConnection;
  //Do some stuff with Q
finally
  if assigned(Q) then Q.Free;
end;

The try..finally block is intended for safe-guarding code and making sure that if anything fails in that block of code, that the code in the finally section will still be executed at the end. Any exception which is raised between TRY and FINALLY will jump down to the code between FINALLY and END.

or...

Code:
try
  //Safe code goes here
except
  //Exception handling goes here
  on e: exception do begin
    ShowMessage('An error has occured: '+e.Message);
  end;
end;

The try..except block is intended for safe-guarding code and catching if an error occurs. If any exception is raised between TRY and EXCEPT, it will jump down to the exception handler(s) between EXCEPT and END. Catch each exception type as shown above (99% of the time you would use "on e: exception do..." and e being anything you want to name it, like ERR or Except.

Some errors are hard to figure out. For example, when working with sockets (in ScktComp unit of Delphi), if an error is raised, even if you have an event handler for the error, it still raises its own internal error and displays a message regardless of any exception handler. In this case, they provide an extra ability: In the error event handler, there's a var parameter "ErrorCode". If I switch that integer to "0" in the event handler, the socket does not raise its internal error. If I ignore the ErrorCode parameter, it will raise the internal exception.

So it kinda depends on how you're doing it. If you're doing FreeAndNil, then FreeAndNil would need to go between FINALLY and END, while the initialization which required you to free will need to go just before the TRY. Any code using this variable must be safe-coded between TRY and FINALLY. Just like I declared the Q: TADOQuery above. In the FINALLY section, all I do is free that Q I created.

JD Solutions
 
Yes you usually want to test on anything. Memory isn't something most think about because of the large virtual memory that Windows has had for a while, but it's a concern if you allocate a lot of memory. That said, part of a program I have that allocates huge amounts of memory (there's other stuff, but this shows the checking necessary on the memory side of things):

Code:
  mem_alloc := true;
  try
    getmem(memptr, memsize);
  except
    on EOutOfMemory do
      mem_alloc := false;
  end;
  result := mem_alloc;

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Thanks djjd47130 that really helped me, this forum is full of great people. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top