2

I made an array full of objects of a class. I followed the instructions here (How do I declare an array of weak references in Swift?) to make a wrapper and make every object in the array a weak reference. However my app still crashes after memory warning due to the memory filling up. Why aren't objects deallocating when not needed?

Additional info, I am using the class to hold images and filters associated with the image, and a collection view to display the images. After scrolling through too many images, the app will crash.

Community
  • 1
  • 1
Trevor Jordy
  • 598
  • 1
  • 7
  • 27
  • 3
    Never use array to store a lot of image, your app WILL crash, instead, just store the name of the images to array and just dump the images in your app's documents folder and get it with the name in your array while scrolling, its not about strong or weak but you store image in array mean it will persist in RAM that will lead to crash – Tj3n Nov 07 '16 at 05:44
  • Weak referencing does not mean that the object will be deallocated when not needed. A object only gets deallocted when its reference count reaches zero. Weak reference means you essentially have a reference to that object but you are not affecting its reference count. And considering the fact that your app is crashing something is holding a strong reference to the objects inside the array. – Windindi Nov 07 '16 at 06:34
  • @Tj3n what do you mean by "get it with the name in your array while scrolling?" My images are in the assets folder, and are assigned the the class instances using named: "" at the moment. Are you saying they should not be associated with the class? And load the images using my collection view methods? – Trevor Jordy Nov 07 '16 at 06:50
  • 1
    @TrevorJordy Does your array holding `UIImage` inside it? If yes, then get rid of it, if not, the memory issue maybe because you dont reuse cell but create new one everytime – Tj3n Nov 07 '16 at 07:25
  • @Tj3n I am reusing cells. It does not directly have UIImages inside of it, but the objects it contains have a var for uiimage. The class that is used to create each object in the array contains fields for names and UIImage. – Trevor Jordy Nov 07 '16 at 07:29
  • 1
    It means you are having UIImage inside of the object that are inside the array? You should remove it and let the cell get it's image when it's running cellForRow, dont need to hold the images in the array – Tj3n Nov 07 '16 at 07:36
  • @Tj3n alright, let me run this by you before I try it. I'm going to change my UIImage field in my class to be a string that holds the name of the image instead, and then in my cellForItemAt method that I'm using I'll set my cell image to UIImage( named : ~insert the image name string here ~) – Trevor Jordy Nov 07 '16 at 08:01
  • 1
    Yes :D try it and see if the problem gone or not – Tj3n Nov 07 '16 at 08:07
  • @Tj3n holy moly it works I love you man. I've been asking about this for months and finally someone knows the answer! Thanks so much! :D – Trevor Jordy Nov 07 '16 at 08:43

0 Answers0