How do i release allocated memory for array?












0















I am not understanding how to give weak refrence to the array or release allocated memory of array, can anyone tell me how to fix this leak?



 var menuDetails:[[String:Any]] = //this my global array object


Getting following leak even i am using ARC.



Screenshot for array memory leak!



Screen Shot



I was just scared about that memory leak,can anyone tell how do i fix it?










share|improve this question

























  • what is the size of array@sharayu

    – Vinod Kumar
    Apr 12 '17 at 12:41











  • don't know its dynamic.

    – sharayu
    Apr 12 '17 at 12:42











  • is this array have large amount of data@sharayu

    – Vinod Kumar
    Apr 12 '17 at 12:43











  • not too much like normal data... having bunch of dictionaries

    – sharayu
    Apr 12 '17 at 12:44











  • There are informative answers here, but a 3 byte leak may (a) not even be a leak and (b) may easily be a bug in Swift. Swift has numerous known leaks. bugs.swift.org/browse/… Even in ObjC, we always dealt with the fact that Foundation has known leaks. It may not be possible to get "no leaks" out of the analysis tools. 3 bytes is generally not something you would spend a lot of time on.

    – Rob Napier
    Apr 12 '17 at 16:41
















0















I am not understanding how to give weak refrence to the array or release allocated memory of array, can anyone tell me how to fix this leak?



 var menuDetails:[[String:Any]] = //this my global array object


Getting following leak even i am using ARC.



Screenshot for array memory leak!



Screen Shot



I was just scared about that memory leak,can anyone tell how do i fix it?










share|improve this question

























  • what is the size of array@sharayu

    – Vinod Kumar
    Apr 12 '17 at 12:41











  • don't know its dynamic.

    – sharayu
    Apr 12 '17 at 12:42











  • is this array have large amount of data@sharayu

    – Vinod Kumar
    Apr 12 '17 at 12:43











  • not too much like normal data... having bunch of dictionaries

    – sharayu
    Apr 12 '17 at 12:44











  • There are informative answers here, but a 3 byte leak may (a) not even be a leak and (b) may easily be a bug in Swift. Swift has numerous known leaks. bugs.swift.org/browse/… Even in ObjC, we always dealt with the fact that Foundation has known leaks. It may not be possible to get "no leaks" out of the analysis tools. 3 bytes is generally not something you would spend a lot of time on.

    – Rob Napier
    Apr 12 '17 at 16:41














0












0








0








I am not understanding how to give weak refrence to the array or release allocated memory of array, can anyone tell me how to fix this leak?



 var menuDetails:[[String:Any]] = //this my global array object


Getting following leak even i am using ARC.



Screenshot for array memory leak!



Screen Shot



I was just scared about that memory leak,can anyone tell how do i fix it?










share|improve this question
















I am not understanding how to give weak refrence to the array or release allocated memory of array, can anyone tell me how to fix this leak?



 var menuDetails:[[String:Any]] = //this my global array object


Getting following leak even i am using ARC.



Screenshot for array memory leak!



Screen Shot



I was just scared about that memory leak,can anyone tell how do i fix it?







ios xcode memory-leaks swift3






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 6:53







user10678219

















asked Apr 12 '17 at 12:35









sharayusharayu

204




204













  • what is the size of array@sharayu

    – Vinod Kumar
    Apr 12 '17 at 12:41











  • don't know its dynamic.

    – sharayu
    Apr 12 '17 at 12:42











  • is this array have large amount of data@sharayu

    – Vinod Kumar
    Apr 12 '17 at 12:43











  • not too much like normal data... having bunch of dictionaries

    – sharayu
    Apr 12 '17 at 12:44











  • There are informative answers here, but a 3 byte leak may (a) not even be a leak and (b) may easily be a bug in Swift. Swift has numerous known leaks. bugs.swift.org/browse/… Even in ObjC, we always dealt with the fact that Foundation has known leaks. It may not be possible to get "no leaks" out of the analysis tools. 3 bytes is generally not something you would spend a lot of time on.

    – Rob Napier
    Apr 12 '17 at 16:41



















  • what is the size of array@sharayu

    – Vinod Kumar
    Apr 12 '17 at 12:41











  • don't know its dynamic.

    – sharayu
    Apr 12 '17 at 12:42











  • is this array have large amount of data@sharayu

    – Vinod Kumar
    Apr 12 '17 at 12:43











  • not too much like normal data... having bunch of dictionaries

    – sharayu
    Apr 12 '17 at 12:44











  • There are informative answers here, but a 3 byte leak may (a) not even be a leak and (b) may easily be a bug in Swift. Swift has numerous known leaks. bugs.swift.org/browse/… Even in ObjC, we always dealt with the fact that Foundation has known leaks. It may not be possible to get "no leaks" out of the analysis tools. 3 bytes is generally not something you would spend a lot of time on.

    – Rob Napier
    Apr 12 '17 at 16:41

















what is the size of array@sharayu

– Vinod Kumar
Apr 12 '17 at 12:41





what is the size of array@sharayu

– Vinod Kumar
Apr 12 '17 at 12:41













don't know its dynamic.

– sharayu
Apr 12 '17 at 12:42





don't know its dynamic.

– sharayu
Apr 12 '17 at 12:42













is this array have large amount of data@sharayu

– Vinod Kumar
Apr 12 '17 at 12:43





is this array have large amount of data@sharayu

– Vinod Kumar
Apr 12 '17 at 12:43













not too much like normal data... having bunch of dictionaries

– sharayu
Apr 12 '17 at 12:44





not too much like normal data... having bunch of dictionaries

– sharayu
Apr 12 '17 at 12:44













There are informative answers here, but a 3 byte leak may (a) not even be a leak and (b) may easily be a bug in Swift. Swift has numerous known leaks. bugs.swift.org/browse/… Even in ObjC, we always dealt with the fact that Foundation has known leaks. It may not be possible to get "no leaks" out of the analysis tools. 3 bytes is generally not something you would spend a lot of time on.

– Rob Napier
Apr 12 '17 at 16:41





There are informative answers here, but a 3 byte leak may (a) not even be a leak and (b) may easily be a bug in Swift. Swift has numerous known leaks. bugs.swift.org/browse/… Even in ObjC, we always dealt with the fact that Foundation has known leaks. It may not be possible to get "no leaks" out of the analysis tools. 3 bytes is generally not something you would spend a lot of time on.

– Rob Napier
Apr 12 '17 at 16:41












3 Answers
3






active

oldest

votes


















3














You don't want to use a weak reference. If you do that your array will get released immediately.



weak var weakArray: [[String:Any]]? =



Will contain nil as soon as you create it.



Instead, you should set the array to nil (or empty) once you're done with the contents:



You could use `menuDetails.removeAll() to delete all the entries in the array, or you could change your declaration to make it an Optional



var menuDetails:[[String:Any]]? = //this my global array object


And then set it to nil when you're done with it:



menuDetails = nil





share|improve this answer
























  • that is what i was looking for ... easy stuff

    – sharayu
    Apr 13 '17 at 4:43



















0














An object will only be retained if another object has a strong reference to it. As soon as your view controller disappears, it will most likely be deallocated as well, which automatically removes its strong references to other objects. Thus, if imageArray is strongly referenced only by your disappearing view controller, the memory will automatically be released. You do not need to use an autoreleasepool.






share|improve this answer































    0














    In order to store weak references in arrays and/or dictionaries, you need an intermediate structure.



    for example:



    struct WeakRef 
    {
    weak var object:AnyObject?
    init( _ objectRef:AnyObject?)
    { object = objectRef }
    }

    // use WeakRef when you add object instances to your dictionary (or array)

    menuDetails[0]["objectKey"] = WeakRef(yourObject)

    // you will need additional code to get the actual object out of the intermediate structure
    // and given that it is a weak reference you'll also need to deal with its optionality.

    if let yourObject = (menuDetails[0]["objectKey"] as? WeakRef)?.object as? YourClass,
    {
    // ... do your thing with your object ...

    }


    The syntax could probably be made more legible by wrapping this in custom operators and generics but this is the general approach to it.






    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%2f43369716%2fhow-do-i-release-allocated-memory-for-array%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      You don't want to use a weak reference. If you do that your array will get released immediately.



      weak var weakArray: [[String:Any]]? =



      Will contain nil as soon as you create it.



      Instead, you should set the array to nil (or empty) once you're done with the contents:



      You could use `menuDetails.removeAll() to delete all the entries in the array, or you could change your declaration to make it an Optional



      var menuDetails:[[String:Any]]? = //this my global array object


      And then set it to nil when you're done with it:



      menuDetails = nil





      share|improve this answer
























      • that is what i was looking for ... easy stuff

        – sharayu
        Apr 13 '17 at 4:43
















      3














      You don't want to use a weak reference. If you do that your array will get released immediately.



      weak var weakArray: [[String:Any]]? =



      Will contain nil as soon as you create it.



      Instead, you should set the array to nil (or empty) once you're done with the contents:



      You could use `menuDetails.removeAll() to delete all the entries in the array, or you could change your declaration to make it an Optional



      var menuDetails:[[String:Any]]? = //this my global array object


      And then set it to nil when you're done with it:



      menuDetails = nil





      share|improve this answer
























      • that is what i was looking for ... easy stuff

        – sharayu
        Apr 13 '17 at 4:43














      3












      3








      3







      You don't want to use a weak reference. If you do that your array will get released immediately.



      weak var weakArray: [[String:Any]]? =



      Will contain nil as soon as you create it.



      Instead, you should set the array to nil (or empty) once you're done with the contents:



      You could use `menuDetails.removeAll() to delete all the entries in the array, or you could change your declaration to make it an Optional



      var menuDetails:[[String:Any]]? = //this my global array object


      And then set it to nil when you're done with it:



      menuDetails = nil





      share|improve this answer













      You don't want to use a weak reference. If you do that your array will get released immediately.



      weak var weakArray: [[String:Any]]? =



      Will contain nil as soon as you create it.



      Instead, you should set the array to nil (or empty) once you're done with the contents:



      You could use `menuDetails.removeAll() to delete all the entries in the array, or you could change your declaration to make it an Optional



      var menuDetails:[[String:Any]]? = //this my global array object


      And then set it to nil when you're done with it:



      menuDetails = nil






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Apr 12 '17 at 12:47









      Duncan CDuncan C

      93.8k13114201




      93.8k13114201













      • that is what i was looking for ... easy stuff

        – sharayu
        Apr 13 '17 at 4:43



















      • that is what i was looking for ... easy stuff

        – sharayu
        Apr 13 '17 at 4:43

















      that is what i was looking for ... easy stuff

      – sharayu
      Apr 13 '17 at 4:43





      that is what i was looking for ... easy stuff

      – sharayu
      Apr 13 '17 at 4:43













      0














      An object will only be retained if another object has a strong reference to it. As soon as your view controller disappears, it will most likely be deallocated as well, which automatically removes its strong references to other objects. Thus, if imageArray is strongly referenced only by your disappearing view controller, the memory will automatically be released. You do not need to use an autoreleasepool.






      share|improve this answer




























        0














        An object will only be retained if another object has a strong reference to it. As soon as your view controller disappears, it will most likely be deallocated as well, which automatically removes its strong references to other objects. Thus, if imageArray is strongly referenced only by your disappearing view controller, the memory will automatically be released. You do not need to use an autoreleasepool.






        share|improve this answer


























          0












          0








          0







          An object will only be retained if another object has a strong reference to it. As soon as your view controller disappears, it will most likely be deallocated as well, which automatically removes its strong references to other objects. Thus, if imageArray is strongly referenced only by your disappearing view controller, the memory will automatically be released. You do not need to use an autoreleasepool.






          share|improve this answer













          An object will only be retained if another object has a strong reference to it. As soon as your view controller disappears, it will most likely be deallocated as well, which automatically removes its strong references to other objects. Thus, if imageArray is strongly referenced only by your disappearing view controller, the memory will automatically be released. You do not need to use an autoreleasepool.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 12 '17 at 13:03









          Sagar BhutSagar Bhut

          461420




          461420























              0














              In order to store weak references in arrays and/or dictionaries, you need an intermediate structure.



              for example:



              struct WeakRef 
              {
              weak var object:AnyObject?
              init( _ objectRef:AnyObject?)
              { object = objectRef }
              }

              // use WeakRef when you add object instances to your dictionary (or array)

              menuDetails[0]["objectKey"] = WeakRef(yourObject)

              // you will need additional code to get the actual object out of the intermediate structure
              // and given that it is a weak reference you'll also need to deal with its optionality.

              if let yourObject = (menuDetails[0]["objectKey"] as? WeakRef)?.object as? YourClass,
              {
              // ... do your thing with your object ...

              }


              The syntax could probably be made more legible by wrapping this in custom operators and generics but this is the general approach to it.






              share|improve this answer




























                0














                In order to store weak references in arrays and/or dictionaries, you need an intermediate structure.



                for example:



                struct WeakRef 
                {
                weak var object:AnyObject?
                init( _ objectRef:AnyObject?)
                { object = objectRef }
                }

                // use WeakRef when you add object instances to your dictionary (or array)

                menuDetails[0]["objectKey"] = WeakRef(yourObject)

                // you will need additional code to get the actual object out of the intermediate structure
                // and given that it is a weak reference you'll also need to deal with its optionality.

                if let yourObject = (menuDetails[0]["objectKey"] as? WeakRef)?.object as? YourClass,
                {
                // ... do your thing with your object ...

                }


                The syntax could probably be made more legible by wrapping this in custom operators and generics but this is the general approach to it.






                share|improve this answer


























                  0












                  0








                  0







                  In order to store weak references in arrays and/or dictionaries, you need an intermediate structure.



                  for example:



                  struct WeakRef 
                  {
                  weak var object:AnyObject?
                  init( _ objectRef:AnyObject?)
                  { object = objectRef }
                  }

                  // use WeakRef when you add object instances to your dictionary (or array)

                  menuDetails[0]["objectKey"] = WeakRef(yourObject)

                  // you will need additional code to get the actual object out of the intermediate structure
                  // and given that it is a weak reference you'll also need to deal with its optionality.

                  if let yourObject = (menuDetails[0]["objectKey"] as? WeakRef)?.object as? YourClass,
                  {
                  // ... do your thing with your object ...

                  }


                  The syntax could probably be made more legible by wrapping this in custom operators and generics but this is the general approach to it.






                  share|improve this answer













                  In order to store weak references in arrays and/or dictionaries, you need an intermediate structure.



                  for example:



                  struct WeakRef 
                  {
                  weak var object:AnyObject?
                  init( _ objectRef:AnyObject?)
                  { object = objectRef }
                  }

                  // use WeakRef when you add object instances to your dictionary (or array)

                  menuDetails[0]["objectKey"] = WeakRef(yourObject)

                  // you will need additional code to get the actual object out of the intermediate structure
                  // and given that it is a weak reference you'll also need to deal with its optionality.

                  if let yourObject = (menuDetails[0]["objectKey"] as? WeakRef)?.object as? YourClass,
                  {
                  // ... do your thing with your object ...

                  }


                  The syntax could probably be made more legible by wrapping this in custom operators and generics but this is the general approach to it.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 12 '17 at 16:09









                  Alain T.Alain T.

                  8,15111328




                  8,15111328






























                      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%2f43369716%2fhow-do-i-release-allocated-memory-for-array%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

                      If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                      Alcedinidae

                      Can an atomic nucleus contain both particles and antiparticles? [duplicate]