Solved

Handle an exception?

Posted on 2004-10-16
24
288 Views
Last Modified: 2010-04-05
I use Delphi 4 Professional and around certain blocks of code that i know can cause an exception (mainly user input) i use TRY..EXCEPT blocks.

However, i downloaded the latest version of MemProof and ran my program through it while i tested it out.  It shows the exceptions happening in the "Resource Counters" section as i put invalid information in to test out the program.  The problem is that when i then close the program MemProof shows that the exceptions are still using resources (it works out 3 bytes per exception i think).

Example of a TRY...EXCEPT block

Try
    Result:= False
   
   {Code that if all goes well will set Result to True}

Except
   // Set result to False to show failure
   Result := False;

End

Anyone know why this is and how to reclaim the lost resources?
0
Comment
Question by:mi6agent
  • 12
  • 10
  • 2
24 Comments
 
LVL 12

Assisted Solution

by:Ivanov_G
Ivanov_G earned 100 total points
ID: 12328623
I don't know about MemProof, but I will make some remarks on your code:

Result := False; // thus you avoid compiler warning "The Values might not be initialized" or smth like that...
try
   {Code that if all goes well will set Result to True}

except
   // the value of Result is already set before TRY block
   on E: Exception do
      begin  
        MessageDlg(E.Message, mtError, [mbOK], 0); // show the exception message
        Exit;  // exit from the routine
      end;
end;
0
 

Author Comment

by:mi6agent
ID: 12328866
oops, my mistake - i always use Result:= False  before the Try

I tried

except
    on E: Exception do
      begin  
        MessageDlg(E.Message, mtError, [mbOK], 0); // show the exception message
       Exit;  // exit from the routine
     end;

but seems Memproof claims resources still being used - even though no major resources are used in any of the code, it is always just the exception that seems to not release some bytes (according to Memproof of course).

I'm just interested in why an exception - that i thought is handled - uses resources.  ie: does an exception use resources that must be freed?



0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12329441
function  DangerousFunc(May be some parameters): Boolean;
begin
  Result:= True;
  try
     {Dangerous code that is possible to cause an exception}
  except
     Result := False;
     MessageDlg(E.Message, mtError, [mbOK], 0);
    // There is nothing to free. That is a usual way for handle a possible exception
  end;
end;
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:mi6agent
ID: 12330890
Thing is, i don't use MessageDlg(E.Message, mtError, [mbOK], 0); as it is a function which will return true or false.

Could that cause the problem (not displaying the message)?
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12331296
It could not be the problem. It is your decision to display or not a message. There is absolutely correct to be as following:

function  DangerousFunc(May be some parameters): Boolean;
begin
  Result:= True;
  try
     {Dangerous code that is possible to cause an exception}
  except
     Result := False;
     // There is nothing to free. That is a usual way for handle a possible exception
  end;
end;

If there occurs an exception the function will return False, otherwise it will return True.
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12331552
{Code that if all goes well will set Result to True}  ???

Do you create some object here? Can you paste some code?
0
 

Author Comment

by:mi6agent
ID: 12332114
It happens in any try..except block.  Even those that do not create an object - ie: i have a try..except block around testing for a numeric value and it too will cause the problem.


{I know i can use other ways to test for numeric values - this is just an example to show the problem of  the try..except block}

ie:

result:=false
try
  tmp := strtoint(edit1.text);
  if tmp >100 then result := true;
except
  result:=false
end;
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12332478
it is possible to avoid exception at all:

function  IsNumeric(V: Variant): Boolean;
var
  I:      Integer;
  Code:   Integer;
begin
  Result := False;
  try
    Val(V, I, Code);
    Result := (Code=0);
  except
    Result := False;
  end;
end;
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12332505
function  IsNumeric(V: Variant): Boolean;
var
  I:      Integer;
  Code:   Integer;
begin
  Val(V, I, Code);
  Result := (Code=0);
end;
0
 

Author Comment

by:mi6agent
ID: 12332894
thanks esoftbg but as i said, the example code was just to show the type of exception - ie: no objects created within the try..except - which is why i am wondering why memproof is showing resources of 3 bytes per exception.

btw - memproof is available free from http://www.automatedqa.com/downloads/memproof.asp if you would like to see for yourself the problem about try..except blocks.
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12333315
I just downloaded memproof for Delphi 7, but I don't know how to use it, it is without help files ....
0
 

Author Comment

by:mi6agent
ID: 12333364
Just unzip it, then double click the memproof exe - use file\open to load in the program you want to monitor.

Select Run and Memproof will run your program and show you all of the resources it uses.  When you close your program Memproof will show you all of the resources that your program left "open" - (ie: things that should be fixed)

0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12333394
O.k. I understand now that MemProof will start the .exe created by Delphi. (I did expect that will monitor an application started under Delphi IDE). I will test an exe with exception just now ....
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12333432
And after closing the app, how can I know is all resources freed ?
0
 

Author Comment

by:mi6agent
ID: 12335009
Click on the "Resource Counters" icon on the left panel - it will list all the resources used and freed by the program
0
 

Author Comment

by:mi6agent
ID: 12335021
"Resource Details" icon will give you more information about each of the "problem" areas such as non-freed objects, etc
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12336206
Next is the result after closing .exe (created by Delphi 7) without exception // StrToInt('128');
      303  Virtual Memory        02190000       4096  VirtualAlloc(00000000,4096,4096,64)
Next is the result after closing .exe (created by Delphi 7) after  exception    // StrToInt('128z')
      303  Virtual Memory        02190000       4096  VirtualAlloc(00000000,4096,4096,64)
I think Delphi 7 created better .exe than Delphi 4 ....
0
 

Author Comment

by:mi6agent
ID: 12339507
>I think Delphi 7 created better .exe than Delphi 4

Not if those numbers are the in the "current size" column when you close the exe.

The first 4096 - if that is in the current size column, then your exe is leaking 4k - i had the same problem till i fixed the form.pas file as there was a known leak in delphi.  The other 4096 and 64 are also leaks if those numbers are in the current column.

Memproof is a good program to have don't you think?

What mine showed was in the "area" column under "exception" and 3 bytes per time i self caused the error.  Oddly enough i think i found the issue - the exception happens if i check for <0 and i entered -0 to convert.
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12342104
> Memproof is a good program to have don't you think?
O.k. it is a good programm, but it is possible there is no memory leak ....
May be Windows memory manager will deallocate that memory later ....
You may force the memory manager by

procedure TFormMain.FormDestroy(Sender: TObject);
begin
  SetProcessWorkingSetSize(GetCurrentProcess, $FFFFFFFF, $FFFFFFFF);
end;

(it works perfect under Windows XP and Delphi 7):

Cheers !
0
 

Author Comment

by:mi6agent
ID: 12342482
>May be Windows memory manager will deallocate that memory later

Yes, but "later" is not always a good idea which is why i use memproof to ensure the program has no leaks while it is running.
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12345111
Just force the memory manager on main form's OnDestriy event and all will be o.k. (My example is o.k.)
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12351756
(My example is o.k.): i.e. at the Resources Details page displays nothing after .exe is closed under Memproof, no matter with or without exception.
0
 
LVL 12

Accepted Solution

by:
esoftbg earned 200 total points
ID: 12352170
Hi mi6agent, I don't believe anymore Memproof !

I made 2 tests for 2 different .exe made by Delphi 7:

1). Application which contains 20 Forms and compiles 68737 lines Delphi code. Running under Memproof, after it's closure Memproof displays nothing at the Resources Details page (no matter there is or not an exception) ????

2). Application which contains 1 Form and compiles 74 lines Delphi code. Running under Memproof, after it's closure:
     2.1). Memproof displays at the Resources Details page when there is not exception:
             303  Virtual Memory        02190000       4096  VirtualAlloc(00000000,4096,4096,64)
     2.2). Memproof displays at the Resources Details page when there is an exception:
             303  Virtual Memory        02190000       4096  VirtualAlloc(00000000,4096,4096,64)
absolutely identical results ????

0
 

Author Comment

by:mi6agent
ID: 12357236
Ok, i think i need to recheck my coding because Memproof is showing some exception issues which i cause when testing the program but are not cleared.  So i think it is something i have forgotten or left out.

Anyway, what i shall do is raise the points and then split the points between esoftbg and Ivanov_G for helping - with a grade A of course :)
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to convert wav to mp3 in delphi 9 200
oracle global variables 4 68
JAudiorecorder record freezing the app 29 67
Tvertscrollbox like a whatsapp layout delete messages 1 16
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question