sqldb create/free lifetime of object

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

sqldb create/free lifetime of object

noreply
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  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: sqldb create/free lifetime of object

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

> 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  -  [hidden email]
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: sqldb create/free lifetime of object

Michael Van Canneyt
In reply to this post by noreply


On Wed, 12 Apr 2017, Lars wrote:

> 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.

>
> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: sqldb create/free lifetime of object

Michael Van Canneyt
In reply to this post by LacaK


On Wed, 12 Apr 2017, LacaK wrote:

> Yes, example should be adjusted. Please report bug to not forget.

The example is correct. All objects are freed. No need for a bugreport.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: sqldb create/free lifetime of object

LacaK

>> Yes, example should be adjusted. Please report bug to not forget.
>
> The example is correct. All objects are freed. No need for a bugreport.
Yes, correct.
-Laco.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: sqldb create/free lifetime of object

noreply
In reply to this post by LacaK
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.
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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: sqldb create/free lifetime of object

Michael Van Canneyt


On Wed, 12 Apr 2017, [hidden email] wrote:

> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: sqldb create/free lifetime of object

Free Pascal - General mailing list
In reply to this post by noreply

Am 12.04.2017 14:32 schrieb <[hidden email]>:
> 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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Loading...