Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2017-04

sqldb create/free lifetime of object

8 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Lars

The sql query object and transaction object are created but not freed:

http://www.freepascal.org/docs-html/fcl/sqldb/usingsqldb.html

I think the docs should mention that the objects need not to be freed, as this violates normal programming practice of freeing all objects you create.

P.s. how is it freed magically? Sorry I am on cell phone and cannot look at source easily to find out

This is one gripe I have with programming is that you never really know if something should be freed just by looking at the code. If it is not freed and it was created by you it is a good indication its a mem leak, but not if there is magic behind the scenes to free it automatically

LacaK
to Lars

Yes, example should be adjusted. Please report bug to not forget. Thanks
-Laco.

Show quoted text 18 lines
The sql query object and transaction object are created but not freed: http://www.freepascal.org/docs-html/fcl/sqldb/usingsqldb.html I think the docs should mention that the objects need not to be freed, as this violates normal programming practice of freeing all objects you create. P.s. how is it freed magically? Sorry I am on cell phone and cannot look at source easily to find out This is one gripe I have with programming is that you never really know if something should be freed just by looking at the code. If it is not freed and it was created by you it is a good indication its a mem leak, but not if there is magic behind the scenes to free it automatically _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Michael Van Canneyt
to Lars: The sql query object and transaction object are created but not freed:…
Lars wrote 7 lines
The sql query object and transaction object are created but not freed: http://www.freepascal.org/docs-html/fcl/sqldb/usingsqldb.html I think the docs should mention that the objects need not to be freed, as this violates normal programming practice of freeing all objects you create.

They must of course be freed. They ARE freed.

P.s. how is it freed magically? Sorry I am on cell phone and cannot look at source easily to find out

All three classes are components.
Q and T are owned by C. When C is freed, Q and T are freed.

Show quoted text 5 lines
This is one gripe I have with programming is that you never really know if something should be freed just by looking at the code. If it is not freed and it was created by you it is a good indication its a mem leak, but not if there is magic behind the scenes to free it automatically

There is no magic other than standard TComponent owner/owned functionality.

Michael.

Unknown author
to LacaK: Yes, example should be adjusted. Please report bug to not forget. Thanks -Laco.
LacaK wrote
Yes, example should be adjusted. Please report bug to not forget. Thanks -Laco.

It is not a bug, I just wanted to clarify the magic going on behind the scenes - which, is "ownership".

That's all I needed to know.

The key to seeing this trick/magic is in the Create function where you pass in the C (connection) as the owner.

But on my cell phone I didn't really see this, and, it's not always obvious.. hence my gripe.

In a way, this is a form of garbage collection or RAII sort of. And reduces the obnoxious free's required in non garbage collected programming languages... So it's a nice feature/trick to have ownership. But at the same time you can think there is a memory leak since there is no create/free pair easily visible...

So you train your eyes to look for ownership.

And similarly, delphi gui widgets you throw onto a form, don't need to be freed yourself, even though the program created them - so in a way delphi TForm's gui widgets are also garbage collected, sort of. Again a nice feature/trick to avoid all the obnoxious free calls that bloat up the source code with line noise.

Michael Van Canneyt
to Unknown author
Show quoted text 20 lines
On 2017-04-12 05:03, LacaK wrote: > Yes, example should be adjusted. Please report bug to not forget. > Thanks > -Laco. > It is not a bug, I just wanted to clarify the magic going on behind the scenes - which, is "ownership". That's all I needed to know. The key to seeing this trick/magic is in the Create function where you pass in the C (connection) as the owner. But on my cell phone I didn't really see this, and, it's not always obvious.. hence my gripe. In a way, this is a form of garbage collection or RAII sort of. And reduces the obnoxious free's required in non garbage collected programming languages... So it's a nice feature/trick to have ownership.
It's one of the reasons I descend most of my classes from TComponent;
the cleanup is then automatic...

Michael.

Sven Barth
to Unknown author: It is not a bug, I just wanted to clarify the magic going on behind the scenes - which,…
In a way, this is a form of garbage collection or RAII sort of. And

reduces the obnoxious free's required in non garbage collected programming languages... So it's a nice feature/trick to have ownership. But at the same time you can think there is a memory leak since there is no create/free pair easily visible...

So you train your eyes to look for ownership. And similarly, delphi gui widgets you throw onto a form, don't need to be

freed yourself, even though the program created them - so in a way delphi TForm's gui widgets are also garbage collected, sort of. Again a nice feature/trick to avoid all the obnoxious free calls that bloat up the source code with line noise.

Both are in fact using the same mechanism as you can put e.g. a TSQLTransaction on a form as well.

Regards,
Sven