Sign in Join

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

Mailing lists / fpc-devel / 2008-12

Call initialisation of library automatically in unit-initialization?

7 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

Joost van der Sluis

Hi all,

When you use the MySQL client, you have to call myslq_library_init to initialise the library, and mysql_library_end to de-initialize it again.

When the library is dynamically loaded using mysqlXXdyn.pp, shall I call these functions automatically? Or should we leave it to the programmer? (I'm in favour of doing it automatically)

But when the library is linked in statically? Add these functions to the initialize and finalize sections of the unit mysqlXX.pp? Or should we leave these calls to the programmer? (In this case I would let the programmer do this)

Does this proposal make sense?

Joost.

Reply by email

Marco van de Voort
to Joost van der Sluis

In our previous episode, Joost van der Sluis said:

Show quoted text 6 lines
When you use the MySQL client, you have to call myslq_library_init to initialise the library, and mysql_library_end to de-initialize it again. When the library is dynamically loaded using mysqlXXdyn.pp, shall I call these functions automatically? Or should we leave it to the programmer? (I'm in favour of doing it automatically)

No real strong opinion, BUT if you don't do it, if the prototype changes, you can work around without requiring a new version of FPC.

(e.g. a certain MS version of the dll turns out to accidentally have decorated versions).

But when the library is linked in statically? Add these functions to the initialize and finalize sections of the unit mysqlXX.pp? Or should we leave these calls to the programmer? (In this case I would let the programmer do this)

I would too.

Reply by email

Michael Van Canneyt
to Joost van der Sluis: When you use the MySQL client, you have to call myslq_library_init to initialise the…
Joost van der Sluis wrote 8 lines
Hi all, When you use the MySQL client, you have to call myslq_library_init to initialise the library, and mysql_library_end to de-initialize it again. When the library is dynamically loaded using mysqlXXdyn.pp, shall I call these functions automatically? Or should we leave it to the programmer? (I'm in favour of doing it automatically)

Are there any options to these calls ?

If there are, it is a no, I think ?

Michael.

Reply by email

Joost van der Sluis
to Michael Van Canneyt

Op woensdag 03-12-2008 om 13:09 uur [tijdzone +0100], schreef Michael Van Canneyt:

Show quoted text 13 lines
On Wed, 3 Dec 2008, Joost van der Sluis wrote: > When you use the MySQL client, you have to call myslq_library_init to > initialise the library, and mysql_library_end to de-initialize it again. > > When the library is dynamically loaded using mysqlXXdyn.pp, shall I call > these functions automatically? Or should we leave it to the programmer? > (I'm in favour of doing it automatically) Are there any options to these calls ? If there are, it is a no, I think ?

Didn't thought about that. And yes, there are options. They are in practice only used with the embedded-mysql version. And iven in that case I guess they are rarely used:

"The argc and argv arguments are analogous to the arguments to main(), and enable passing of options to the embedded server. For convenience, argc may be 0 (zero) if there are no command-line arguments for the server. This is the usual case for applications intended for use only as regular (non-embedded) clients, and the call typically is written as mysql_library_init(0, NULL, NULL)."

So, what to do?

Joost.

Reply by email

Martin Friebe
to Joost van der Sluis
Joost van der Sluis wrote 22 lines
Op woensdag 03-12-2008 om 13:09 uur [tijdzone +0100], schreef Michael Van Canneyt: > On Wed, 3 Dec 2008, Joost van der Sluis wrote: > > >> When you use the MySQL client, you have to call myslq_library_init to >> initialise the library, and mysql_library_end to de-initialize it again. >> >> When the library is dynamically loaded using mysqlXXdyn.pp, shall I call >> these functions automatically? Or should we leave it to the programmer? >> (I'm in favour of doing it automatically) >> > Are there any options to these calls ? > > If there are, it is a no, I think ? > Didn't thought about that. And yes, there are options. They are in practice only used with the embedded-mysql version. And iven in that case I guess they are rarely used:

....

So, what to do?

There is another option. You can keep track if the user called it. If the user did not call it, but any other function is called, then call it yourself

procedure MaybeCallInit;
begin
  if InitCalled then exit;
  DoCallInit(0, nil, nil);
  InitCalled := True;
end;

Well it will be more complex, as if this is in a multi threaded environment you have to keep track for each Thread. That is unless the library is loaded and initialized before any threads are spawned...

Best Regards
Martin

Reply by email

Joost van der Sluis
to Martin Friebe

Op woensdag 03-12-2008 om 15:06 uur [tijdzone +0000], schreef Martin Friebe:

Show quoted text 37 lines
Joost van der Sluis wrote: > Op woensdag 03-12-2008 om 13:09 uur [tijdzone +0100], schreef Michael > Van Canneyt: > >> On Wed, 3 Dec 2008, Joost van der Sluis wrote: >> >> >>> When you use the MySQL client, you have to call myslq_library_init to >>> initialise the library, and mysql_library_end to de-initialize it again. >>> >>> When the library is dynamically loaded using mysqlXXdyn.pp, shall I call >>> these functions automatically? Or should we leave it to the programmer? >>> (I'm in favour of doing it automatically) >>> >> Are there any options to these calls ? >> >> If there are, it is a no, I think ? >> > > Didn't thought about that. And yes, there are options. They are in > practice only used with the embedded-mysql version. And iven in that > case I guess they are rarely used: > .... > So, what to do? > > There is another option. You can keep track if the user called it. If the user did not call it, but any other function is called, then call it yourself procedure MaybeCallInit; begin if InitCalled then exit; DoCallInit(0, nil, nil); InitCalled := True; end;

That's not possible, since the user can not call it before the library is loaded. And this code should be placed in the code to load the library.

Besides, the init is not the problem, because MySQL will call it automatically. But the finalization of the library, that's the real point. (If it's not called, that introduces mem-leaks)

But to call the finalization and not the initialization is pretty bad design, imho.

Well it will be more complex, as if this is in a multi threaded environment you have to keep track for each Thread. That is unless the library is loaded and initialized before any threads are spawned...

These calls have to be called before all other threads are spawned. Or else you're in trouble.

Joost.

Reply by email

Joost van der Sluis
to Joost van der Sluis

Op woensdag 03-12-2008 om 18:39 uur [tijdzone +0100], schreef Joost van der Sluis:

Show quoted text 50 lines
Op woensdag 03-12-2008 om 15:06 uur [tijdzone +0000], schreef Martin Friebe: > Joost van der Sluis wrote: > > Op woensdag 03-12-2008 om 13:09 uur [tijdzone +0100], schreef Michael > > Van Canneyt: > > > >> On Wed, 3 Dec 2008, Joost van der Sluis wrote: > >> > >> > >>> When you use the MySQL client, you have to call myslq_library_init to > >>> initialise the library, and mysql_library_end to de-initialize it again. > >>> > >>> When the library is dynamically loaded using mysqlXXdyn.pp, shall I call > >>> these functions automatically? Or should we leave it to the programmer? > >>> (I'm in favour of doing it automatically) > >>> > >> Are there any options to these calls ? > >> > >> If there are, it is a no, I think ? > >> > > > > Didn't thought about that. And yes, there are options. They are in > > practice only used with the embedded-mysql version. And iven in that > > case I guess they are rarely used: > > > .... > > So, what to do? > > > > > There is another option. You can keep track if the user called it. If > the user did not call it, but any other function is called, then call it > yourself > > procedure MaybeCallInit; > begin > if InitCalled then exit; > DoCallInit(0, nil, nil); > InitCalled := True; > end; That's not possible, since the user can not call it before the library is loaded. And this code should be placed in the code to load the library. Besides, the init is not the problem, because MySQL will call it automatically. But the finalization of the library, that's the real point. (If it's not called, that introduces mem-leaks) But to call the finalization and not the initialization is pretty bad design, imho.

Because the library-handle is also reference counted it's quite difficult for the programmer to call these functions at the right moment.

So I've decided to call them automatical, and add the parameters to the InitialiseMySQL procedure, so that they are passed to the library-init function.

Joost.

Reply by email