Search code examples
iphoneobjective-cdealloc

Objective- C prevent NSString from being released/ dealloc


Total newbie Iphone/ Obj. C question:

I have a class called Location, in the Location.h i declare:

@interface Location : NSObject
{
     NSString *lat;
     NSString *lon;    
}

In my Location.m i have to methods:

-(void)setLatLon:(NSString*)lati:(NSString*)longi 
{
     NSLog(@"called setLatLon");
     lat = lati; 
     lon = longi; 
}

which I call on location updates from the LocationManager. Now when I try to send that out as a JSON with

-(void)sendLocation 
{
     NSDictionary *sendData = [NSDictionary dictionaryWithObjectsAndKeys:
                          imei, @"imei",
                          lat, @"lat",
                          lon, @"lon", 
                          nil];
...}

i get this error:

*** -[CFString retain]: message sent to deallocated instance 0xc05b5d0

So lat & lon seem to be deallocated. How can I prevent that or have I implemented a totally stupid 'design' here?


Solution

  • Try this:

    @interface Location : NSObject {
    
    NSString *lat;
    NSString *lon;
    
    }
    
    @property (nonatomic, strong) NSString *lon;
    @property (nonatomic, strong) NSString *lat;
    
    @implementation Location
    
    @synthesize lon;
    @synthesize lat;
    
    
    -(void)setLatLon:(NSString*)lati:(NSString*)longi
    {
        self.lat = lati;
        self.lon = longi;
    }
    

    And If you want dive deeply in ios dev, you should read follow article: Advanced Memory Management Programming Guide and The Objective-C Programming Language