Swift - How to find all instances of a class, and all references that point to the same instance?










3















I inherited an application where certain class is instantiated and passed back and forth multiple times. So I have about 20 private and public variables where



  1. Class instantiates: myClass = MyClass()


  2. MyClass instance is passed back and forth: self.myClass = someOtherClass.myClass

  3. Sometimes variable is passed from class to class multiple times

  4. And a class may create a new instance or receive an instance from some other class in various cases

I want to fix this. But before changing anything I want to understand how many instances of that class I have, and which references point to the same instance.



What I do now: I am running the following statement in each method of MyClass:



print(Unmanaged.passUnretained(self).toOpaque())


and then additional prints in callers to identify who called that instance. This is quite tedious, but moreover it completely depends on my ability to cover all possible cases if this class usage at runtime, and it won't find nil references that classes may pass to each other (and which I need to know of)



So is there a better way? Or can this method be improved in some way?



Thanks in advance.










share|improve this question






















  • Instances are tracked with Instruments (Allocations). References you might track with Memory Graph. But neither of those is going to instrument your code live in the way you are probably hoping.

    – matt
    Nov 13 '18 at 20:14











  • By "fix this" do you mean you want to turn it from a reference type to a value type? Is the app actually broken and you are trying to track down a bug or are you just refactoring?

    – Daniel T.
    Nov 13 '18 at 21:10















3















I inherited an application where certain class is instantiated and passed back and forth multiple times. So I have about 20 private and public variables where



  1. Class instantiates: myClass = MyClass()


  2. MyClass instance is passed back and forth: self.myClass = someOtherClass.myClass

  3. Sometimes variable is passed from class to class multiple times

  4. And a class may create a new instance or receive an instance from some other class in various cases

I want to fix this. But before changing anything I want to understand how many instances of that class I have, and which references point to the same instance.



What I do now: I am running the following statement in each method of MyClass:



print(Unmanaged.passUnretained(self).toOpaque())


and then additional prints in callers to identify who called that instance. This is quite tedious, but moreover it completely depends on my ability to cover all possible cases if this class usage at runtime, and it won't find nil references that classes may pass to each other (and which I need to know of)



So is there a better way? Or can this method be improved in some way?



Thanks in advance.










share|improve this question






















  • Instances are tracked with Instruments (Allocations). References you might track with Memory Graph. But neither of those is going to instrument your code live in the way you are probably hoping.

    – matt
    Nov 13 '18 at 20:14











  • By "fix this" do you mean you want to turn it from a reference type to a value type? Is the app actually broken and you are trying to track down a bug or are you just refactoring?

    – Daniel T.
    Nov 13 '18 at 21:10













3












3








3








I inherited an application where certain class is instantiated and passed back and forth multiple times. So I have about 20 private and public variables where



  1. Class instantiates: myClass = MyClass()


  2. MyClass instance is passed back and forth: self.myClass = someOtherClass.myClass

  3. Sometimes variable is passed from class to class multiple times

  4. And a class may create a new instance or receive an instance from some other class in various cases

I want to fix this. But before changing anything I want to understand how many instances of that class I have, and which references point to the same instance.



What I do now: I am running the following statement in each method of MyClass:



print(Unmanaged.passUnretained(self).toOpaque())


and then additional prints in callers to identify who called that instance. This is quite tedious, but moreover it completely depends on my ability to cover all possible cases if this class usage at runtime, and it won't find nil references that classes may pass to each other (and which I need to know of)



So is there a better way? Or can this method be improved in some way?



Thanks in advance.










share|improve this question














I inherited an application where certain class is instantiated and passed back and forth multiple times. So I have about 20 private and public variables where



  1. Class instantiates: myClass = MyClass()


  2. MyClass instance is passed back and forth: self.myClass = someOtherClass.myClass

  3. Sometimes variable is passed from class to class multiple times

  4. And a class may create a new instance or receive an instance from some other class in various cases

I want to fix this. But before changing anything I want to understand how many instances of that class I have, and which references point to the same instance.



What I do now: I am running the following statement in each method of MyClass:



print(Unmanaged.passUnretained(self).toOpaque())


and then additional prints in callers to identify who called that instance. This is quite tedious, but moreover it completely depends on my ability to cover all possible cases if this class usage at runtime, and it won't find nil references that classes may pass to each other (and which I need to know of)



So is there a better way? Or can this method be improved in some way?



Thanks in advance.







ios swift






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 20:04









Kiril S.Kiril S.

5,64452043




5,64452043












  • Instances are tracked with Instruments (Allocations). References you might track with Memory Graph. But neither of those is going to instrument your code live in the way you are probably hoping.

    – matt
    Nov 13 '18 at 20:14











  • By "fix this" do you mean you want to turn it from a reference type to a value type? Is the app actually broken and you are trying to track down a bug or are you just refactoring?

    – Daniel T.
    Nov 13 '18 at 21:10

















  • Instances are tracked with Instruments (Allocations). References you might track with Memory Graph. But neither of those is going to instrument your code live in the way you are probably hoping.

    – matt
    Nov 13 '18 at 20:14











  • By "fix this" do you mean you want to turn it from a reference type to a value type? Is the app actually broken and you are trying to track down a bug or are you just refactoring?

    – Daniel T.
    Nov 13 '18 at 21:10
















Instances are tracked with Instruments (Allocations). References you might track with Memory Graph. But neither of those is going to instrument your code live in the way you are probably hoping.

– matt
Nov 13 '18 at 20:14





Instances are tracked with Instruments (Allocations). References you might track with Memory Graph. But neither of those is going to instrument your code live in the way you are probably hoping.

– matt
Nov 13 '18 at 20:14













By "fix this" do you mean you want to turn it from a reference type to a value type? Is the app actually broken and you are trying to track down a bug or are you just refactoring?

– Daniel T.
Nov 13 '18 at 21:10





By "fix this" do you mean you want to turn it from a reference type to a value type? Is the app actually broken and you are trying to track down a bug or are you just refactoring?

– Daniel T.
Nov 13 '18 at 21:10












1 Answer
1






active

oldest

votes


















0














Sounds simple but why not add a static variable to the class in question and increment it in the init method?



This way you’ll have a definite count of the number of instances.



(Decrement it in the deinit() of course)



Failing that you could have a “global” variable type MyClass array in your appDelegate.



In your MyClass init, get a reference to your delegate, and have it add itself to the array.



Use weak references or decrement in Deinit to avoid double counting for dirty reassignment.



This way dead instances are released rather than being retained by the array.



This way you should have a count and list of instances.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288680%2fswift-how-to-find-all-instances-of-a-class-and-all-references-that-point-to-t%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Sounds simple but why not add a static variable to the class in question and increment it in the init method?



    This way you’ll have a definite count of the number of instances.



    (Decrement it in the deinit() of course)



    Failing that you could have a “global” variable type MyClass array in your appDelegate.



    In your MyClass init, get a reference to your delegate, and have it add itself to the array.



    Use weak references or decrement in Deinit to avoid double counting for dirty reassignment.



    This way dead instances are released rather than being retained by the array.



    This way you should have a count and list of instances.






    share|improve this answer





























      0














      Sounds simple but why not add a static variable to the class in question and increment it in the init method?



      This way you’ll have a definite count of the number of instances.



      (Decrement it in the deinit() of course)



      Failing that you could have a “global” variable type MyClass array in your appDelegate.



      In your MyClass init, get a reference to your delegate, and have it add itself to the array.



      Use weak references or decrement in Deinit to avoid double counting for dirty reassignment.



      This way dead instances are released rather than being retained by the array.



      This way you should have a count and list of instances.






      share|improve this answer



























        0












        0








        0







        Sounds simple but why not add a static variable to the class in question and increment it in the init method?



        This way you’ll have a definite count of the number of instances.



        (Decrement it in the deinit() of course)



        Failing that you could have a “global” variable type MyClass array in your appDelegate.



        In your MyClass init, get a reference to your delegate, and have it add itself to the array.



        Use weak references or decrement in Deinit to avoid double counting for dirty reassignment.



        This way dead instances are released rather than being retained by the array.



        This way you should have a count and list of instances.






        share|improve this answer















        Sounds simple but why not add a static variable to the class in question and increment it in the init method?



        This way you’ll have a definite count of the number of instances.



        (Decrement it in the deinit() of course)



        Failing that you could have a “global” variable type MyClass array in your appDelegate.



        In your MyClass init, get a reference to your delegate, and have it add itself to the array.



        Use weak references or decrement in Deinit to avoid double counting for dirty reassignment.



        This way dead instances are released rather than being retained by the array.



        This way you should have a count and list of instances.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 21:03

























        answered Nov 13 '18 at 20:49









        WoodstockWoodstock

        12.6k1261100




        12.6k1261100



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288680%2fswift-how-to-find-all-instances-of-a-class-and-all-references-that-point-to-t%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線