1

I'm using Zeos and SQLite3 DB in Delphi

ZQuery2.Close;
ZQuery2.SQL.Clear;
ZQuery2.SQL.Add('SELECT * FROM users WHERE un = ' + QuotedStr( UserName ) );

ZQuery2.Open;

OutputDebugString(PWideChar( ZQuery2.FieldDefList.CommaText )); // log : id,un,pw
OutputDebugString(PWideChar(ZQuery2.FieldByName('pw').AsString)); //causes error sometimes

the code is working but sometimes I get the following error message Exception class EDatabaseError with message 'ZQuery2:Field'pw' not found'.

Somebody
  • 703
  • 1
  • 7
  • 23
  • Odd. When the exception occurs, does 'pw' occur in the FieldDefList and in ZQuery2's actual list of fields? Is there anything unusual about the of UserName when the exception occurs and does it consistently happen for the same UserName value? Have you tried checking for a memory overwrite? – MartynA Feb 25 '20 at 17:53
  • @MartynA FieldDefList always returning the fields and the username is the same and also it happens for different names sometimes working and sometimes throw this exception, what you mean by checking for a memory overwrite? – Somebody Feb 25 '20 at 18:00
  • 1
    Well, memory overwrites happen usually when something is written to the wrong place in memory, overwriting what is there, usually because of an incorrect pointer value. Usually, the pointer value is so wildly wrong that the OS can detect it and raise an AV, but sometimes it is less obvious. Delphi's memory manager has a 'full debug mode' which adds special checks for this condition - see http://docwiki.embarcadero.com/RADStudio/Rio/en/Configuring_the_Memory_Manager – MartynA Feb 25 '20 at 18:10
  • @MartynA that's right it was memory overwrite, please post it as an answer, thanks – Somebody Feb 25 '20 at 18:44

1 Answers1

1

This is odd because a field of a dataset shouldn't just disappear while the app is in the middle of running, especially if other fields are still operating normally. So, I would suspect something like a memory overwrite being the cause.

Memory overwrites usually happen when something is written to the wrong place in memory, overwriting what is there, usually because of an incorrect pointer value or a so-called "buffer overrun" where the writing operation carries on beyond where is should stop. Usually, the pointer value is so wildly wrong that the OS can detect it and raise an AV, but sometimes it is less obvious.

Delphi's memory manager has a 'full debug mode' which adds special checks for this condition, see here.

I suggest you enable full debug mode as per the linked document and wait for the exception to occur.

MartynA
  • 30,454
  • 4
  • 32
  • 73