JSON RPC Module: Strange gc-problems

Monkey Targets Forums/iOS/JSON RPC Module: Strange gc-problems

maverick69(Posted 2011) [#1]
Hey monks ;)

I'm currently playing around and trying to build a JSON RPC Client Module for Monkey that wraps https://github.com/samuraisam/DeferredKit

Here is my code so far (please note that you have manually add the Classes-Directory to your xcodeproject):

#import "Classes/JSONRPCService.h"

class JSONServiceWrapper : public gxtkObject {
public:
    void call(String endpoint, String method, Array<String> params = NULL);
    virtual void onDataLoaded(String response);
    virtual void onDataFailed(String err);
};

@interface JSONService : NSObject <JSONRPCServiceDelegate>
{
@public
    void (JSONServiceWrapper::*callbackOnSuccess) (String);
    void (JSONServiceWrapper::*callbackOnFail) (String);
    JSONServiceWrapper *jInstance;
    
    NSString *response;
	JSONRPCService* svc;
}

-(void) call:(String)endpoint withMethod:(String) method andParams:(NSArray*) params;
-(void) onDataLoaded:(String) data;
-(void) onFailed:(String) err;

@end

void JSONServiceWrapper::call(String endpoint, String method, Array<String> params) {
    JSONService *s = [[JSONService alloc] init];
    s->callbackOnSuccess = &JSONServiceWrapper::onDataLoaded;
    s->callbackOnFail = &JSONServiceWrapper::onDataFailed;
    s->jInstance = this;
    [s call:endpoint.ToNSString() withMethod:method.ToNSString() andParams:nil];

}

void JSONServiceWrapper::onDataLoaded(String response) {  
}

void JSONServiceWrapper::onDataFailed(String response) {    
}

@implementation JSONService

#pragma mark -
#pragma mark JSONRPCServiceDelegate

-(void) dataLoaded:(NSData*)data {   
    response = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
    if (callbackOnSuccess && jInstance)
        (jInstance->*callbackOnSuccess)(String(response));
}

-(void) loadingFailed:(NSString*) errMsg {
    if (callbackOnFail && jInstance)
        (jInstance->*callbackOnFail)(String(response));
}

-(void) call:(String)endpoint withMethod:(String) method andParams:(NSArray*) params {
	jInstance->onDataLoaded("Start Call");
    [svc release];
    svc = [[JSONRPCService alloc] initWithURL:[NSURL URLWithString:endpoint.ToNSString()]];
    svc.delegate = self;

    if (!params)
        params = [NSArray array];
    
    [svc execMethod:method.ToNSString() andParams:[NSArray array] withID:@"1"];    
}
@end


Now here is a small example program that works:

Import horizon.application
Import horizon.faderbrightness
Import horizon.scenesplashscreen
Import horizon.zone
Import horizon.util
Import horizon.arrayhelper
Import horizon.collision
Import horizon.touchcontroller
Import horizon.zone
Import soundmanager
Import scenegame
Import scenemarket
Import jsonservice.jsonservice

Class JSONW Extends JSONServiceWrapper
		Method onDataLoaded:Void(response$)
			Print response
		End
		
		Method onDataFailed:Void(response$)
			Print response
		End
End

Global j:JSONW

Function Main:Int()
	Local app:Application = Application.GetInstance()
	app.Init()
	app.SetSize(960, 640)	
	app.AddFader(New FaderBrightness)
'	app.AddScene("title", New SceneSplashScreen("gfx/title.png", 3000, "main"))
	app.AddScene("main", New SceneGame)	
	app.AddScene("market", New SceneMarket)
	
	j = New JSONW
	j.call("http://xxxxxxxxxxx", "ping", [""])
	
	app.Run()
	Return 0
End


When I move the j-Object from global scope to local the app crashes, when it receives the callback:
       (jInstance->*callbackOnSuccess)(String(response));


Do you see anything strange in my code which I shouldn't do?