Domanda

Recently I was facing an issue where I was navigating from a screen A to screen B. when I was coming back from screen B to screen A, the Live Bytes in the application were not returning to the initial value. After further investigation I found out that I was retaining some global objects in some methods which were called more than once. So I had to fix the calling mechanism of the method.

I fixed the issue, but I was thinking about one alternate solution. What if I simply used a for loop in dealloc which runs depending on the value of retain count. I think it is not advisable to use such approach, but what is the exact problem in this approach if I am sure that the objects are not accessed from anywhere outside the file.

Thanks in advance.

È stato utile?

Soluzione

What if I simply used a for loop in dealloc which runs depending on the value of retain count.

I wouldn't be surprised if Xcode detects code like that and energizes the aluminum case of your MacBook Pro with several amps.

I think it is not advisable to use such approach, but what is the exact problem in this approach if I am sure that the objects are not accessed from anywhere outside the file.

You're right -- not advisable. There are at least two problems:

  1. It completely breaks the memory management paradigm of Objective-C. You really can't be sure that no other object has retained one of your objects. Just one example: you don't know in your -dealloc method whether any of the objects to which your ivars refer might have been autoreleased.

  2. It's the wrong fix. Doing what you propose doesn't fix bugs in your code, it only covers them up. Your objects should correctly manage the objects that they use, and not worry about what other objects may or may not have retained. If you follow that simple formula, you don't have to worry about whether objects are accessed from "outside the file" or not -- everything just works.

Not only should you not use -retainCount to run the number of retains down to 0, you shouldn't look at -retainCount at all.

Altri suggerimenti

Retain count is not for you to count on. There are some internal implementations which increase/decrease the retain count without you know it so using it is not advisable.

You should use the xcode instruments for finding memory leaks which will lead you to places in your code where objects are retained and not released.

or you can just enable ARC and let it manage the memory for you.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top