4 Replies - 2170 Views - Last Post: 03 January 2012 - 07:32 PM Rate Topic: -----

#1 RandomlyKnighted   User is offline

  • D.I.C Lover
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,384
  • Joined: 14-January 10

Environment Variable Throws Stack Overflow Exception

Posted 03 January 2012 - 04:08 PM

I'm working on project and for some reason Visual Studio is saying: "An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll" when I get an environment variable.

public partial class Form1 : Form
    {
        // Declare and initilize variable
        public SerialPort sp;
        string driveLetter = System.Environment.GetEnvironmentVariable("SystemDrive");
        beginTelemetry begin = new beginTelemetry();
        plotData plot = new plotData();

        #region Form1

        public Form1()
        {
            InitializeComponent();
            comPortComboBox.Items.AddRange(SerialPort.GetPortNames());
        }

        #endregion

        #region File Menu



The error occurs on the line where I declare driveLetter. It's not in a loop and it's not being used recursively. So why is it getting a Stack Overflow Exception? Am I overlooking something?

Is This A Good Question/Topic? 0
  • +

Replies To: Environment Variable Throws Stack Overflow Exception

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Environment Variable Throws Stack Overflow Exception

Posted 03 January 2012 - 04:30 PM

I just copy/pasted your code into a fresh project and it works fine

    public partial class MainForm : Form
    {
        #region Constructors
        string bootDrive = System.Environment.GetEnvironmentVariable("SystemDrive");


        public MainForm()
        {
            InitializeComponent();
            MessageBox.Show(bootDrive);
        }



This post has been edited by tlhIn`toq: 03 January 2012 - 04:34 PM

Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Environment Variable Throws Stack Overflow Exception

Posted 03 January 2012 - 04:42 PM

Stack overflow is almost always an infinate loop.

if you put a breakpoint on this line are you sure it doesn't keep getting called?


Reboot the PC?

Try the same code in a fresh project?

YOu have some odd naming practices going on here

06 beginTelemetry begin = new beginTelemetry();

Data types aren't supposed be named as verbs. That's for methods. But beginTelemetry is a type or class. It would be more readable and more conventional for this to be more like

Telemetry telemetry = new Telemetry();
telemetry.Begin();// Call the begin method of the instance telemetry


Same with plotData as a class name. That's just very non-standard and very odd.

Its nice to see someone using #regions.

If you haven't seen it yet, there is a nice FREE tool called nArrange that you should try. It will clean up your C# into #regions for you. Just google "nArrange" and you'll be lead to it.

Attached Image
Was This Post Helpful? 1
  • +
  • -

#4 RexGrammer   User is offline

  • Coding Dynamo
  • member icon

Reputation: 183
  • View blog
  • Posts: 785
  • Joined: 27-October 11

Re: Environment Variable Throws Stack Overflow Exception

Posted 03 January 2012 - 04:45 PM

The only reason I can think of is the error being machine specific to your machine. Since it worked on my PC and tlhIn`toq's. Maybe your main drive doesn't have a label (yeah, a WILD guess, but there's no other reason to it)
Was This Post Helpful? 0
  • +
  • -

#5 RandomlyKnighted   User is offline

  • D.I.C Lover
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,384
  • Joined: 14-January 10

Re: Environment Variable Throws Stack Overflow Exception

Posted 03 January 2012 - 07:32 PM

Turns out it was being called more than once. My code in another class was forcing it into a never ending loop with that line. I simply changed the line of code in the other file causing it to loop and now the problem is solved. It works perfectly now!

Thanks everyone!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1