Random thoughts shooting out of volatile mind
Objective C – My Opinions
Objective – C is a super set of C with messaging style of Small Talk programming languages. Objective C is backbone of Apple's operating system. Cocoa and Cocoa Touch API's which are used by Macintosh OS and iPhone OS. Even though you don't need to use Apple platform to learn Objective C, GNUStep which is clone of NeXTStep based on OpenStep standard allows programming with Objective C and Cocoa based API's on either Windows or Linux platform with GCC complier. But if you want to become a full fledged programmer with Cocoa API's you need to use Mac OS X operating system (and ofcourse their hardware ;) )
Ok now to the point I was basically a programmer who worked on the Java, PHP and Python like dynamic and managed languages. But needs arised and I had to learn Objective C, as usual I learnt the basics of Objective C and started programming with Cocoa Touch API's (i.e iPhone). But within few days I learnt that Objective C is not like the languages which I've been using till now. Let me tell few things to keep in mind when moving from managed programming languages to a unmanged programming language like Objective – C(Before version 2.0. 2.0 has introduced a optional garbage collector, but for iPhone OS there is no garbage collector).
  1. What is managed and unmanaged languages?

  • Programming languages like Java is called managed language because programmer is relived of the memory management issues. To be clear programmer can allocate objects and just forget about it after that. Why I'm telling like this is there is Garbage collector which will be run by JVM periodically which will release these objects.
  • So what are PHP and Pyhon like languages? Well these are also managed + dynamic languages, meaning you don't have to declare a variable before using them. System will decide their type during runtime.
  • Comming to unmanaged languages like Objective C (Objective C 2.0 has introduced garbage collection though garbage collector is absent on iPhone OS). These languages except programmer to handle the memory management. Meaning its up to programmer to release the objects that he has allocated before program exits. Well this is were most of the managed language programmer feel difficulty while transiting to Objective C. We are always told in our engineering courses that you need to release what you allocate but nobody took it seriously that time :) and nobody told us the importance of releasing objects that time (guess lecturers themselves were not nowing the reason ;) ). But belive me when I started with Objective C I understood the reason. It is to avoid memory leaks and it becomes serious issue when your program is supI feel Objective C should be introduced as part of academics in India since it makes students to learn basics of memory management which is most important thing to know before becomming a professional programmer. And Objective C is more OO than the C++. Concepts OOP will be more clear with Objective C than C++ but point should be noted that some concepts of OOP like abstractions are not included in Objective C but I feel abstraction is not required in normal scenarios :). According to me evethough its unmanaged language Objective C is still a beautiful language out there.posed to run on limited memory devices like phones PDA's etc.
  1. Memory Leaks what are these? Ok as I mentioned in my last point in unmanaged languages not releasing the objects which you allocate will lead to memory leaks. Let me try to define it in my own terms :). OS allocates chunk of memory to the programs and expects the programs to return it back when program is done. In other words when you try to allocate an object system gives a memory reference to you (your program) and makes you (your program) its owner. Owner here means once the memory reference is passed to your program its management is upto your program including returning it back to the OS. In other words if you fail to release what you allocate that part of memory will not be touched by the OS even if your program exits (untill and unless a hard reboot occurs) meaning waste of resource and in small device ( also in normal computers ) this may lead to system crash.
  2. Releasing what is the big deal? If you are thinking like this I'll say think again :). This is another place where the Java and other language programmer get trapped while moving to Objective C. I know releasing is simple but keep in mind if you release the object in wrong place you are in big trouble. This normally happens in Objective C you release the objects thinking its not of use any more but be sure before doing so why because in case if you unknowingly refered to the same object again your program crashes showing you EXEC_BAD_ACCESS and you wont even know where this happened. You need to painful debugging to find out and some times even debugging wont resolve the issue. Thanks to the Apple for providing NSZombie and tools like Instruments, Shark and Malloc Debug which will reduce the pain for certain extent but not completely. In the sense it will give a clue on where thing is going wrong andThis normally happens in Objective C you release the objects thinking its not of use any more but be sure before doing so why because in case if you unknowingly refered to the same object again your program crashes showing you EXEC_BAD_ACCESS and you wont even know where this happened. finding and fixing it is upto you.
  3. Some more points to be remembered


    • As I mentioned you should release only those objects of which you are owner in the sense object created with init,alloc,retain etc. Never try to release them or you will corrupt memory leading to a program crash


    • If you are not able to figure out why your program is crashing use NSZombie rather than still trying to debug the program.
      • Becareful while using functions like mutableCopy you need to release the object after the function or it will lead to a leak. i.e obj1 = [[obj2 mutableCopy] autorelease]
        • Use property and synthesizers for the objects inside the class. 
        Well whatever points I mentioned here was my own personal experience. I never read the Memory Management guide from Apple but I suggest you folks to do it first. Since I had to learn things quickly I couldn't read it. And if I have told anything wrongly or there is something were I can improve my self please do comment. Your suggestions are precious for me :).
        I feel Objective C should be introduced as part of academics in India since it makes students to learn basics of memory management which is most important thing to know before becomming a professional programmer. And Objective C is more OO than the C++. Concepts OOP will be more clear with Objective C than C++ but point should be noted that some concepts of OOP like abstractions are not included in Objective C but I feel abstraction is not required in normal scenarios :). According to me evethough its unmanaged language Objective C is still a beautiful language out there.
        P.S When writing the article I was not aware of introduction of garbage collector n Objective C v2.0. So pardon me for my mistake but Objective C still remains unmanaged on the iPhone OS platform
        Posted by: copyninja on Saturday, 23 January 2010

        blog comments powered by Disqus
        Fork me on GitHub