Skip to main content
More code added.; Post Made Community Wiki
Source Link
Abizern
  • 151.7k
  • 41
  • 209
  • 260

Depending on how you are using the two local variables you might need some more memory management.

The way your code reads, you are setting the local variables to the pointers returned by the two class objects. If you've written the newPlanet* classes correctly, then the string objects should be released when the classes are released. If your two local variables then try to use the pointers, you'll have problems as the objects are no longer ther

Two possible corrections:

1. Retain the strings explicitly

As the local variables are being assigned directly, you should have really retained the objects explicitly:

planet_01_Geek = [[newPlanet_01 geekName] copy];
planet_02_Geek = [[newPlanet_02 geekName] copy];

I am specifying copy here because that's the preferred way of keeping hold of objects that might by mutable, otherwise if the original changes, the local variable will also change.

2. Use properties (preferred)

This would be my preferred method: the retain, copy, or assign for the instance variables are then handled by the class.

Declare the properties correctly, i.e:

@property (nonatomic, copy) NSString *planet_01_Geek;
@property (nonatomic, copy) NSString *planet_02_Geek;

Use @synthesize in the implementation.

Then use the property syntax to allocate the variables.

self.planet_01_Geek = [newPlanet_01 geekName];
self.planet_02_Geek = [newPlanet_02 geekName];

This way the correct memory management rules will apply on assignment, and the synthesized accessor methods will also take care of releasing any object that is currently assigned to the local variables.

Edit - A note since further class details shown

In your setGeekName: method you are leaking memory. When you allocate a new pointer to the local variable, you aren't sending a release to the object that used to be in there. A better way to do it (using retain rather than copy to keep it simple:

- (void)setGeekName:(NSString *)gName {
    [gName retain]; // Hold on to the object that is passed in.
    [geekname release]; // Let go of your current object.
    geekName = gName; // Now allocate the new object.
}

Depending on how you are using the two local variables you might need some more memory management.

The way your code reads, you are setting the local variables to the pointers returned by the two class objects. If you've written the newPlanet* classes correctly, then the string objects should be released when the classes are released. If your two local variables then try to use the pointers, you'll have problems as the objects are no longer ther

Two possible corrections:

1. Retain the strings explicitly

As the local variables are being assigned directly, you should have really retained the objects explicitly:

planet_01_Geek = [[newPlanet_01 geekName] copy];
planet_02_Geek = [[newPlanet_02 geekName] copy];

I am specifying copy here because that's the preferred way of keeping hold of objects that might by mutable, otherwise if the original changes, the local variable will also change.

2. Use properties (preferred)

This would be my preferred method: the retain, copy, or assign for the instance variables are then handled by the class.

Declare the properties correctly, i.e:

@property (nonatomic, copy) NSString *planet_01_Geek;
@property (nonatomic, copy) NSString *planet_02_Geek;

Use @synthesize in the implementation.

Then use the property syntax to allocate the variables.

self.planet_01_Geek = [newPlanet_01 geekName];
self.planet_02_Geek = [newPlanet_02 geekName];

This way the correct memory management rules will apply on assignment, and the synthesized accessor methods will also take care of releasing any object that is currently assigned to the local variables.

Depending on how you are using the two local variables you might need some more memory management.

The way your code reads, you are setting the local variables to the pointers returned by the two class objects. If you've written the newPlanet* classes correctly, then the string objects should be released when the classes are released. If your two local variables then try to use the pointers, you'll have problems as the objects are no longer ther

Two possible corrections:

1. Retain the strings explicitly

As the local variables are being assigned directly, you should have really retained the objects explicitly:

planet_01_Geek = [[newPlanet_01 geekName] copy];
planet_02_Geek = [[newPlanet_02 geekName] copy];

I am specifying copy here because that's the preferred way of keeping hold of objects that might by mutable, otherwise if the original changes, the local variable will also change.

2. Use properties (preferred)

This would be my preferred method: the retain, copy, or assign for the instance variables are then handled by the class.

Declare the properties correctly, i.e:

@property (nonatomic, copy) NSString *planet_01_Geek;
@property (nonatomic, copy) NSString *planet_02_Geek;

Use @synthesize in the implementation.

Then use the property syntax to allocate the variables.

self.planet_01_Geek = [newPlanet_01 geekName];
self.planet_02_Geek = [newPlanet_02 geekName];

This way the correct memory management rules will apply on assignment, and the synthesized accessor methods will also take care of releasing any object that is currently assigned to the local variables.

Edit - A note since further class details shown

In your setGeekName: method you are leaking memory. When you allocate a new pointer to the local variable, you aren't sending a release to the object that used to be in there. A better way to do it (using retain rather than copy to keep it simple:

- (void)setGeekName:(NSString *)gName {
    [gName retain]; // Hold on to the object that is passed in.
    [geekname release]; // Let go of your current object.
    geekName = gName; // Now allocate the new object.
}
Source Link
Abizern
  • 151.7k
  • 41
  • 209
  • 260

Depending on how you are using the two local variables you might need some more memory management.

The way your code reads, you are setting the local variables to the pointers returned by the two class objects. If you've written the newPlanet* classes correctly, then the string objects should be released when the classes are released. If your two local variables then try to use the pointers, you'll have problems as the objects are no longer ther

Two possible corrections:

1. Retain the strings explicitly

As the local variables are being assigned directly, you should have really retained the objects explicitly:

planet_01_Geek = [[newPlanet_01 geekName] copy];
planet_02_Geek = [[newPlanet_02 geekName] copy];

I am specifying copy here because that's the preferred way of keeping hold of objects that might by mutable, otherwise if the original changes, the local variable will also change.

2. Use properties (preferred)

This would be my preferred method: the retain, copy, or assign for the instance variables are then handled by the class.

Declare the properties correctly, i.e:

@property (nonatomic, copy) NSString *planet_01_Geek;
@property (nonatomic, copy) NSString *planet_02_Geek;

Use @synthesize in the implementation.

Then use the property syntax to allocate the variables.

self.planet_01_Geek = [newPlanet_01 geekName];
self.planet_02_Geek = [newPlanet_02 geekName];

This way the correct memory management rules will apply on assignment, and the synthesized accessor methods will also take care of releasing any object that is currently assigned to the local variables.