You must Sign In to post a response.
  • Category: Windows 10

    Help with stack overflow errors c# wpf application

    hi. a blind it student using the jaws for windows screen reader from http://www.freedomscientific.com and doing certificate iv programming from http://www.upskilled.edu.au. doing a simple calculator. and have run into a stack over flow. have tried looking on the net, googled, and did read about recursive functions, and also stack over flow, but could only find examples, and fixes for c # console, or windows forms, nothing for universal apps or xaml. so can any one help me out, point me to where i can find the answers, and just fix this problem. this is the only problem. and points the exception to line 266. can any one help me. any one in australia, or south east asia, as similar or same time zone. marvin.
    Ps:: pasting below.

    View Detail
    System.StackOverflowException {The runtime refused to evaluate the expression at th
    System.StackOverflowException was unhandled
    HResult=-2147023895
    InnerException:

    <Page
    x:Class="Calculator.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Calculator"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBlock HorizontalAlignment="Left" Margin="120,50,0,0" TextWrapping="Wrap" Text="Simple Calculator"
    VerticalAlignment="Top"/>
    <TextBox x:Name="Result" IsReadOnly="true" Margin="10,75,0,0"
    TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="340"/>
    <Button Content="1" HorizontalAlignment="Left" VerticalAlignment="Top"
    Margin="78,189,0,0" Width="42" x:Name="btn1" Click="btn1_Click"/>
    <Button Content="2" HorizontalAlignment="Left" VerticalAlignment="Top"
    Margin="125,189,0,0" Width="42" x:Name="btn2" Click="btn2_Click"/>
    <Button Content="3" HorizontalAlignment="Left" VerticalAlignment="Top"
    Margin="172,189,0,0" Width="42" x:Name="btn3" Click="btn3_Click"/>
    <Button Content="4" HorizontalAlignment="Left" VerticalAlignment="Top"
    Margin="78,152,0,0" Width="42" x:Name="btn4" Click="btn4_Click"/>
    <Button Content="5" HorizontalAlignment="Left" VerticalAlignment="Top"
    Margin="125,152,0,0" Width="42" x:Name="btn5" Click="btn5_Click"/>
    <Button Content="6" HorizontalAlignment="Left" VerticalAlignment="Top"
    Margin="172,152,0,0" Width="42" x:Name="btn6" Click="btn6_Click"/>
    <Button Content="7" HorizontalAlignment="Left" VerticalAlignment="Top"
    Margin="78,115,0,0" Width="42" x:Name="btn7" Click="btn7_Click"/>
    <Button Content="8" HorizontalAlignment="Left" VerticalAlignment="Top"
    Margin="125,115,0,0" Width="42" x:Name="btn8" Click="btn8_Click"/>
    <Button Content="9" HorizontalAlignment="Left" VerticalAlignment="Top"
    Margin="172,115,0,0" Width="42" x:Name="btn9" Click="btn9_Click"/>
    <Button Content="0" HorizontalAlignment="Left" VerticalAlignment="Top"
    Margin="125,226,0,0" Width="42" x:Name="btn0" Click="btn0_Click"/>
    <Button Content="+" HorizontalAlignment="Left" VerticalAlignment="Top"
    Margin="219,226,0,0" Width="42" x:Name="btnPlus" Click="btnPlus_Click"/>
    <Button Content="-" HorizontalAlignment="Left" VerticalAlignment="Top"
    Margin="219,189,0,0" Width="42" x:Name="btnMinus" Click="btnMinus_Click"/>
    <Button Content="*" HorizontalAlignment="Left" VerticalAlignment="Top"
    Margin="219,152,0,0" Width="42" x:Name="btnTimes" Click="btnTimes_Click"/>
    <Button Content="/" HorizontalAlignment="Left" VerticalAlignment="Top"
    Margin="219,115,0,0" Width="42" x:Name="btnDiv" Click="btnDiv_Click"/>
    <Button Content="=" HorizontalAlignment="Left" VerticalAlignment="Top"
    Margin="78,226,0,0" Width="42" x:Name="btnEqual" Click="btnEqual_Click"/>
    <Button Content="C" HorizontalAlignment="Left" VerticalAlignment="Top"
    Margin="172,226,0,0" Width="42" x:Name="btnClear" Click="btnClear_Click"/>
    </Grid>
    </Page>
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;

    // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

    namespace Calculator
    {
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
    public MainPage()
    {
    this.InitializeComponent();

    Result.Text = 0.ToString();
    }

    // Function to add numbers and check for a empty char and empty string.

    private void AddNumberToResult(double number)
    {
    if (char.IsNumber(Result.Text.Last()))
    {
    if (Result.Text.Length == 1 && Result.Text == "0")
    {
    Result.Text = string.Empty;
    }
    Result.Text += number;
    }
    else
    {
    if (number != 0)
    {
    Result.Text += number;
    }
    }
    }

    // Enum to add the different mathematical operations for this application.

    enum Operation { MINUS = 1, PLUS = 2, DIV = 3, TIMES = 4, NUMBER = 5 }

    // Function to add the operation and the Result to this application.
    // Tadd a substring to the Result of the text in this application.

    private void AddOperationToResult(Operation operation)
    {
    if (Result.Text.Length == 1 && Result.Text == "0")
    return;

    if (!char.IsNumber(Result.Text.Last()))
    {
    Result.Text = Result.Text.Substring(0, Result.Text.Length - 1);
    }

    // Switch statement to choose the different mathematical operations for this application.

    switch (operation)
    {
    case Operation.MINUS:
    Result.Text += "-";
    break;
    case Operation.PLUS:
    Result.Text += "+";
    break;
    case Operation.DIV:
    Result.Text += "/";
    break;
    case Operation.TIMES:
    Result.Text += "*";
    break;
    }
    }

    // Add all the number functions to the text box and the mathematical operations.
    // Then assign the Result to using the mathematical operators, to display the Result in the text box.
    // Call the Add Number To Result function in the Number click events for this application.

    private void btn1_Click(object sender, RoutedEventArgs e)
    {
    AddNumberToResult(1);
    }

    private void btn2_Click(object sender, RoutedEventArgs e)
    {
    AddNumberToResult(2);
    }

    private void btn3_Click(object sender, RoutedEventArgs e)
    {
    AddNumberToResult(3);
    }

    private void btn4_Click(object sender, RoutedEventArgs e)
    {
    AddNumberToResult(4);
    }

    private void btn5_Click(object sender, RoutedEventArgs e)
    {
    AddNumberToResult(5);
    }

    private void btn6_Click(object sender, RoutedEventArgs e)
    {
    AddNumberToResult(6);
    }

    private void btn7_Click(object sender, RoutedEventArgs e)
    {
    AddNumberToResult(7);
    }

    private void btn8_Click(object sender, RoutedEventArgs e)
    {
    AddNumberToResult(8);
    }

    private void btn9_Click(object sender, RoutedEventArgs e)
    {
    AddNumberToResult(9);
    }

    private void btn0_Click(object sender, RoutedEventArgs e)
    {
    AddNumberToResult(0);
    }

    private void btnPlus_Click(object sender, RoutedEventArgs e)
    {
    AddOperationToResult(Operation.PLUS);
    }

    private void btnMinus_Click(object sender, RoutedEventArgs e)
    {
    AddOperationToResult(Operation.MINUS);
    }

    private void btnTimes_Click(object sender, RoutedEventArgs e)
    {
    AddOperationToResult(Operation.TIMES);
    }

    private void btnDiv_Click(object sender, RoutedEventArgs e)
    {
    AddOperationToResult(Operation.DIV);
    }

    // Function to use the Equals operators and mathematical operators for this application.

    #region Equal
    private class Operand
    {
    public Operation operation = Operation.NUMBER; // Default constructor
    public double value = 0;

    public Operand left = null;
    public Operand right = null;
    }

    //Get an expression from Result.Text and build a
    // tree function.

    private Operand BuildTreeOperand()
    {
    Operand tree = null;

    string expression = Result.Text;
    if (!char.IsNumber(expression.Last()))
    {
    expression = expression.Substring(0, expression.Length - 1);
    }

    string numberStr = string.Empty;
    foreach (char c in expression.ToCharArray())
    {
    if (char.IsNumber(c) || c == '.' || numberStr == string.Empty && c ==
    '-')
    {
    numberStr += c;
    }
    else
    {
    AddOperandToTree(ref tree, new Operand()
    {
    value = double.Parse(numberStr)
    });
    numberStr = string.Empty;

    // Switch statement to show the different calculator operations for this application.

    Operation op = Operation.MINUS; // Default condition.

    switch (c)
    {
    case '-':
    op = Operation.MINUS;
    break;
    case '+':
    op = Operation.PLUS;
    break;
    case '/':
    op = Operation.DIV;
    break;
    case '*':
    op = Operation.TIMES;
    break;
    }
    AddOperandToTree(ref tree, new Operand()
    {
    operation = op
    });
    }
    }

    // Last number.


    AddOperandToTree(ref tree, new Operand()
    {
    value = double.Parse(numberStr)
    });

    return tree;
    }

    // This function will add the Operand tree and all associated objects for this application.

    private void AddOperandToTree(ref Operand tree, Operand elem)
    {
    if (tree == null)
    {
    tree = elem;
    }
    else
    {
    if (elem.operation < tree.operation)
    {
    Operand auxTree = tree;
    tree = elem;
    elem.left = tree;
    }
    else
    {
    AddOperandToTree(ref tree.right, elem); // Recursive function for the Add Operand To Tree structure.//
    }
    }
    }

    // Function to calculate the Operand tree and all objects for this application.

    private double Calc(Operand tree)
    {
    if (tree.left == null && tree.right == null) // Selects the number.
    {
    return tree.value;
    }
    else// Select an operation.
    {
    double subResult = 0;
    // Switch statement to choose the different operations for this application.
    switch (tree.operation)
    {
    case Operation.MINUS:
    subResult = Calc(tree.left) - Calc(tree.right);
    break; // Recursive function.
    case Operation.PLUS:
    subResult = Calc(tree.left) + Calc(tree.right);
    break;
    case Operation.DIV:
    subResult = Calc(tree.left) / Calc(tree.right);
    break;
    case Operation.TIMES:
    subResult = Calc(tree.left) * Calc(tree.right);
    break;
    }
    return subResult;
    }
    }

    // Function to have the Equals button, set empty string and then set up the calculations and the Results in the text box.

    private void btnEqual_Click(object sender, RoutedEventArgs e)
    {
    if (string.IsNullOrEmpty(Result.Text))
    return;

    // Build a new string tree from the Result.Text TextBox.

    Operand tree = BuildTreeOperand();

    // Evaluate tree to calculate final Result from the TextBox.

    double value = Calc(tree);

    Result.Text = value.ToString();
    }

    // Function to clear the text box and reset all values to 0 or null.

    #endregion Equal

    private void btnClear_Click(object sender, RoutedEventArgs e)
    {
    Result.Text = 0.ToString();
    }
    }
    }

    2574
  • #763735
    The stack has limited memory. It can overflow. Typically the StackOverflowException is triggered by a recursive method that creates a deep call stack. The problem is linked to the concept of the stack memory region in general.
    basically in your case when you binding tree node your goes in loop and gets this exception

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #763762
    Hai Marvin Hunkin ,
    It seems that the value is exceeding when it is calculated by using the operands.
    So you might need to keep it in BigInt which will have big number to limit and you will not get the value exceeding.
    Below is the link where you can get the work around:

    https://social.msdn.microsoft.com/Forums/en-US/4db636f6-44a5-46d9-8f0e-eef5436ae851/stack-over-flow-in-a-c-xaml-universal-application?forum=wpf

    Hope it will be helpful to you,

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #763769

    It is because, it contains too many nested method calls

    In your function somewhere it should end. If it reaches it should stop rec function.

    For ex.
    If you are trying to pars XML it in the rec function you should check whether it is last node. If last node u should stop your rec and continue to next node.

    Likewise you have to handle in your requirement.


    In your case when should the following will happen.
    Make sure the following are happening in your call.

    elem.operation < tree.operation


    By Nathan
    Direction is important than speed

  • #763772
    hi. well tried the elem.operation < tree.operation; and says the expression was invalid. still giving me stack over flow. can some one help me out, totally frustrated, and where should i put this, and some sample code, i am trying my best, but the equals is falling over, wonder if the empty string is the cause. can some one please help. frustrated, and stuck. not sure what else to try, have tried googling and googling, cannot seem to find an answer. trying my best, marvin. ps: what am i doing wrong, help me to help my self, so i can learn, not to do it, but any links, articles, code samples. really struggling. got it working 99 % but one % not working.


Sign In to post your comments