Subject Urgent help needed with an access violation
Author Salvatore Besso
hello all,

I hope to be in time before Jason releases the next official version, but
suddendly I receive an AV simply trying to open an TIB_Query that has a
perfectly legal SQL statement. But notice that this happens only the second time
that I try to open it.

Very schematic scenario:

- The data module containing the query is created and the
query is still closed. This DM is used by two forms
- Creation of a first form that doesn't use that query
- Creation on demand of a second form that uses that query
that has an TIB_Grid for browsing and an TIB_LocateEdit
for searching

The data-module contains two public procedures:

procedure TMyDM.OpenEditTables;
begin
EditQuery.Prepare;
EditQuery.Params.ByName('LOGOWNER').AsInteger := FOwnerID;
EditQuery.Open;
ProvQuery.Prepare;
ProvQuery.Open;
ModeQuery.Prepare;
ModeQuery.Open
end;

procedure TMyDM.CloseEditTables:
begin
EditQuery.Close;
EditQuery.Unprepare;
ProvQuery.Close;
ProvQuery.Unprepare;
ModeQuery.Close;
ModeQuery.Unprepare
end;

This is the OnCreate handler for the second form:

begin
inherited;
MyDM.OpenEditTables
end;

and this is the OnClose handler for the second form:

begin
MyDM.CloseEditTables
end;

The first time the second form is created all works as expected, the queries are
opened and I can browse the data. Then I close the second form that is freed and
its variable nil'ed and of course the TIB_Query's are closed (the data-module
still exists in memory because is used by the first form as well) and I return
to the first form. Now I open the second form for edit another time:

procedure TMyDM.OpenEditTables;
begin
EditQuery.Prepare;
EditQuery.Params.ByName('LOGOWNER').AsInteger := FOwnerID;
EditQuery.Open;
ProvQuery.Prepare;
ProvQuery.Open; // <--- blam! Access violation
ModeQuery.Prepare;
ModeQuery.Open
end;

I'm still investigating on the possible reasons of this AV but I still can't
figure out why it happens, but it seems that it is something related to
KeyLinks. ProvQuery and ModeQuery have both KeyLinks and KeyDescLinks defined
(only one column) and the KeySource is EditQuery for both. At some point in
IB_Components, in method TIB_Dataset.CheckKeyLinksMaps, the KeyLinks columns are
stored in KeyLinks.Objects and the first time the query is opened all is ok. But
the second time the query is opened, I don't know for what reason, an invalid
pointer is stored in KeyLinks.Objects and then, in method
TIB_Dataset.SysUpdateKeyLinksData at line 28675 the instruction
"keyCol.Assign( tmpCol )" causes the AV because tmpCol contains the invalid
pointer (but that is not nil nor equal to Pointer(-1)) retrieved from
KeyLinks.Objects.

I repeat that this query normally works without problems. The problem arises
only when I open it the second time, and another strange thing that doesn't
always happen is that after the AV sometimes the query is in edit mode instead
of browse mode. To make things work again I have to close the first form,
destroy the data-module and repeat the process again (data-module creation,
first form creation and second form creation). I have also tried to remove all
event handlers from the query and its datasource, but without success, the AV
still happens.

So far, so bad :-( I continue investigation but a help or an indication from you
would be greatly appreciated.

Regards
Salvatore