Recent

Author Topic: Exceptions causing Memory Leaks?  (Read 3092 times)

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Exceptions causing Memory Leaks?
« on: March 23, 2019, 04:02:40 pm »
Hi Folks,

ok, i'll bite. After searching and trying, and nothing helped, i'll ask finally.

Do i have to clean up an Exception?

Background:
I have some - Try - Except blocks
Code: Pascal  [Select][+][-]
  1. Try
  2. //DoSomething with a Database which might cause an exception
  3. Except
  4. On Err:ESQLDatabaseError Do
  5. Begin
  6. WriteErrortologfile;
  7. End;
  8.  
I have zero Memory leaks, if i don't enter the Except-Fork
If the code enters the except-fork, i have 27 unfreed blocks
The HeapTrc-Output doesn't help much, because it points to the line/unit, from which that procedure was called.
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1059
Re: Exceptions causing Memory Leaks?
« Reply #1 on: March 23, 2019, 04:06:41 pm »
Hi Folks,

ok, i'll bite. After searching and trying, and nothing helped, i'll ask finally.

Do i have to clean up an Exception?

Background:
I have some - Try - Except blocks
Code: Pascal  [Select][+][-]
  1. Try
  2. //DoSomething with a Database which might cause an exception
  3. Except
  4. On Err:ESQLDatabaseError Do
  5. Begin
  6. WriteErrortologfile;
  7. End;
  8.  
I have zero Memory leaks, if i don't enter the Except-Fork
If the code enters the except-fork, i have 27 unfreed blocks
The HeapTrc-Output doesn't help much, because it points to the line/unit, from which that procedure was called.
You don't have to free up the exception itself, that happens automatically. However, the code that threw the exception may not have cleaned up everything before/when it threw the exception.

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Exceptions causing Memory Leaks?
« Reply #2 on: March 23, 2019, 04:15:12 pm »
*sigh*

i feared as much.....
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Exceptions causing Memory Leaks?
« Reply #3 on: March 23, 2019, 05:54:17 pm »
Have to reopen this one.
Scenario as follows:

In a library with exported flat functions.

Class1=MainThread
Class2=ChildThread
Class3=Database-Object
Class4=LogFile

Class1 creates Class2 which creates Class3, which creates Class4

The Exception occurs in Class 3, which i catch with a Try-Except (Class 3 is my "Representation" of a Database with all the Toys - Connection, Transaction, Query).
The Query creates the exception (an SQL-Statement checking if a table exists (SELECT * FROM UnknownTable) - If table unknown raise red flags)

What i don't understand: If everything is OK, no Memory leak and all objects are freed properly (otherwise i'd have leaks).
If the Exception occurs, i have a memory leak.
I've stepped through the whole code with no error, and even my Logfile shows proper chaining in freeing the objects.
All my procedure-calls transmit a result-message back to the start of the chain, which i evaluate, and if an error occured (like the Exception mentioned), i trigger the standard chain of destroying all objects (starting with class4, back to class 3, back to class 2, and finally back to class 1)
This standard-chain of freeing the objects is used, if everything went well! And there i have no leaks.

What am i missing?

EDIT: No Pointers at play. I use exactly one Pointer-Variable in my testing-project (Pointer to Record), which i dispose of after passing it to the library.
(Even tried setting it to nil inside the library. No difference, as well for the good result as well as the bad result)
« Last Edit: March 23, 2019, 05:58:31 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: Exceptions causing Memory Leaks?
« Reply #4 on: March 23, 2019, 07:27:40 pm »
HeapTrc should point you to the object(s) which have not been freed on exception.
That should give you a clue to what went wrong when exception is thrown.
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Exceptions causing Memory Leaks?
« Reply #5 on: March 23, 2019, 07:55:48 pm »
I know that.
Right now i'm trying to figure out how to read the Heaptrc-output.
Any pointers to that?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

jamie

  • Hero Member
  • *****
  • Posts: 6128
Re: Exceptions causing Memory Leaks?
« Reply #6 on: March 23, 2019, 08:04:39 pm »
use the Call Stack option in the debugger options..

VIEW:Debugger:Call Stack

it should give you a reverse roll back of where it came from..

The entry at the top of the list is the most recent function/procedure.
The only true wisdom is knowing you know nothing

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Exceptions causing Memory Leaks?
« Reply #7 on: March 23, 2019, 08:15:00 pm »
uhh...it stays empty
Debug-Infotype is Dwarf with sets (godwarfsets)

EDIT: Ah got it.
It shows only during singlesteps.
Gotcha
« Last Edit: March 23, 2019, 08:16:55 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Exceptions causing Memory Leaks?
« Reply #8 on: March 23, 2019, 08:31:53 pm »
I just can't make heads or tails of it
hmmm.... is it because the exception occurs in the constructor of the class?
Each class creates its child in its constructor (and so on....)

EDIT: No difference if it's in its own procedure
« Last Edit: March 23, 2019, 08:48:39 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Exceptions causing Memory Leaks?
« Reply #9 on: March 24, 2019, 01:33:55 am »
You need to use some defensive programming. If you create an instance of a class, use Try/Finally to make sure it gets freed when done/exception raised:
Code: Pascal  [Select][+][-]
  1.   c1 := TClass1.Create;
  2.   try
  3.     c1.func1....
  4.     ....
  5.   finally
  6.     c1.Free;
  7.   end;

Use it with every class.
« Last Edit: March 24, 2019, 01:38:44 am by engkin »

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Exceptions causing Memory Leaks?
« Reply #10 on: March 24, 2019, 08:33:55 am »
You mean to tell me, if an object throws an exception it becomes unusable and i have to destroy it???

And i do the freeing in the destructor of the class containing this object
Code: Pascal  [Select][+][-]
  1. destructor Class3.Destroy;
  2. Begin
  3.   Try
  4.     Transaction.Active:=False;
  5.     Query.Close;
  6.     Connection.CLose;
  7.   Finally
  8.     Query.Free;
  9.     Transaction.Free;
  10.     Connection.Free;
  11.   End;
  12. Inherited Desstroy;
  13. end;
  14.  
« Last Edit: March 24, 2019, 09:23:45 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Exceptions causing Memory Leaks?
« Reply #11 on: March 24, 2019, 09:36:39 am »
This is getting weirder and weirder.

i have the usual threesome of a Connection (TSQL-Connector, setting Type during runtime), Transaction and Query

Just for grins, i forced some exceptions on the connection-object (the usual fun: wrong UserName, wrong Password, wrong DB-Name, User has no privileges)
The code catches the exception, logs it, and jumps out, and the chain progresses properly, all objects freed, no leaks.

It's just, when the exception occurs with the Query-Object....
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Exceptions causing Memory Leaks?
« Reply #12 on: March 24, 2019, 10:00:24 am »
Solved for now.

It really seems to be the SQL_Statement, that run afoul (Checking if a table exists).
I've now used Connection.GetTableNames, and the Leak is gone.
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018