1

To prevent a cross-threading exception I have the following static method:

public static void SetText(System.Windows.Forms.Form form, System.Windows.Forms.Control ctrl, string text)
{
   if (ctrl.InvokeRequired)
   {
      SetTextCallback d = new SetTextCallback(SetText);
      form.Invoke(d, new object[] { form, ctrl, text });
   }
   else
   {
      ctrl.Text = text;
   }
}

coming from this answer

Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on

It works solid but I was wondering: The form object contains all kinds of controls etc but passing the object around does not cost extra RAM or such as it seems.

How come?

Isn't there any difference in usage of RAM if;

  • I'd execute this method in the form class itself

or

  • I'd pass the entire form object to this method in a different class?

EDIT

I was confused due to an article I had read persueing not to pass the Windows.Form object (can't find the article) so I automatically linked this with possible high usage of RAM passing objects.

MwBakker
  • 498
  • 5
  • 18
  • Its that as big that will it take 1MB per control. And yes, you have to do it per control. – Willy David Jr Oct 05 '17 at 09:57
  • Sorry I don't fully understand your comment – MwBakker Oct 05 '17 at 09:59
  • 1
    It seems you are confused as to how "passing objects around" works. Usually, forms are passed by reference, not by value. [See this answer](https://stackoverflow.com/a/430958/2099119) for a much better explanation of what that means, and mare sure to read [this excellent blog post](http://jonskeet.uk/csharp/references.html) as well. – waka Oct 05 '17 at 10:09
  • Yes you are correct I was a little confused, due to a read on an article persueing not to pass Windows.Form as an object. Since then I thought this was also linked with extra RAM usage (why I also linked this I do not know) – MwBakker Oct 05 '17 at 10:13
  • 1
    @MwBakker: Also make sure to [read this article by Lee Richardson](http://www.leerichardson.com/2007/01/parameter-passing-in-c.html), it sure did help to clear up my confusion which I had about the same topic. :) – waka Oct 05 '17 at 10:15
  • Thank you, that looks like a good read ! – MwBakker Oct 05 '17 at 10:18
  • What if I set a property in a class instead of passing parameter in method, is this set property then still a referrence? – MwBakker Oct 06 '17 at 07:17

2 Answers2

2

It is possible to have a memory leak when the form closes but the other thread is still executing and holding a reference. If you're worried about threads holding references to forms or controls after the user has closed the form, consider using WeakReference in the non-UI thread:

https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/weak-references https://msdn.microsoft.com/en-us/library/system.weakreference(v=vs.110).aspx

It signals to the virtual machine that you want access to the object but if the garbage collector wants to collect it, it can. The responsibility falls to you to make sure the form has not been collected.

2

Form is passed by reference. See https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/reference-types

Markus
  • 2,184
  • 2
  • 22
  • 32
  • So therefor there is no extra memory required during the 'pass', because it's just a referrence? – MwBakker Oct 05 '17 at 10:04
  • 1
    Well, only the "pointer" uses memory, which is 32 Bit on 32 Bit processes and 64 Bit on 64 Bit processes... – Markus Oct 05 '17 at 10:05
  • So every object passed as parameter is not an object shift itself, but 'just' a pointer, am I correct? Or is this just the case with large-containment objects like a Windows.Form – MwBakker Oct 05 '17 at 10:08
  • 2
    Yes, every reference type is passed by reference per default in C#. – Markus Oct 05 '17 at 10:11
  • Got it, thank you. As said in the comments I was confused due to an article I had read persueing not to use Windows.Form as an object (can't find the article) so I automatically linked this with possible high usage of RAM. – MwBakker Oct 05 '17 at 10:16
  • What if I set a property in a class instead of passing parameter in method, is this set property then still a referrence? – MwBakker Oct 05 '17 at 12:59