Sign in Join

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

Mailing lists / fpc-pascal / 2000-12

About ansistrings.

10 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

FPC Mail list reader

Hi. I'm doing some stuff in which i declare a record-type which contains an ansistring. When i try to allocate a variable of that type, i get a General Protection Fault. Here's the source code :

---||cut here||---------------------
type TStuff=record
            s:ansiestring;
           end;
     Pstuff=^Tstuff;
var stuff:array[1..10] of Pstuff;
    idx:word;
begin
 idx:=1;
 new(stuff[idx]);
end.
---||stop cutting||-----------------

Under Go32 it seems to work, but it crashes under Win32 & Linux.

I've noticed that if i replace "new(stuff[idx])" with "new(stuff[1])" it works. And Delphi doesn't seem to have any problem with any of the 2 calls to new().

Is there any obvious solution for this ?

Thanx for reading till here,

Bogdan.

Florian Klaempfl
to FPC Mail list reader
you wrote 24 lines
Hi. I'm doing some stuff in which i declare a record-type which contains an ansistring. When i try to allocate a variable of that type, i get a General Protection Fault. Here's the source code : ---||cut here||--------------------- type TStuff=record s:ansiestring; end; Pstuff=^Tstuff; var stuff:array[1..10] of Pstuff; idx:word; begin idx:=1; new(stuff[idx]); end. ---||stop cutting||----------------- Under Go32 it seems to work, but it crashes under Win32 & Linux. I've noticed that if i replace "new(stuff[idx])" with "new(stuff[1])" it works. And Delphi doesn't seem to have any problem with any of the 2 calls to new(). Is there any obvious solution for this ?

Which compiler version do you use? At least with 1.0.2 under linux I can't reproduce it with the program you provided.

Udo Giacomozzi
to FPC Mail list reader: Hi. I'm doing some stuff in which i declare a record-type which contains an ansistring.…

Hello FPC,

Sunday, December 31, 2000, 12:40:07 AM, you wrote:

FMlr> Hi. I'm doing some stuff in which i declare a record-type which FMlr> contains an ansistring. When i try to allocate a variable of that type, i FMlr> get a General Protection Fault. Here's the source code :

FMlr> Under Go32 it seems to work, but it crashes under Win32 & Linux.

FMlr> I've noticed that if i replace "new(stuff[idx])" with "new(stuff[1])" it FMlr> works. And Delphi doesn't seem to have any problem with any of the 2 calls FMlr> to new().

Does this particular code generate the GPF or do you have some other code after new() ?
as ansistrings are some sort of pointers, sometimes it makes problems when the variable is not correctly initialized.
i don't know right now if new() clears the contents of the memory block (ie. sets to zero) but if it don't and you access the string, you may run into problems.

sometimes it also does not generate an GPF but later in the program strange things are happening...

Udo

Best regards, Udo
Florian Klaempfl
to Udo Giacomozzi
you wrote 6 lines
Does this particular code generate the GPF or do you have some other code after new() ? as ansistrings are some sort of pointers, sometimes it makes problems when the variable is not correctly initialized. i don't know right now if new() clears the contents of the memory block (ie. sets to zero)

It does (and dispose cleans the ansistring up even if it nested in arrays/records/objects)

Udo Giacomozzi
to Florian Klaempfl

Hello Florian,

Sunday, December 31, 2000, 6:05:50 PM, you wrote:
FK> It does (and dispose cleans the ansistring up even if it nested in FK> arrays/records/objects)

good :)

what about FillChar() to zero when the string is already set?

Udo

Florian Klaempfl
to Udo Giacomozzi
you wrote 9 lines
Hello Florian, Sunday, December 31, 2000, 6:05:50 PM, you wrote: FK> It does (and dispose cleans the ansistring up even if it nested in FK> arrays/records/objects) good :) what about FillChar() to zero when the string is already set?
This will fail. Then you've to do a
finalize(data);
fillchar(data,sizeof(data),0);

Be careful: finalize is supported only in the 1.1 branch.

Udo Giacomozzi
to Florian Klaempfl

Hello Florian,

you wrote
>what about FillChar() to zero when the string is already set?
FK> This will fail. Then you've to do a
FK> finalize(data);
FK> fillchar(data,sizeof(data),0);

what does finalize() exactly? does it free all ansistrings or does it more? i could not find it in the docs...

Udo

PS: Happy new year to all!!! :)

Florian Klaempfl
to Udo Giacomozzi: FK> This will fail. Then you've to do a FK> finalize(data); FK>…
you wrote 11 lines
Hello Florian, Sunday, December 31, 2000, 7:20:20 PM, you wrote: >>what about FillChar() to zero when the string is already set? FK> This will fail. Then you've to do a FK> finalize(data); FK> fillchar(data,sizeof(data),0); what does finalize() exactly? does it free all ansistrings or does it more?

It decreases the reference counters of all ref. counted data (ansistrings, widestring, dyn. arrays and interfaces). If the ref. count is zero, the data is disposed/released and the field is set to zero.

i could not find it in the docs...

Because it is in the development branch (1.1) only as well as interfaces and dyn. arrays.