Question about Activator.CreateInstance

yhaxyhax USMember ✭✭

Curious about the inner workings of this call:
Detail = (Page)Activator.CreateInstance (typeof(somepage));

If I have a listview that is associated with code that changes the detail (using above) every time an item is selected, am I creating a new type of some page in memory for every selection? or is the built in garbage collector smart enough to get rid of the previously created instance?

hope that makes sense. thanks!

Best Answer

Answers

  • ChristianFalchChristianFalch NODeveloper Group Leader ✭✭✭

    Activator.CreateInstance will call the constructor with the signature that matches the parameters used when calling the method, so all garbage collection and memory issues will be equal to you calling new SomePage().

  • yhaxyhax USMember ✭✭

    @ChristianFalch said:
    Activator.CreateInstance will call the constructor with the signature that matches the parameters used when calling the method, so all garbage collection and memory issues will be equal to you calling new SomePage().

    Thanks @ChristianFalch. I understand about the constructors, I'm just curious about the destructors I guess. Just wanted to clarify, if I have a list view with items A and B:

    I press A, a new instance of SomePage is called - new SomePage( some param 1)

    then I press B, a new instance of SomePage is called - new SomePage (some param 2)

    between the unloading of A and the appearing of B, all necessary destructors etc are called right? There is no copy of A on the memory stack somewhere?

  • VivekJhaveriVivekJhaveri USMember
    edited February 2016

    between the unloading of A and the appearing of B, all necessary destructors etc are called right? There is no copy of A on the memory stack somewhere?

    That is not correct. A will be removed from the view, but .net makes no guarantees about when the memory will be freed up - that's the job of the garbage collector. There are no "destructors" in .net - only finalizers (and you shouldn't ever need those IMHO.)

    Usually when people ask such a question, they want to make sure that heavyweight resources are correctly disposed. Does your page contain something that requires disposing? Also, are you familiar with IDisposable?

  • yhaxyhax USMember ✭✭
    edited February 2016

    @VivekJhaveri it doesn't have anything heavyweight per se, it was more a worry of loading up some stack that will eventually lead to a memory overflow exception on the device, somewhere down the line.

    Not that familiar with IDisposable, reading through the docs now. Thanks!

  • aeodeveloperaeodeveloper Member ✭✭

    @yhax how to use param in createinstance??
    I am new in xamarin, help me please,

    I use this to create instance

    (Page)Activator.CreateInstance (typeof(Somepage));

    but one of my Somepage class constructor using username parameter "public Somepage(string username)"

    how can I createInstance with parameter??

Sign In or Register to comment.