PDA

View Full Version : error: statically allocated instance of Objective-C class 'NSString'




NSNick
Jul 1, 2008, 08:00 PM
What does "error: statically allocated instance of Objective-C class 'NSString'" mean?

Also, what does "warning local declaration of 'newData' hides instance variable" mean?



lee1210
Jul 1, 2008, 08:20 PM
The first one i'll let someone else handle. Posting your code might help.

The second one means that there is a variable that is either globally scoped, a class member, or a function variable and you've redeclared that variable in a lower scope. For example, you have a global variable newData, and in a function you declare another variable named newData. C and Objective-C, as far as I know, doesn't have a scope resolution operator like C++'s ::. As such, you will not be able to access the higher scoped variable with this name in the scope that the new variable is declared. You probably accidentally put the type before the variable in a function when you wanted to use it, etc.

If you truly want a new variable at the lower scope it would be best to pick a different name.

-Lee

Catfish_Man
Jul 2, 2008, 12:32 AM
The first one just means you said NSString foo instead of NSString *foo

sujithkrishnan
Jul 2, 2008, 01:46 AM
second one means...

U may declared a myVariable as property or global... then again u are declaring the same thing locally...(just rename the second declaration or remove that declrtn.)

NSNick
Jul 2, 2008, 08:59 PM
The purpose of my code is to have an NSTextField, button, and an NSTableView. When the button is pressed a new cell is created in the table and the data from the textfield is added into the cell.

#import <Cocoa/Cocoa.h>
#import "ItemData.h"

@interface AppController : NSObject {
IBOutlet NSTableView *myTable; // Outlet to the table view, connected in IB.
NSMutableArray *items; // An array of AppointmentData objects.
}

- (IBAction)addItem:(id)sender;
- (IBAction)removeSelectedItem:(id)sender;


@end


#import "AppController.h"



@implementation AppController

- (void)dealloc {
[items release];
items = nil;
[super dealloc];
}

- (void)awakeFromNib {
// Create the storage our table will use.
items = [[NSMutableArray alloc] init];

// Be the data source...
[myTable setDataSource:self];




// Let the text in the @"Info" column wrap.
[[[myTable tableColumnWithIdentifier:@"Info"] dataCell] setWraps:YES];

// Start with atleast one item.
[self addItem:nil];
}

// ---------------------------------------------------------
// Action methods
// ---------------------------------------------------------



- (void)addItem:(id)sender {
// Append a newly created data object, then reload the table contents.
ItemData *itemData = [[ItemData alloc] init];
[items addObject: itemData];
[itemData release];
[myTable reloadData];
}

- (void)removeSelectedItem:(id)sender {
// Remove the selected row from the data set, then reload the table contents.
[items removeObjectAtIndex: [myTable selectedRow]];
[myTable reloadData];
}

// ---------------------------------------------------------
// Data source methods
// ---------------------------------------------------------

- (int)numberOfRowsInTableView:(NSTableView *)tv {
return [items count];
}

- (id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tc row:(int)row {
return [[items objectAtIndex:row] info];
}

- (void)tableView:(NSTableView *)tv setObjectValue:(id)objectValue forTableColumn:(NSTableColumn *)tc row:(int)row {

[[items objectAtIndex:row] setInfo: objectValue];
}

@end


#import <Foundation/Foundation.h>

@interface ItemData : NSObject {
IBOutlet NSTextField *newDataField;
NSString *info;
NSString *newData;
}

- (void)setInfo:(NSString *)newInfo;
- (NSString *)info;

@end


#import "ItemData.h"



@implementation ItemData

- (id)init {
self = [super init];
if (self) {
NSString *newData = @"%@", newDataField;
[self setInfo: newData];
}
return self;
}

- (void)dealloc {
[info release];
info = nil;
[super dealloc];
}

- (void)setInfo:(NSString *)newInfo {
if (info != newInfo) {
[info release];
info = [newInfo copy];
}
}

- (NSString *)info {
return [[info retain] autorelease];
}

@end

NSNick
Jul 2, 2008, 09:00 PM
The error occurs after this line
NSString *newData = @"%@", newDataField;
The warning occurs after this line
[self setInfo: newData];

Krevnik
Jul 2, 2008, 10:20 PM
Two things:

1) Put your code in code tags. Otherwise it is nearly impossible to read correctly.

2) Two, this line isn't exactly valid:

NSString *newData = @"%@", newDataField;

The problem is that you can't create strings like that. Try this:

NSString *newData = [NSString stringWithFormat:@"%@",[newDataField stringValue]];

And maybe rename newData. As the second poster said, it hides the object variable of the same name. Pretty much means the compiler doesn't know for sure which one you mean.

Edit: I just realized what you were actually trying to do... tweaked...

NSNick
Jul 4, 2008, 01:07 PM
I have made the suggested changes and now there are no errors or warnings; however, now when I click Add, it adds a row that says (null) instead of the data that was typed into the textfield.

therevolution
Jul 4, 2008, 01:37 PM
- (void)setInfo:(NSString *)newInfo {
if (info != newInfo) {
[info release];
info = [newInfo copy];
}
}


I don't know if this is the entire problem, but this section seems off. Notice that if info is not equal to newInfo, nothing happens. You'll never be able to set a new value.

What you want is more like this. It's overcommented, I know, but it's to explain what each statement is doing.


- (void)setInfo:(NSString *)newInfo {
// If values are the same, do nothing.
// Use isEqualToString, not ==, as you should care about the strings'
// values, not whether they are the same object.
if ([info isEqualToString:newInfo]) {
return;
}

// If info is not nil, release it.
if (info != nil)
[info release];

// Now assign the value to info, and be sure to retain it
// or else it will get autoreleased.
info = [[newInfo copy] retain];
}

NSNick
Jul 4, 2008, 02:28 PM
It doesn't seem to behave any differently with the new code.

NSNick
Jul 4, 2008, 05:54 PM
When I change ItemData.m to look like


#import "ItemData.h"



@implementation ItemData

- (id)init {
self = [super init];
if (self) {
//Creates now row with data in text field
[self setInfo:[NSString stringWithFormat:@"%@",[newDataField stringValue]]];
}
NSLog(@"newDataField %@", newDataField);
NSLog(@"newData %@", newData);
return self;
}

- (void)dealloc {
[info release];
info = nil;
[super dealloc];
}

//setInfo method for the initialization part of ItemData
- (void)setInfo:(NSString *)newInfo {
if (info != newInfo) {
[info release];
info = [newInfo copy];
}
}

- (NSString *)info {
return [[info retain] autorelease];
}

@end


The debugger tells me that both newDataField and NewData are (null) even when there is data in the text field.

I also know that the new row will not display (null) if the line

[self setInfo: NSLocalizedString(@"<Your Appointment Info>", @"String to display for unset appointment info")];

is used instead of

[self setInfo:[NSString stringWithFormat:@"%@",[newDataField stringValue]]];

In that case it will display "<Your Appointment Info>"

Any thoughts??

manjot01synergy
Aug 18, 2010, 04:24 AM
it gives warning alert hides instance variables

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Fortune Cookie" message:@"know your fortune,crack your cookie now" delegate:self cancelButtonTitle:@"No thanks" otherButtonTitles:@"crack the cookie",nil];
[alert show];
[alert release];
please tell how to fix this warning

gnasher729
Aug 18, 2010, 10:24 AM
What does "error: statically allocated instance of Objective-C class 'NSString'" mean?

Also, what does "warning local declaration of 'newData' hides instance variable" mean?

Example:

int multiply (int x, int y) {
int result = 0;
if (x >= 0 && y >= 0) { int result = x * y; }
return result;
}

Try guessing what multiply (3, 4) will return.

And you can't have variables or members (local, static or extern) of type NSString, only variables and members of type NSString*.


please tell how to fix this warning

Look in the @interface what instance variables you have. Then look what local variables you have. Notice anything?

Good strategy is to use names for instance variables that can be clearly distinguished from other names. Makes your code a lot, lot more readable as well.

gnasher729
Aug 18, 2010, 10:32 AM
The error occurs after this line
NSString *newData = @"%@", newDataField;
The warning occurs after this line
[self setInfo: newData];

A non Objective-C example:

char *p1, p2;

What _exactly_ does this declaration do? (It should be obvious that it doesn't do what you think it does or I wouldn't have posted it, but what _does_ it mean? ). Now apply that knowledge to your example. You may try

char *p1 = "Hello", p2 = ", world";

and the compiler might give you a useful hint.

Peter Maurer
Aug 18, 2010, 11:20 AM
This is borderline off-topic, but just in case some memory management novices are trying to learn from discussions like this one...


- (void)setInfo:(NSString *)newInfo {
if (info != newInfo) {
[info release];
info = [newInfo copy];
}
}


I don't know if this is the entire problem, but this section seems off.

Looks like a perfectly fine run-of-the-mill setter to me. I do, however, think the alternative setter you're suggesting contains a memory leak in the following lines:


if (info != nil) // this check isn't really necessary -- [nil release] is a perfectly valid no-op
[info release]; // releasing once
info = [[newInfo copy] retain]; // retaining twice


You're releasing once, but retaining twice (once implicitly in -copy, then once more explicitly). If this actually represents the setters you're using in your projects, you should review your code some time soon. :)

(I'd also suggest switching to properties whenever possible. After all, the safest way to avoid memory leaks is not even writing code that's prone to leaking in the first place.)

--

EDIT: Yikes! I fell for an incredibly old thread.