Recent

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

aducom

  • Full Member
  • ***
  • Posts: 162
    • 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: 873
  • FPC Unleashed FTW
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.
FPC Unleashed - inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐ Star it on GitHub!

aducom

  • Full Member
  • ***
  • Posts: 162
    • 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: 18952
  • Glad to be alive.
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 »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12770
  • 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: 18952
  • Glad to be alive.
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.
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

Fibonacci

  • Hero Member
  • *****
  • Posts: 873
  • FPC Unleashed FTW
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
FPC Unleashed - inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐ Star it on GitHub!

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1582
    • 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: 162
    • 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: 1582
    • 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: 18952
  • Glad to be alive.
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.
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

Thaddy

  • Hero Member
  • *****
  • Posts: 18952
  • Glad to be alive.
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.
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

aducom

  • Full Member
  • ***
  • Posts: 162
    • 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: 18952
  • Glad to be alive.
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 »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

paule32

  • Hero Member
  • *****
  • Posts: 645
  • One in all. But, not all in one.
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 »
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

 

TinyPortal © 2005-2018