 |
 |
a few newbie questions: Cocoa & Obj C
|
 |
|
 |
|
Professional Poster
Join Date: Feb 2001
Status:
Offline
|
|
I have a few stupid questions, about Objective C & Cocoa.
1. Why would function arguments get mixed up? I have one object which has an id to another object. The first object calls a method in the second object with lots of arguments. And the arguments are getting mixed up -- I have checked this by NSLogging immediately before the function call and immediately upon entering the function. If I call the same function from within the second class, the arguments don't get mixed up. My experience is in Java, when this could never happen, so I don't know what to do. I don't think there are any memory leaks, though.
2. How do I get multithreading to work? I tried just throwing off a new NSThread, giving it the selector of the method to run. This worked but it gave me all sorts of memory errors, for example about any @"strings". So at the beginning of the thread method I allocated a new memory pool but the @"strings" still weren't being collected. Is there some trick to getting a memory pool up and running?
Thanks for any help.
|
|
The 4 o'clock train will be a bus.
It will depart at 20 minutes to 5.
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status:
Offline
|
|
Originally posted by tie:
1. Why would function arguments get mixed up? I have one object which has an id to another object. The first object calls a method in the second object with lots of arguments. And the arguments are getting mixed up -- I have checked this by NSLogging immediately before the function call and immediately upon entering the function. If I call the same function from within the second class, the arguments don't get mixed up.
The only way I can think this can happen is if you didn't #import the header for the second class from the first class' .m file (or didn't declare the method in question in the second class' header file). This has the potential to do strange things to float and double arguments -- this is directly inherited from C.
I assume you're ignoring a compiler warning too -- if so, don't do that. Many warnings in ObjC are errors in Java, so pay very close attention to any warnings the compiler gives you. Most ObjC developers, once they get comfortable with the language anyway, make sure their code compiles without any warnings, so that when one comes up they know it's something that needs to be looked at.
Also, it's better to explicity declare the reference to the second class like "SecondClass *varible", not just "id variable", as the compiler can do better type checking for you. Use id when the variable really could potentially be a reference to different kinds of classes.
2. How do I get multithreading to work? I tried just throwing off a new NSThread, giving it the selector of the method to run. This worked but it gave me all sorts of memory errors, for example about any @"strings". So at the beginning of the thread method I allocated a new memory pool but the @"strings" still weren't being collected. Is there some trick to getting a memory pool up and running?
You have to release the NSAutoreleasePool instance to have it flush. Basically, when you do your own pool, any objects autoreleased between the [[NSAutoreleasePool alloc] init] go into that pool, and you have to dealloc that pool (by calling -release on it) to get everything to flush. If you have a long-running thread that has its own little run loop, then I'd recommend releasing the pool and allocating the other one every so often (every iteration, or every few iterations, etc).
Constant strings of the form @"string" are never deallocated -- the memory for them is embedded in the binary program image in static storage. They are acutally a special subclass of NSString to ensure they're not deallocated.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Feb 2001
Status:
Offline
|
|
Thanks for the hint about function arguments. I had been using an id mostly to avoid circular import dependencies (which wouldn't compile even with #ifdef lines in the headers, which have always worked for me in C). But I discovered the @class specifier which fixes it. And with the static typing, the parameter passing errors disappeared.
Somehow, multithreading seems to be working. I don't know why it had been giving me errors about deallocating @"strings" because of course they aren't supposed to be released. Of course, I have a thread deadlock problem now, but fixing that is a piece of cake compared to figuring out the idiosyncrasies of Obj C. 
|
|
The 4 o'clock train will be a bus.
It will depart at 20 minutes to 5.
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Feb 2001
Status:
Offline
|
|
Okay, this is sort of ridiculous. Everything was working perfectly. Then I compiled again and now it is interpreting all my NSColor *objects as NSCFStrings (whatever those are  ). Needless to say, I get about a million errors. I'm getting sick of Objective C. Why is it so crazy?!
[EDIT: Well, it's working again.  ]
[This message has been edited by tie (edited 04-20-2001).]
|
|
The 4 o'clock train will be a bus.
It will depart at 20 minutes to 5.
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status:
Offline
|
|
When you change one source code file, the C/ObjC compiler does not automatically recompile any other file that depends on the one you change the way the Java compiler does. Thus, it's far more likely to have the compiled classes be out of sync with each other.
When bizarre impossible things start happening, that's a signal to "clean" the project (i.e. delete all the compiled files -- the button with a broom) and recompile everything from scratch.
When using Makefiles, there was a way to build a full dependency tree ("make depend"), though this was largely broken in the pre-MacOS X ObjC environments. I have no clue how to do something similar using the new Project Builder and its underlying jamfiles.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|