#1 2016-03-04 11:43:37

cypriotcalm
Member
Registered: 2015-02-18
Posts: 122

TSQLDBConnectionProperties and freeing NewConnections

Hi there!

I have the following code:

var
  ConnectionProperties: TSQLDBConnectionProperties;
  Conn1, Conn2: TSQLDBConnection;
begin
  ConnectionProperties := TSQLDBFireDACConnectionProperties.Create(
    'MySQL?Server=127.0.0.1;Port=3306',
    'db',
    'root',
    'pass');
  Conn1 := ConnectionProperties.ThreadSafeConnection;
  Conn2 := ConnectionProperties.NewConnection;
  try
    Conn1.Connect;
    Conn2.Connect;
  finally
    Conn1.Free;
    Conn2.Free;
    ConnectionProperties.Free; // ---> ERROR invalid pointer operation because Conn1 was freed before
  end;
end;

My question is: why is the memory management of .ThreadSafeConnection and .NewConnection handeled in different ways? Is it a bug or a feature? .ThreadSafeConnection calls internally the .NewConnection.

Thank you!

Best regards,
cc

Offline

#2 2016-03-04 16:13:59

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: TSQLDBConnectionProperties and freeing NewConnections

Some database driver (OleDb for example) require to free connection resources in the same thread connection was created. So it is unsafe to write Conn1.Free;
If your application use threads you must use threadSafeConnection and on thread terminate call ConnectionProperties.EndCurrentThread to correctly release connection resources

Offline

#3 2016-03-04 17:07:08

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: TSQLDBConnectionProperties and freeing NewConnections

You should not free the ThreadSafe connection instances, which are owned by ConnectionProperties.

Offline

Board footer

Powered by FluxBB