Recent

Author Topic: Nil value on assignment of '' ?  (Read 1798 times)

aducom

  • Full Member
  • ***
  • Posts: 159
    • http://www.aducom.com
Nil value on assignment of '' ?
« on: February 28, 2024, 05:36:34 pm »
I'm a bit confused. I'm testing a string for an empty value like 'if something <> '' then and get an access violation. Debugging, I find that a line as
something := '';

the value of something is nil?

I must be overlooking some kind of switch here, as I cannot explain. Can't test for nil as it is a string...

What am I overlooking?

b.r. Albert

Fibonacci

  • Hero Member
  • *****
  • Posts: 647
  • Internal Error Hunter
Re: Nil value on assignment of '' ?
« Reply #1 on: February 28, 2024, 05:39:11 pm »
Make a small example and post the code here.

aducom

  • Full Member
  • ***
  • Posts: 159
    • http://www.aducom.com
Re: Nil value on assignment of '' ?
« Reply #2 on: February 28, 2024, 05:45:36 pm »
procedure myobject.test;
var
   a: string;
begin
   a :='';
   if a='' then begin // access violation....

Thaddy

  • Hero Member
  • *****
  • Posts: 16411
  • Censorship about opinions does not belong here.
Re: Nil value on assignment of '' ?
« Reply #3 on: February 28, 2024, 05:52:12 pm »
That in itself can not crash, because it won't compile....but what string type are you using? String can have many meanings on fpc.
What type of class are you using? it is object or Tobject?
Maybe you interpreted it as a class procedure?
Or did you instantiate the class properly?
And which mode?

ALL of the above is basic information to help you.

We do not have the right information from that incomplete piece of code.
« Last Edit: February 28, 2024, 06:02:18 pm by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12000
  • FPC developer.
Re: Nil value on assignment of '' ?
« Reply #4 on: February 28, 2024, 06:16:06 pm »
Please provide a complete, compilable program that demonstrates the problem.

Thaddy

  • Hero Member
  • *****
  • Posts: 16411
  • Censorship about opinions does not belong here.
Re: Nil value on assignment of '' ?
« Reply #5 on: February 28, 2024, 06:19:38 pm »
Please provide a complete, compilable program that demonstrates the problem.
It needs to be complete, compilation is allowed to fail.
There is nothing wrong with being blunt. At a minimum it is also honest.

Fibonacci

  • Hero Member
  • *****
  • Posts: 647
  • Internal Error Hunter
Re: Nil value on assignment of '' ?
« Reply #6 on: February 28, 2024, 06:19:53 pm »
@marcov: Damn, you scared me. That serious tone :D

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1455
    • Lebeau Software
Re: Nil value on assignment of '' ?
« Reply #7 on: February 28, 2024, 06:23:29 pm »
I'm a bit confused. I'm testing a string for an empty value like 'if something <> '' then and get an access violation. Debugging, I find that a line as
something := '';

the value of something is nil?

Correct.  When the string type maps to a long string (AnsiString or UnicodeString) rather than to ShortString, then it is a reference type, ie a pointer to data, and so an empty string is implemented as a nil pointer.

Can't test for nil as it is a string...

Testing a long string for '' or nil does the exact same thing:

Code: Pascal  [Select][+][-]
  1. var
  2.    a: string;
  3. begin
  4.    a :='';
  5.    if a = '' then // OK
  6.    if Pointer(a) = nil then // OK
  7.  

The codegen for both cases is the same.

Neither case by itself should ever cause an AV.  The only scenario I can think of that would AV is if you are testing a non-static string member of a class, and you are using an invalid object pointer to reach that member.
« Last Edit: February 28, 2024, 06:37:53 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

aducom

  • Full Member
  • ***
  • Posts: 159
    • http://www.aducom.com
Re: Nil value on assignment of '' ?
« Reply #8 on: February 28, 2024, 06:31:38 pm »
Sounds ok to me, however, In my experience, the application crashes when comparing with '', comparing with nil generates an error as you cannot compare a pointer with a string. But I could typecase of course. I'm confused because I'm using Lazarus for ages, and never have seen this happening.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1455
    • Lebeau Software
Re: Nil value on assignment of '' ?
« Reply #9 on: February 28, 2024, 06:39:12 pm »
In my experience, the application crashes when comparing with ''

Then something is seriously wrong with your compiler/environment.  It should not be crashing.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Thaddy

  • Hero Member
  • *****
  • Posts: 16411
  • Censorship about opinions does not belong here.
Re: Nil value on assignment of '' ?
« Reply #10 on: February 28, 2024, 06:39:29 pm »
I won't spend time on this if I see no code.
There is nothing wrong with being blunt. At a minimum it is also honest.

Thaddy

  • Hero Member
  • *****
  • Posts: 16411
  • Censorship about opinions does not belong here.
Re: Nil value on assignment of '' ?
« Reply #11 on: February 28, 2024, 06:41:46 pm »
Then something is seriously wrong with your compiler/environment.  It should not be crashing.
No there is something seriously wrong with a programmer that makes assumptions and shows us no reproduceable code.
There is nothing wrong with being blunt. At a minimum it is also honest.

aducom

  • Full Member
  • ***
  • Posts: 159
    • http://www.aducom.com
Re: Nil value on assignment of '' ? {closed}
« Reply #12 on: February 28, 2024, 06:43:16 pm »
Actualy Remy Lebeau is right. The variable is not static, declaring it static fixed the issue for me. Lesson learned. Thx all.

Thaddy

  • Hero Member
  • *****
  • Posts: 16411
  • Censorship about opinions does not belong here.
Re: Nil value on assignment of '' ?
« Reply #13 on: February 28, 2024, 06:46:22 pm »
Remy is - almost - always right. :o :D But in this case I am right too because your explanation does not make any sense.
« Last Edit: February 28, 2024, 06:47:58 pm by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: Nil value on assignment of '' ?
« Reply #14 on: February 28, 2024, 06:48:03 pm »
if you Access STRING - then the CompilerProc fpc_ansistr_assign will be involved:

var s1 : String;
begin
  s1 := 'foo';  // this instruct's "fpc_ansistr_assign", to allocate Memory/Move.
end;

fpc_ansistr_assign check for "nil".
Else it would be result in AV.
« Last Edit: February 28, 2024, 06:49:51 pm by paule32 »

 

TinyPortal © 2005-2018