OutOfMemoryException when creating a TaskService with wrong password

Topics: Errors
Apr 11, 2014 at 1:47 PM
Edited Apr 11, 2014 at 1:58 PM
Hello,

recently I get an OutOfMemoryException when I instantiate an object of TaskService (giving a wrong password) and my windows account got blocked (I cannot login anymore) :/

In a first call I get "Access Denied" .. in a subsequent call I get "OutOfMemoryException"

Call:
            TaskService ts = new TaskService(
                Credentials.ServerName,
                Credentials.Username,
                Credentials.AccountDomain,
                Credentials.Password);
Exception:

System.OutOfMemoryException
bei Microsoft.Win32.TaskScheduler.V2Interop.TaskSchedulerClass.Connect(Object serverName, Object user, Object domain, Object password)
bei Microsoft.Win32.TaskScheduler.TaskService.Connect()
bei Microsoft.Win32.TaskScheduler.TaskService.EndInit()
bei Microsoft.Win32.TaskScheduler.TaskService..ctor(String targetServer, String userName, String accountDomain, String password, Boolean forceV1)


Any idea?


Thanks!

MuhaNERD
Apr 14, 2014 at 7:13 AM
I see that someone else had the same problem long time ago (https://taskscheduler.codeplex.com/workitem/8839). But I don't find a solution for it? Any help please?

p.s.
I noticed that this error happens when "Credentials.ServerName" is the local machine. When I test the case (i.e., providing wrong password) on a remote server I got always "Access Denied" (which is what I expect also on the local machine). The question is: why there is a difference between executing it on local vs. remote machine?
Coordinator
Apr 14, 2014 at 4:22 PM
What values are you providing as parameters and what version of Windows is the local machine? OutOfMemoryException usually means there is an infinite loop somewhere. I don't see any in this section of my code so I'm wondering if this may be a Microsoft issue. If it is, then maybe I can prevent the scenario in my code.
Apr 15, 2014 at 5:55 AM
It happens on Window 8.1 Pro.

I wrote a small test WPF application. Here is the code (used v4.0 of Task Scheduler Managed Wrapper):

MainWindows.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.TxtServer.Text = Environment.MachineName;
        this.TxtUsername.Text = string.Format("{0}\\{1}", Environment.UserDomainName, Environment.UserName);
    }

    private void BtnOk_OnClick(object sender, RoutedEventArgs e)
    {
        string server = this.TxtServer.Text;

        string[] domainUsername = this.TxtUsername.Text.Split('\\');
        string domain = string.Empty, username = string.Empty;
        if (domainUsername.Any())
        {
            if (domainUsername.Length == 2)
            {
                domain = domainUsername[0];
                username = domainUsername[1];
            }
        }

        string password = this.TxtPassword.Password;
        try
        {
            TaskService ts = new TaskService(server, username, domain, password);
            TaskDefinition td = ts.NewTask();
        }
        catch (Exception ex)
        {
            MessageBox.Show(string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
        }
    }

    private void BtnCancel_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }
}

MainWindow.xaml

<Window x:Class="TaskSchedulerDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:ignore="http://www.ignore.com"

    mc:Ignorable="d ignore"
    SizeToContent="WidthAndHeight"
    WindowStartupLocation="CenterScreen"
    ResizeMode="NoResize"
    Title="Task Scheduler Demo"
    >
<StackPanel Margin="10">
    <StackPanel Margin="5,0,0,5">
        <Label Content="Enter username and password"/>
    </StackPanel>
    <StackPanel Margin="10, 0, 0, 0" Orientation="Horizontal">
        <StackPanel  Width="100">
            <Label Content="Server:" Width="170" Height="30" Margin="-5, 0, 0, 0"/>
            <Label Content="Username:" Width="170" Height="30" Margin="-5, 5, 0, 0"/>
            <Label Content="Password:"  Width="170" Height="30" Margin="-5, 5, 0, 0"/>
        </StackPanel>
        <StackPanel Width="250" HorizontalAlignment="Center">
            <TextBox   x:Name="TxtServer" TextWrapping="Wrap" Width="250" Height="23" Margin="0, 5, 0, 0" />
            <TextBox   x:Name="TxtUsername" TextWrapping="Wrap" Width="250" Height="23" Margin="0, 10, 0, 0"/>
            <PasswordBox   x:Name="TxtPassword" Width="250" Height="23" Margin="0, 10, 0, 0"/>
        </StackPanel>
    </StackPanel>
    <Grid  Margin="0, 10, 0, 0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="110"/>
            <ColumnDefinition SharedSizeGroup="Buttons"/>
            <ColumnDefinition Width="5"/>
            <ColumnDefinition SharedSizeGroup="Buttons"/>
        </Grid.ColumnDefinitions>
        <Button Grid.Column="1" x:Name="BtnOk" Content="OK" Click="BtnOk_OnClick" IsDefault="True"/>
        <Button Grid.Column="3" x:Name="BtnCancel" Content="Cancel" Click="BtnCancel_Click" IsCancel="True"/>
    </Grid>
</StackPanel>
</Window>
Apr 16, 2014 at 7:10 AM
Have you tried it? Did you find anything that explains the problem?
Coordinator
Apr 16, 2014 at 2:03 PM
I believe that since you are pulling the server name from a TextBox, if blank the value will be String.Empty. This may be causing the problem. I have updated the code so that String.Empty values are changed to null. However, until that code is released, try this check:
string server = this.TxtServer.Text;
if (String.Empty == server)
   server = null;
Apr 17, 2014 at 6:52 AM
Well giving "server = null" has solved the problem, but not because it was empty. I have always provided a server name even if I am working with my local computer. Apparently I had to give "null" if the "server == computer_name". Maybe you should fix this in your code in this way:

if (server.equals(Environment.MachineName)){
server = null;
}

I must admit that I find the Execption I got because of this issue a weird one :/


Thanks anyway for your help! :)
Coordinator
Apr 17, 2014 at 4:06 PM
I have updated the code in 2.1.1 to avoid this problem. The oddly termed exception is actually coming from the Microsoft system library. It must be a bug there. Anyway, thanks to your help, users of this library should never encounter it again.
Marked as answer by dahall on 9/1/2015 at 10:08 AM