user2017285
user2017285

Reputation: 283

calling method from another class using thread ios

I have two classes one is Sample class and ServerConnection class.

sample.m

.
.
    ServerConnection *serverConnection = [[ServerConnection alloc]init:val1 _handler:serverConnectionHandler _connectionURL:url];
//    [NSThread detachNewThreadSelector:@selector(run) toTarget:serverConnection withObject:NULL];    
    [serverConnection performSelectorInBackground:@selector(run) withObject:NULL];

In serverConnection i have only two methods ServerConnection.h

@interface ServerConnection : NSObject
{
//    NSString* url;
//    Handler* handler;
//    int action_code;
}

@property (nonatomic,retain) NSString* url;
@property (nonatomic,retain) Handler* handler;
@property (nonatomic,retain) NSNumber* action_code;

-(id) init:(int)_action_code _handler:(Handler *)_handler _connectionURL:(NSString *)_connectionURL;
-(void)run;

ServerConnection.m

- (id) init:(int)_action_code _handler:(Handler *)_handler _connectionURL:(NSString *)_connectionURL{
    if (self = [super init]) {
        action_code = 0;
        action_code = [[NSNumber alloc]initWithInt:_action_code];
        handler = [[Handler alloc]init:_handler];
        url = [[NSString alloc]initWithString:_connectionURL];
    }
    return self;
}

-(void)run
{
     NSLog(@"url--> %@",url);

   //Here using NSURLConnection to fetch data from server
}

when run methods starts i'll get this error sometimes Excess bad access and after enabling zombie showing this error message -[CFString release]: message sent to deallocated instance 0xf9978d0* and **malloc: error for object 0x11039420: pointer being freed was not allocated set a breakpoint in malloc_error_break to debug**

i referred old posts and tried to solve but still facing the same issue. anyone please point me where i am doing mistake?

Upvotes: 1

Views: 186

Answers (1)

zrzka
zrzka

Reputation: 21219

I don't see anything wrong here with your url ivar. Is this your whole code? What's the ServerConnection lifecycle? Monday morning can be the reason that I don't see anything suspicious ... Few additional comments ...

action_code = 0; line is useless, because you're assigning NSNumber on the next line. And action_code is NSNumber, so, even if you want to assign 0 to it, you should write action_code = @( 0 ).

Also the style ... action_code should be named as actionCode. The init... method should look like:

- (id)initWithActionCode:(int)actionCode handler:(Handler *)handler connectionURL:(NSString *) connectionURL {

And if (self = [super init]) { should look like if ( ( self = [super init] ) ) { or use two liner:

self = [super init];
if ( self ) {

You should improve your style to make your code more readable by others.

Upvotes: 1

Related Questions