First, my background: a lot of my recent experience comes from C#.NET. I skipped the whole C++ ship, and was a pretty decent C programmer back in the day.
I'm trying to make sense of the whole retain/release/autorelease thing in Obj-C. I just don't get it. My C is kind of rusty, but it might be helpful to talk about retain/release/autorelease in terms of C.
In C, you can create a string like this:
which I rarely used, but I THINK that syntax does a malloc(sizeof(char) * strlen("Hello World")) and points hello to that memory location, right?
Or you can do it like this:
And of course, later, you want to do free(hello) if you used malloc().
That's about as much as I understand with memory allocation in C. Now, Obj-C makes it more complex, I think. For instance, what the heck does init do? I mean what is it REALLY doing behind the scenes? But that's another topic I suppose. Here are my real questions about retain/release:
1) How the heck do I know when something needs to be released? In other words, when does something returned by a statement actually have a retain count of at least 1? Take the below code snippet:
Right after those statements, what is the retainCount of a, b, c, and d? Or maybe a better question is, how do I know which one of those needs to be released manually? And do I need to just do [x release] or [x release]; x = nil; ?
I guess I'll stop here before getting into autorelease..one thing at a time. One quick question (unrelated), about delegates: they're kind of like events in C# and JavaScript, right? onMouseOver, onMouseOut, onChange, etc. Once I assign an object's delegate to another object, then I can access those special event functions..correct?
I'm trying to make sense of the whole retain/release/autorelease thing in Obj-C. I just don't get it. My C is kind of rusty, but it might be helpful to talk about retain/release/autorelease in terms of C.
In C, you can create a string like this:
Code:
char *hello = "Hello World";
which I rarely used, but I THINK that syntax does a malloc(sizeof(char) * strlen("Hello World")) and points hello to that memory location, right?
Or you can do it like this:
Code:
char *hello;
hello = (char *)malloc(sizeof(char) * 512);
hello = "This can hold up to 512 bytes";
And of course, later, you want to do free(hello) if you used malloc().
That's about as much as I understand with memory allocation in C. Now, Obj-C makes it more complex, I think. For instance, what the heck does init do? I mean what is it REALLY doing behind the scenes? But that's another topic I suppose. Here are my real questions about retain/release:
1) How the heck do I know when something needs to be released? In other words, when does something returned by a statement actually have a retain count of at least 1? Take the below code snippet:
Code:
NSString *a = @"Hi there";
NSString *b = [NSString stringWithString:@"Hi there"];
NSString *c = [NSString new]; //I know that new = alloc + init
NSString *d; [d initWithString:@"Hi there"]; //I don't have a compiler handy now, this might be invalid
Right after those statements, what is the retainCount of a, b, c, and d? Or maybe a better question is, how do I know which one of those needs to be released manually? And do I need to just do [x release] or [x release]; x = nil; ?
I guess I'll stop here before getting into autorelease..one thing at a time. One quick question (unrelated), about delegates: they're kind of like events in C# and JavaScript, right? onMouseOver, onMouseOut, onChange, etc. Once I assign an object's delegate to another object, then I can access those special event functions..correct?