Quantcast
Channel: Visual Studio General Questions forum
Viewing all articles
Browse latest Browse all 21115

False positive error 'use of uninitialized variable'

$
0
0

Reference...

http://social.msdn.microsoft.com/Forums/en-US/2ab98a70-9ce4-4d46-8c2c-3b432014bdde/how-to-turn-off-error-cs0165-uninitialized-variable?forum=csharplanguage

But... this is the code; a simpler example...

using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

namespace org.d3x0r.xperdex.games.unity_plugin
{

public class PatchSphere

{ // 64 is the max patch size to render the sphere as one mesh int cells; bool is_unity = false; public PatchSphere( int cells = 12 ) { this.cells = cells; } void Function() { Vector3 tmp_v = Vector3.up; Quaternion longitude_rot; Vector3 tmp_elevation; if( is_unity ) longitude_rot = Quaternion.AngleAxis( 30f, Vector3.up ); else ; // q cannot be initialized outside of unity environment for( int row = 0; row < cells; row++ ) { int cols = row * 6; if( is_unity ) tmp_elevation = longitude_rot * tmp_v; /*....*/ } } } }


is_unity is set once, and is never referenced with a set in any other code.

I can (in this case) add 'const' to make it work...

The variable 'is_uniity' isn't declared as volatile, so the code shouldn't expect it to change outside of the current thread...

So there is no path where 'loginitude_rot' can be used without it previously being initialized.

In this case Quaternion is a struct, from the unity engine, which is not available to use outside of the unity environment, but I want to test my mesh generation code offline... 

can solve this also with #ifdef

but can't the logic be improved to not be a false positive error?

I had a different example that caused this... maybe a condition that on entry to the function sets a variable and sets another bool variable that prevents any usage of the variable if it the bool wasn't also set...

void somethinglike() { bool initialized = false; object o; /*actually a whole collection of objects*/ int some_condition = 3; if( some_condition == 3 ) { initialized = true; o = this; /* and other object inits... */ } /* ... the some time later always within a test */ if( initialized ) {

// error here: o uninitialized - at build time

Console.WriteLine( "me: " + o ); } }


again, it's not a volatile, and in this case the variable is even private to the execution context of the function, so there is no way that it can be modified outside of this context... (okay yes I could pass it as a ref, but that ref is only valid for the duration of that called function , because you can't store a reference or out )....



Viewing all articles
Browse latest Browse all 21115

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>