Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Array recognized as CFString

Array recognized as CFString
Thread Tools
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Aug 4, 2001, 04:10 PM
 
I have an NSMutableArray which is initialized as follows:
NSMutableArray *array; //in header file
array = [NSMutableArray array]; //in applicationWillFinishLaunching

and then it gets this error message later on in another method:
Aug 04 17:02:01 Zipper[463] *** -[NSCFString addObject:]: selector not recognized
Aug 04 17:02:01 Zipper[463] *** -[NSCFString addObject:]: selector not recognized

when this code runs
[array addObject:aFile]; //aFile is a plain NSString

What's causing the error message?
     
Forum Regular
Join Date: Aug 2001
Location: Vienna, Austria
Status: Offline
Reply With Quote
Aug 4, 2001, 06:33 PM
 
Don't use [NSMutableArray array]. Use:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
- (<font color = purple>id</font>)init {
<font color = green>if</font>(<font color = purple>self</font>=[<font color = purple>super</font> init]) {
array=[[NSMutableArray alloc] init];
}
<font color = green>return</font> <font color = purple>self</font>;
}
- (<font color = green>void</font>)dealloc {
[array release];
}
</font>[/code]

[NSMutableArray array] adds the object to the autorelease pool, which means that it will get deallocated earlier than you expected.
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
Aug 4, 2001, 11:04 PM
 
Right, if you want to have the array as an ivar you need to hold a retain on it, and release it in -dealloc, making sure it stays around while your instance stays around. Just change [NSMutableArray array] to [[NSMutableArray array] retain] or [[NSMutableArray alloc] init] (the +alloc implies a retain count of 1).

And when you override -dealloc, make sure to call [super dealloc] at the end ;-)

In the current code, just so you know why that particular error message happened, the array was autoreleased immediately, which means it was dealloced at the end of the event. You still had a pointer to it, but the memory was freed, and an NSString just so happened to be allocated in that spot in memory. If you see methods being sent to the utterly wrong class, this is virtually always the reason why it happens. If there was some other random memory that was allocated there, you would have gotten a segfault or bus error.
     
davecom  (op)
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Aug 4, 2001, 11:52 PM
 
Thanks, as usual, for all the help. Progress continues...
     
Forum Regular
Join Date: Aug 2001
Location: Vienna, Austria
Status: Offline
Reply With Quote
Aug 6, 2001, 07:09 AM
 
Originally posted by lindberg:
<STRONG>And when you override -dealloc, make sure to call [super dealloc] at the end ;-)</STRONG>
Oops. I tend to forget to call super. I think I have to check my latest code for that bug...
Thanks!
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 11:22 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2