none
Creating an object seems to fail if run fast or something is not like in theory. Can anyone explain please? RRS feed

  • Question

  • Creating an object seems to fail if run fast or something is not like in theory. Can anyone explain? Edit
    Can anyone help or give an advice please?

    Creating a new object seems to be problematic:

    In my example, The NewObjectClass includes C# wrapper code and dlls from C++ which I execute in a WPF window where I try to create an instance of the NewObjectClass.


    Since ikeynum is never null and aax is never null when it enters the

    dictProcessedThingsInprogres.Add(ikeynum, aax);   //** here I get a null reference exception in debugger

    I do not understand what happens. Scope of a variable in a function is by definition given by the context of the function(when function ends, variable should end).

    If I call the function 5 times and I define variables and Objects in the function 5 times, it should not happen.

    Can anyone please explain why I get a null reference exception on the line with stars and how to avoid it?


    My function is a thread function which is called in each thread:

    I try to create an object in a thread function which is called as a thread function.


    This I try to fill in the threadfunction:

    private Dictionary<int, NewObjectClass> dictProcessedThingsInprogres = new Dictionary<int, NewObjectClass>();

    .. I call the thread function

    Thread a = new Thread(processMeThreadFunc);

               a.Start(sendobject);


    ... here is the function

    public void processMeThreadFunc(object fromdictfilesandstates1Rowinf)

    {


    filesandstates process = (filesandstates) fromdictfilesandstates1Rowinf;

    int ikeynum = process.number;


      NewObjectClass aax;

        aax = new NewObjectClass();

        if(aax!=null) {

       

            dictProcessedThingsInprogres.Add(ikeynum, aax);   //** here I get a null reference exception in debugger

            aax = null;

        }

        else

        {

            countnulls++;

            return;  // ostanu v dictionary nespracovane, pozriet, akoby nestihalo vytvarat pdmarkarey preto ma instance = null

        }



    }

    ---------------------------------

    And here is the exception:

    System.NullReferenceException occurred

      HResult=-2147467261

      Message=Object
    reference not set to an instance of an object.

      Source=mscorlib

      StackTrace:

           at
    System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean
    add)

           at
    EncodingThreadingTest.MainWindow.processEncodingNew(Object
    fromdictfilesandstates1Rowinf) in
    C:\...\zterm\Prenasaniena64bit_postupne\ProcessingThreadingTest\MainWindow.xaml.cs:line
    223

      InnerException:
    Thursday, December 6, 2012 9:20 AM

Answers

All replies

  • Creating an object seems to fail if run fast or something is not like in theory. Can anyone explain? Edit
    Can anyone help or give an advice please?

    Creating a new object seems to be problematic:

    In my example, The NewObjectClass includes C# wrapper code and dlls from C++ which I execute in a WPF window where I try to create an instance of the NewObjectClass.


    Since ikeynum is never null and aax is never null when it enters the

    dictProcessedThingsInprogres.Add(ikeynum, aax);   //** here I get a null reference exception in debugger

    I do not understand what happens. Scope of a variable in a function is by definition given by the context of the function(when function ends, variable should end).

    If I call the function 5 times and I define variables and Objects in the function 5 times, it should not happen.

    Can anyone please explain why I get a null reference exception on the line with stars and how to avoid it?


    My function is a thread function which is called in each thread:

    I try to create an object in a thread function which is called as a thread function.


    This I try to fill in the threadfunction:

    private Dictionary<int, NewObjectClass> dictProcessedThingsInprogres = new Dictionary<int, NewObjectClass>();

    .. I call the thread function

    Thread a = new Thread(processMeThreadFunc);

               a.Start(sendobject);


    ... here is the function

    public void processMeThreadFunc(object fromdictfilesandstates1Rowinf)

    {


    filesandstates process = (filesandstates) fromdictfilesandstates1Rowinf;

    int ikeynum = process.number;


      NewObjectClass aax;

        aax = new NewObjectClass();

        if(aax!=null) {

       

            dictProcessedThingsInprogres.Add(ikeynum, aax);   //** here I get a null reference exception in debugger

            aax = null;

        }

        else

        {

            countnulls++;

            return;  // ostanu v dictionary nespracovane, pozriet, akoby nestihalo vytvarat pdmarkarey preto ma instance = null

        }



    }

    ---------------------------------

    And here is the exception:

    System.NullReferenceException occurred

      HResult=-2147467261

      Message=Object
    reference not set to an instance of an object.

      Source=mscorlib

      StackTrace:

           at
    System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean
    add)

           at
    EncodingThreadingTest.MainWindow.processEncodingNew(Object
    fromdictfilesandstates1Rowinf) in
    C:\...\zterm\Prenasaniena64bit_postupne\ProcessingThreadingTest\MainWindow.xaml.cs:line
    223

      InnerException:
    Thursday, December 6, 2012 9:17 AM
  • My function is a thread function which is called in each thread:

    I try to create an object in a thread function which is called as a thread function.

    You are undoubtedly encountering a reace-condition between your various threads. If you put your code fragments in code blocks so I can read them conveniently, I will look at it to try and find the error.

    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    We are here to learn, to share knowledge, and to earn points; all in about equal measure.

    Thursday, December 6, 2012 9:33 AM
  • My function is a thread function which is called in each thread:

    I try to create an object in a thread function which is called as a thread function.

    You are undoubtedly encountering a reace-condition between your various threads. If you put your code fragments in code blocks so I can read them conveniently, I will look at it to try and find the error.

    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    We are here to learn, to share knowledge, and to earn points; all in about equal measure.


    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    We are here to learn, to share knowledge, and to earn points; all in about equal measure.

    Thursday, December 6, 2012 10:18 AM
  • Hi Peter,

    Welcome to the MSDN Forum.

    I agree with Pieter, to avoid this issue, please try: 

    you can try this class: http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx 

    Before your work, call WaitOne first: http://msdn.microsoft.com/en-us/library/58195swd.aspx 

    When you finish it, call set at last: http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.set.aspx 

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Friday, December 7, 2012 10:01 AM
    Moderator