提问者:小点点

尝试调用单例方法时System.StackOverflowException


我正在用C#WPF开发一个小型密码生成器。 我有一个Log类,它有将消息记录到文件中的方法。 它一直运行良好,直到我添加日志时,无线电检查。 我的日志类:

public sealed class Log : MainWindow
    {
        private static Log instance = null;
        private static readonly object padlock = new object();
        public string Date;

        public bool isStarted;

        Log()
        {
            isStarted = false;
        }

        public static Log Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new Log();
                    }
                    return instance;
                }
            }
        }

        public void ActivityLog(string message, bool start)
        {
            if(isStarted)
            {
                Date = DateTime.Now.ToString("dd'/'MM'/'yyyy HH:mm:ss");

                StreamWriter sw = new StreamWriter("activity.log", true, Encoding.ASCII);
                if (start)
                    sw.WriteAsync($"\n[{Date}] {message}\n");
                else
                    sw.WriteAsync($"[{Date}] {message}\n");

                sw.Close();
            }     
        }

        public void PasswordLog(string message)
        {
            Date = DateTime.Now.ToString("dd'/'MM'/'yyyy HH:mm:ss");
            StreamWriter sw = new StreamWriter("passwords.log", true, Encoding.ASCII);
            sw.WriteAsync($"[{Date}] {message}\n");
            sw.Close();
        }

    }

下面是导致异常的代码(如果我注释了这段代码,它可以正常工作)

private void radioChecked(object sender, RoutedEventArgs e)
        {
            RadioButton radio = (RadioButton)sender;
            switch (radio.Name)
            {
                case "startUpper":
                    Log.Instance.ActivityLog("Start with set to uppercase character", false);
                    break;
                case "startLower":
                    Log.Instance.ActivityLog("Start with set to lowercase character", false);
                    break;
                case "startNumber":
                    Log.Instance.ActivityLog("Start with set to number", false);
                    break;
                case "startSpecial":
                    Log.Instance.ActivityLog("Start with set to special character", false);
                    break;
                case "startDefault":
                    Log.Instance.ActivityLog("Start with set to default", false);
                    break;
            }
        }

还有这些单选按钮的xaml

<Grid Margin="0,75,0,0" HorizontalAlignment="Right" VerticalAlignment="Center" Height="190" Width="180">
            <Label x:Name="startWithLabel" Content="Start with:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,0,0,0" FontWeight="Normal" FontSize="25" FontFamily="{StaticResource COPRGT}" Width="170" RenderTransformOrigin="0.5,0.5"/>
            <RadioButton Checked="radioChecked" x:Name="startUpper" Content="Uppercase letter" HorizontalAlignment="Left" Margin="20,50,0,0" VerticalAlignment="Top" FontFamily="{StaticResource COPRGT}"/>
            <RadioButton Checked="radioChecked" x:Name="startLower" Content="Lowercase letter" HorizontalAlignment="Left" Margin="20,80,0,0" VerticalAlignment="Top" FontFamily="{StaticResource COPRGT}"/>
            <RadioButton Checked="radioChecked" x:Name="startNumber" Content="Number" HorizontalAlignment="Left" Margin="20,110,0,0" VerticalAlignment="Top" FontFamily="{StaticResource COPRGT}"/>
            <RadioButton Checked="radioChecked" x:Name="startSpecial" Content="Special character" HorizontalAlignment="Left" Margin="20,140,0,0" VerticalAlignment="Top" FontFamily="{StaticResource COPRGT}"/>
            <RadioButton Checked="radioChecked" x:Name="startDefault" Content="Default" IsChecked="True" HorizontalAlignment="Left" Margin="20,170,0,0" VerticalAlignment="Top" FontFamily="{StaticResource COPRGT}"/>
        </Grid>

当我试图编译时,它在窗口初始化时引发异常initializeComponent();


共1个答案

匿名用户

堆栈溢出通常是由无限递归引起的。 (方法A调用方法B,方法B调用方法A等)

这只是在没有看到堆栈跟踪的情况下的胡乱猜测,但我的猜测是这样的:

public sealed class Log : MainWindow

它为什么从MainWindow继承? 这意味着当创建新的log时,MainWindow中的所有代码都在运行。 如果radiochecked位于mainwindow中,那么这很可能是导致递归的原因。