0

I have the following code:

var wqry:TAdoQuery;
...
  FillChar(wSpaces,cSpacesAfter,' ');
  try
    wqry := TADOQuery.Create(nil);//here the error
    wqry.Connection:=...

cSpacesAfter is a constant and has the value 1035. wSpaces is a local string variable. The problem is that I receive the following error when TAdoQuery is created

enter image description here

even it is in french, I believe you got the idea.....

If I comment the FillChar code, everything works ok. I have the usual compiler directives, nothing special. I'm using Delphi 7.

Can someone tell me what is wrong with that code?

RBA
  • 12,337
  • 16
  • 79
  • 126
  • If that's copy-pasted code, you're initializing `wqry` but using `wry`. Note the missing `q` in the second name. If it's not copy-pasted code, please fix it (by copy-pasting code) – Cosmin Prund May 09 '11 at 13:00
  • wSpaces:=stringofchar(' ',cSpacesAfter); - resolved the problem. no error is raised. – RBA May 09 '11 at 13:22
  • thank you for the -1. indeed, it was a silly question, lack of sleep made its word. – RBA May 09 '11 at 19:35

2 Answers2

3

The troublesome code is most likely this one

FillChar(wSpaces,cSpacesAfter,' ');

I'm assuming that wSpaces is of string type. A string variable is in fact nothing more than a pointer to the data structure that holds the string. You don't need to use pointer syntax because the compiler takes care of that for you.

So what this code does is overwrite the variable holding that pointer with 4 space characters and then write 1031 more spaces over the top of whatever follows the variable. In short you will completely corrupt your memory. That would explain why the FillChar works but the very next line of code dies a painful and dramatic death.

If your string indeed had space for 1035 characters your could instead write:

FillChar(wSpaces[1], cSpacesAfter, ' ');

However, if may be more idiomatic to write:

wSpaces := StringOfChar(' ', cSpacesAfter);
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • yes, wSpaces is a local string variable. FillChar(wSpaces[1], cSpacesAfter, ' '); - generates 000000.. AV. – RBA May 09 '11 at 13:29
  • @RBA It only generates an AV if you don't allocate some storage first! Anyway, StringOfChar is your answer as you have said in a comment above. – David Heffernan May 09 '11 at 13:33
1

FillChar procedure fills out a section of storage Buffer with the same byte or character FillValue FillCount times.

It is principally used to initialise arrays of numbers. It can be used to initialise records and strings, but care should be used to avoid overwriting length fields. StringOfChar is best for filling out strings to the same character.

Are you sure wSpaces has the size enough to fit all of cSpacesAfter you write to it?

Community
  • 1
  • 1
Kromster
  • 7,181
  • 7
  • 63
  • 111
  • if added setlength(wSpaces,cSpacesAfter+1); before FillChar function and the error is still generated. – RBA May 09 '11 at 13:20
  • You would have to do something like this: `Setlength(wSpaces, cSpacesAfter); FillChar(wSpaces[1], cSpacesAfter, ' ');` as per my answer, but `StringOfChar` is much better. – David Heffernan May 09 '11 at 13:25