-1

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?

Mehul Mistri
  • 14,844
  • 14
  • 66
  • 94
fweigl
  • 19,982
  • 18
  • 97
  • 188

3 Answers3

2

You should declare retain/strong properties for the iVars you're trying to use. I'm not sure how you're storing the "imei", but the lat and lon are being deallocated because you're not retaining then. Try something like:

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

@property (retain) NSString *lat;
@property (retain) NSString *lon;

and on the .m file:

@synthesize lat, lon;

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

and

-(void)sendLocation 
{
     NSDictionary *sendData = [NSDictionary dictionaryWithObjectsAndKeys:
                          imei, @"imei", //dunno what is this
                          self.lat, @"lat",
                          self.lon, @"lon", 
                          nil];
...}
Mehul Mistri
  • 14,844
  • 14
  • 66
  • 94
Bruno Koga
  • 3,836
  • 2
  • 29
  • 44
  • but, of course. If you're having this kind of issue, you should study the iOS Memory Management stuff to fully understand what is happening here... – Bruno Koga Aug 14 '12 at 12:59
  • Thanks you! I'll be reading up on all that stuff, but right now i just needed a quick fix to keep me going ;) – fweigl Aug 14 '12 at 13:05
2

You could add properties as other responses have suggested and as you start moving forward to work on more advanced projects I definitely agree with that approach, especially if you want to take advantage of ARC and other time-saving and error-avoiding features of Objective-C.

Aside from that, however, you can fix your code as it is currently written with a minimal change. Change this:

lat = lati;
lon = longi;

to this:

 lat = [lati retain]; 
 lon = [longi retain]; 

This will cause your code to retain the references it has to these variables. IF you do this approach, you will want to make sure to release your variables when you are done, usually in a dealloc method. If you don't take care of that, you will be creating memory leaks.

Tim Dean
  • 7,971
  • 2
  • 25
  • 58
1

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

tikhop
  • 1,934
  • 13
  • 29
  • That did it, thanks a lot. It didnt work without the self. reference, coming from Java i supposed it being something like "this", is that completely wrong? – fweigl Aug 14 '12 at 13:03