Computer Science Canada Objective C Memory Management |
Author: | DtY [ Sun Aug 09, 2009 6:47 pm ] |
Post subject: | Objective C Memory Management |
I've been learning objective C for iphone development, but the book I'm reading assumes you already know objective C. I already know C, so I read a bit about obj. C, and I think I'm comfortable with the message system (or whatever they call it). Now, something that I'm having trouble with is the memory management, as I understand it, all objects use reference counting, the reference count is increased if you send it the retain message, and decreased if you call the release method (and free()ed if the reference count drops to zero). Is that all right? Also, what does NS mean, like NSString and NSArray? Is that part of Objective C, or is that an Apple addon? [edit] And all the datatypes from C (int, char, struct, etc.) are not objects, right? |
Author: | wtd [ Sun Aug 09, 2009 7:01 pm ] |
Post subject: | RE:Objective C Memory Management |
NS is shorthand for NeXTSTEP. Apple bought NeXT and OpenStep (which evolved from NeXTSTEP) became the foundations for Mac OS X. You're quite right on how retain and release work. Also see retainCount, and "free" actually frees the memory. You should also look into autorelease pools. You can still use int and such, but it's preferable to use the typedef'd Obj-C equivalents, like NSInteger. |
Author: | DtY [ Sun Aug 09, 2009 7:52 pm ] |
Post subject: | RE:Objective C Memory Management |
The book I'm reading briefly mentioned auto release pools, but suggested you avoid them on the iPhone because of severely limited memory. I get the impression that once in a while the garbage collector is started and checks if an object that should be auto released still has references to it, and frees the memory if it doesn't? Like Java works (I think). So the NS___ types are specific to Apple's compiler, or would they work if compiled with GCC instead? Thanks ![]() |
Author: | andrew. [ Sun Aug 09, 2009 8:12 pm ] |
Post subject: | RE:Objective C Memory Management |
I think the NS types are specific to the Apple compiler. I may be incorrect though. |
Author: | wtd [ Sun Aug 09, 2009 8:44 pm ] |
Post subject: | RE:Objective C Memory Management |
There is nothing about them that ties them to Apple's compiler. However, they are part of Apple's libraries. |
Author: | rdrake [ Sun Aug 09, 2009 9:31 pm ] | ||
Post subject: | Re: RE:Objective C Memory Management | ||
As wtd mentioned, reference counting works that way. As I recall the iPhone does not have a garbage collector, you must deallocate your own objects. andrew. @ Sun Aug 09, 2009 8:12 pm wrote: I think the NS types are specific to the Apple compiler. I may be incorrect though. There are other implementations (GNUstep) out there that provide somewhat equivalent implementations. Also, Apple's compiler is GCC right now. I think they're going for Clang + LLVM later, but anyway... for the curious, Clang + LLVM will compile Objective-C code just fine, you just need the correct libraries installed. On Linux this isn't possible unfortunately due to incomplete implementations.
Well, the only problem I've really had with Clang + LLVM was with Objective-C 2.0-style properties. GNUstep lacks the correct code in order to pass the messages it generates. Apple's site includes a rather large Objective-C programming guide, as well as guides on iPhone memory management. Also check out the Stanford CS139p lectures, available on iTunes, which include a lecture on memory management for the iPhone as well among other topics. Oh, and to add onto what wtd said about the C primitive types... you can use them as he said, there also exists typedefs to each of them (NSFloat, NSInteger, etc.). They are certainly not objects and you will do nasty things if you treat them as such. For example:
You will get a random value as %@ is for an object which i is not. The framework usually has an equivalent to the primitive types anyway. Check out classes such as NSNumber. |
Author: | DtY [ Mon Aug 10, 2009 11:16 am ] |
Post subject: | RE:Objective C Memory Management |
Okay, thanks. That %@ makes more sense now, I was wondering why it didn't use printf style formatting. |
Author: | andrew. [ Mon Aug 10, 2009 12:53 pm ] |
Post subject: | RE:Objective C Memory Management |
Guess I was wrong. |
Author: | wtd [ Mon Aug 10, 2009 5:23 pm ] |
Post subject: | RE:Objective C Memory Management |
Happens to the best of us from time to time. |
Author: | Joel92 [ Mon Aug 10, 2009 8:08 pm ] |
Post subject: | Re: Objective C Memory Management |
Yeah, sadly the iPhone OS doesn't support garbage collection (N) but I guess it's good practice doing manual memory management. |
Author: | DtY [ Tue Aug 11, 2009 6:34 pm ] |
Post subject: | RE:Objective C Memory Management |
I'm having a problem with the interface builder, I'm following along with the book, and so far the project hasn't changed the code yet, only used IB to put two text boxes in, and change a couple of the settings, and it says to press build and run to test it out. It starts up okay, but when I click on the text box, which the text says should just open up a keyboard, nothing happens. Anyone know what the problem might be? |
Author: | rdrake [ Tue Aug 11, 2009 10:39 pm ] |
Post subject: | RE:Objective C Memory Management |
No. Check the properties of the text box and make sure you selected the type of keyboard to pop up. Oh wait, did you wire up the text box to anything in the code? Otherwise it might just sit there and respond to nothing. |
Author: | DtY [ Wed Aug 12, 2009 9:23 am ] |
Post subject: | RE:Objective C Memory Management |
There are two text boxes, one I didn't change the keyboard type, and one that's set to numpad. Do I have to make it respond to some code? The book says it should pop up as it is. I was thinking that might be the problem, since you have to write code to release the keyboard, it doesn't really made sense to be able to get the keyboard up without code. Do you know what I need to do to make it open? [edit] I restarted from the beginning of the chapter, and the text boxes work now. I guess I did something wrong, though I'm not sure what, I went through the inspector thoroughly at least twice. |
Author: | Joel92 [ Sat Aug 15, 2009 2:03 pm ] |
Post subject: | Re: Objective C Memory Management |
@OP what book are you using? |
Author: | DtY [ Sat Aug 15, 2009 5:50 pm ] |
Post subject: | RE:Objective C Memory Management |
"Beginning iPhone Development: Exploring the iPhone SDK" by Dave Mark & Jeff LaMarche, published by Apress. [edit] Since I have this thread open already, can I have some input on this layout? ![]() It's an RPN calculator, and all the buttons are working (except the trig ones floating out to the right), I was thinking of putting a pi button under the +/-, and log somewhere. There's a lot of extra screen space though, what should I do with it? Expand the buttons maybe? Also, how do you do log in RPN, do you push the base, then the number, or what? |
Author: | rdrake [ Sat Aug 15, 2009 8:37 pm ] |
Post subject: | Re: RE:Objective C Memory Management |
DtY @ Sat Aug 15, 2009 5:50 pm wrote: Expand the buttons maybe? Yes. Big fingers won't be able to hit those buttons. |
Author: | wtd [ Sat Aug 15, 2009 9:57 pm ] |
Post subject: | Re: RE:Objective C Memory Management |
rdrake @ Sun Aug 16, 2009 9:37 am wrote: DtY @ Sat Aug 15, 2009 5:50 pm wrote: Expand the buttons maybe? Yes. Big fingers won't be able to hit those buttons.This. Also, switch color schemes. White text on a black background, please. |
Author: | DtY [ Sat Aug 15, 2009 10:19 pm ] |
Post subject: | Re: RE:Objective C Memory Management |
wtd @ Sat Aug 15, 2009 9:57 pm wrote: rdrake @ Sun Aug 16, 2009 9:37 am wrote: DtY @ Sat Aug 15, 2009 5:50 pm wrote: Expand the buttons maybe? Yes. Big fingers won't be able to hit those buttons.This. Also, switch color schemes. White text on a black background, please. Is that supposed to make it look like an HP? I hate white on black, so there's no chance of that happening. And okay, I'll make the buttons bigger |
Author: | wtd [ Sun Aug 16, 2009 10:04 am ] |
Post subject: | Re: RE:Objective C Memory Management |
[quote="DtY @ Sun Aug 16, 2009 11:19 am"] wtd @ Sat Aug 15, 2009 9:57 pm wrote: rdrake @ Sun Aug 16, 2009 9:37 am wrote: Also, switch color schemes. White text on a black background, please.
Is that supposed to make it look like an HP? I hate white on black, so there's no chance of that happening. No, silly. It's supposed to make it look more at home on an iPhone. |
Author: | Joel92 [ Sun Aug 16, 2009 1:43 pm ] |
Post subject: | Re: RE:Objective C Memory Management |
DtY @ Sat Aug 15, 2009 5:50 pm wrote: "Beginning iPhone Development: Exploring the iPhone SDK" by Dave Mark & Jeff LaMarche, published by Apress.
Ah right. How much Objective-C knowledge does it assume you to know? Just the basic message syntax. or anything else? |
Author: | DtY [ Sun Aug 16, 2009 2:51 pm ] |
Post subject: | Re: RE:Objective C Memory Management |
Joel92 @ Sun Aug 16, 2009 1:43 pm wrote: DtY @ Sat Aug 15, 2009 5:50 pm wrote: "Beginning iPhone Development: Exploring the iPhone SDK" by Dave Mark & Jeff LaMarche, published by Apress.
Ah right. How much Objective-C knowledge does it assume you to know? Just the basic message syntax. or anything else? It's kinda back and forth, it assumes you know basic objective C, but not that you've used Cocoa before, and went in depth explaining @property/@synthesize, and some other (what seems to me at least to be) simple objective C syntax. The message syntax was really the only concept that hasn't been convered |
Author: | Joel92 [ Sun Aug 16, 2009 9:09 pm ] |
Post subject: | Re: RE:Objective C Memory Management |
DtY @ Sun Aug 16, 2009 2:51 pm wrote: Joel92 @ Sun Aug 16, 2009 1:43 pm wrote: DtY @ Sat Aug 15, 2009 5:50 pm wrote: "Beginning iPhone Development: Exploring the iPhone SDK" by Dave Mark & Jeff LaMarche, published by Apress.
Ah right. How much Objective-C knowledge does it assume you to know? Just the basic message syntax. or anything else? It's kinda back and forth, it assumes you know basic objective C, but not that you've used Cocoa before, and went in depth explaining @property/@synthesize, and some other (what seems to me at least to be) simple objective C syntax. The message syntax was really the only concept that hasn't been convered Alright, pretty basic stuff. I'm reading Kochan's book right now and eventually I'll move onto "Beginning iPhone Development" I'd like to get a pretty solid understanding of ObjC |