Use of unassigned vars

This forum is meant for questions and discussions about the X# language and tools
Post Reply
FFF
Posts: 1582
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Use of unassigned vars

Post by FFF »

Folks,

Code: Select all

TRY
Response := (HttpWebResponse)Request:GetResponse()
 SR := StreamReader{Response:GetResponseStream()}
	ResponseData := SR:ReadToEnd()
CATCH Wex AS System.Net.WebException
 SR := StreamReader{Wex.Response:GetResponseStream()}
	ResponseData := SR:ReadToEnd()
THROW Exception{ResponseData}
FINALLY
 SR:Close() // line 61
END TRY
gives me a
warning XS0165: Use of unassigned local variable 'SR' 61,3 EasyHttp.prg
Why?


Karl
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
wriedmann
Posts: 3757
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Use of unassigned vars

Post by wriedmann »

Hi Karl,

you are in a Try/Catch block, and if your code fails with an exception other than the one you are catching, before SR is initialized, you call the :Close() method on a not initialized object.
This sort of compiler warning helps me a lot!

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
FFF
Posts: 1582
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Use of unassigned vars

Post by FFF »

Wolfgang,
yep, i hear you.
But what to do? As far as i saw, i have to provide the input stream at instantiation of the object, so when moving instantiation out of the block i'm unsafe, in case the stream call fails. OTOH, reminds me why i don't like the concept of writing constructors which do more than producing an object..
Somewhat contradictory to the "assign vals to property in the constructor call in the other thread.
BTW, i tried the new conditional syntax oSR?:Close() to no avail, not sure what to make of that..

Finally, in my defense: this is not my code, but the transposition of VB source ;)

Thx
Karl
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
wriedmann
Posts: 3757
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Use of unassigned vars

Post by wriedmann »

Hi Karl,

I would set the SR to null before the Try statement, so the error goes away. And in the Finally block I would add a "if SR != null" statement.

It seems X# makes better warnings that VB.NET - IMHO the warning is 100% correct.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
FFF
Posts: 1582
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Use of unassigned vars

Post by FFF »

Hi Wolfgang,
not sure what use a := Null has? If both instantiation paths in try and catch fail, the final :close would crash, nevertheless.
I think the ?: syntax prevents a crash...

But i wonder: i get no warning in the ResponseData := SR:ReadToEnd() lines, probably because the instantiation is "inside" the same block. What means, the real warning occurs because the compiler can't feather that SR exists as either the try or the catch path WILL be executed.

In a certain way the inverse situation of "unreachable code" ;)

Not sure if this qualifies as a bug or not...
Anyway, interesting discussion,
thx for you time!
Karl
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
wriedmann
Posts: 3757
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Use of unassigned vars

Post by wriedmann »

Hi Karl,

personally I use statements like

Code: Select all

SR := null
often also in my VO applications to keep the compiler calm, and to say explicetely that I know that this variable can be null.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Frank Maraite
Posts: 178
Joined: Sat Dec 05, 2015 10:44 am
Location: Germany

Use of unassigned vars

Post by Frank Maraite »

Hi Karl,
in your first line after try you have

Code: Select all

Response := (HttpWebResponse)Request:GetResponse()
This may fail to another then a System.Net.WebException. This (other) exception is not catched. So the second line

Code: Select all

SR := StreamReader{Response:GetResponseStream()}
is not executed and not in any catch block.

Solution:

Code: Select all

LOCAL SR := NULL AS StreamReader
..
FINALLY
IF SR != NULL
   SR:Close()
ENDIF
I experience that the compiler is always right with that warning. It reminds you that SR may be uninitialized without intend and you have to check out this case.

Frank
Post Reply