When [pool release] causes BAD_ACCESS error

The Objective-C 2.0 introduced a garbage collector. It is nice and very simple to use.

However, I still prefer old style, release, retain, copy model. The reason is simple. You can control when to retain and release object. By doing so, you can manage memory more well. I don’t think old style is difficult to understand.

However, when you are involved in a group project in which a few programmers join, you don’t know how others write memory related codes. In such a case, garbage collection can be better.

Anyway, when making threads, you are supposed to make a dedicated memory pool for yourself. In the end of such thread function, you should issue :

[pool release];

However, it can raises BAD_ACCESS_EXE exception. Why? It is because certain objects are released too much. Especially when you deal with collections, this can happen, because Apple’s explanation when objects are retained, just referenced, or copied is not clear sometimes.

How to figure out which causes the exception and where it happens?

There are good documentation on the issue.

  1. Instruments on Leopard: How to debug those random crashes in your Cocoa app
  2. CocoaDev Debugging Autorelease
To make things short, I would like to mention that NSZombie is crucial here.

2 responses to this post.

  1. Posted by cgkim on July 31, 2008 at 1:06 AM

    2.0의 가베지 컬렉터가 제 타이밍에 제대로 돌아주지 않는다는 이야기가 주변에서 종종 있더군요. :)
    그래서 그런지 저도 아직 쓰지 않고 있습니다. 사실 가베지 컬렉터가 없어도 전혀 불편함을 느끼지 못하고 있고, 메모리 해제 타이밍을 개발자가 마음대로 조절할 수 있는 강점을 굳이 포기할 이유가 없기 때문이기도 하지요.
    팀 단위의 개발에서도 모든 개발자가 원칙만 제대로 지켜준다면 큰 문제가 아닐것 같은데요.


  2. Posted by jongampark on July 31, 2008 at 8:00 AM

    Really? I didn’t hear about it. Let me search it on the Google when I go back home.
    I doubt if it really doesn’t work in time. As you know, what is bad about garbage collector is that it needs some time to figure out what is garbage and it cleans up things periodically or if it meets certain condition. So, it is inheritantly delayed usually. I don’t know if people say that it doesn’t clean up in time still even though they consider the nature of the garbage collector. Let me search it.

    And.. yeah.. if and only if other team members follow the principle of memory management, there is no problem. But.. you know the reality, don’t you? :)
    I even have source codes in which people use the – init message to reset the object state. Conceptually, it is do-able, but in Objective-C, init is kind of part of a constructor. I would rather make reset message, because the init also “initializes” its parent class.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: