Page 1 of 1

Parameter (string * key)

Posted: 14 May 2013, 03:49
by scofy
Many functions using the parameter (string * key). This parameter point is need to delete or do not need ,
the API documentation did not specify.
for example:(SimpleChat)

Code: Select all

			CT2CA pszConvertedAnsiString (username);
			std::string* stdUsername = new string(pszConvertedAnsiString);

			// Programmatically set the zone name 
			std::string* stdZoneName = new string("BasicExamples");

			// Perform login request
			LoginRequest* request = new LoginRequest(stdUsername, NULL, stdZoneName);
			ptrMainFrame->m_ptrSmartFox->Send(request);

			delete request;
			request = NULL;

i saw code.i see here stdZoneName and stdZoneName is delete in ~LoginRequest()

Code: Select all

	LoginRequest::~LoginRequest()
	{
		delete zoneName;
		zoneName = NULL;

		delete userName;
		userName = NULL;

		delete password;
		password = NULL;

		delete parameters;
		parameters = NULL;
	}
but use SFSObject,for example

Code: Select all

SFSObject::GetByte(string* key);
	SFSObject::PutBoot(string* key,bool* val);	
	these points is need delete by self;

What I mean is a function best to state those pointers need to manage, which is not required。
This is my understanding, do not know right, if the use of the wrong, it will cause memory leaks。

(String * key) as a parameter, and is very troublesome to use。
for example:
way1:

Code: Select all

	SFSObject *obj = new SFSObject();
	string sName = "name";
	string sPwd = "password";
	string sZone = "zone";
	string sGame = "Game";
	obj->PutUtfString(&sName,&userName);
    obj->PutUtfString(&sPwd,&userName);
	obj->PutUtfString(&sZone,&sGame);
	//TODO: send cmd;
	delete obj;
way2:

Code: Select all

SFSObject *obj = new SFSObject();
	string sName = new string("name");
	string sPwd = new string("password");
	string sZone = new string("zone");
	string sGame = new string("Game");
	obj->PutUtfString(sName,&userName);
    obj->PutUtfString(sPwd,&userName);
	obj->PutUtfString(sZone,&sGame);
	//TODO: send cmd;
	delete obj;
	delete sName;
	delete sPwd;
	delete sZone;
	delete sGame;

but use other ClientAPI(like ObjectC)

Code: Select all

	SFSObject *obj = [[SFSObject newInstance] autorelease];
    [obj putUtfString:@"name" value:[NSString  stringWithUTF8String:userName.c_str()]];
    [obj putUtfString:@"password" value:[NSString  stringWithUTF8String:userName.c_str()]];
    [obj putUtfString:@"zone" value:@"Game"];
Very simple!!!

if Modify

Code: Select all

	SFSObject::GetByte(const string& key);
or

Code: Select all

	SFSObject::GetByte(const char *pKey);
then can use

Code: Select all

	SFSObject *obj = new SFSObject();
	obj->PutUtfString("name",&userName);
    obj->PutUtfString("password",&userName);
	obj->PutUtfString("zone","Game");
	//TODO: send cmd;
	delete obj;
so simple too.

Re: Parameter (string * key)

Posted: 14 May 2013, 06:46
by scofy
way3:

Code: Select all

	SFSObject *obj = new SFSObject();
	obj->PutUtfString(&string("name"),&userName);
    obj->PutUtfString(&string("sPwd"),&userName);
	obj->PutUtfString(&string("sZone"),&string("sGame"));
	//TODO: send cmd;
	delete obj;
	
use SFSObject::GetInt (is right?)

Code: Select all

	int icon = *obj->GetInt(&string("icon"));

Re: Parameter (string * key)

Posted: 14 May 2013, 07:33
by Lapo
Please make sure to use code blocks in order to format the code snippets correctly, otherwise the post becomes unreadable.
While composing the message all you have to do is selecting the code snippet and press the Code button on top of the text area.
Thanks

Re: Parameter (string * key)

Posted: 14 May 2013, 08:01
by bqlf1907
Why use the pointer???

Code: Select all

long int* SFSObject::GetInt(string* key);

the paremeter is pointer,
return value is pointer too.
It is not easy to use.

Re: Parameter (string * key)

Posted: 15 May 2013, 09:10
by MBagnati
We are investigating how simplify the usage of pointers as function parameters, following suggestions incoming from you and from other users, to get two goals:
first to have a general guideline about who is responsible to release memory to avoid leaks;
second to give a more confortable API interface.

Probably the solution will be the usage of references so you will write

Code: Select all

SFSObject *obj = new SFSObject();
obj->PutUtfString("zone","Game");
or

Code: Select all

SFSObject *obj = new SFSObject();
string sZone = "zone";
string sGame = "Game";
obj->PutUtfString(&sZone,&sGame);
instead of

Code: Select all

SFSObject *obj = new SFSObject();
string sZone = new string("zone");
string sGame = new string("Game");
obj->PutUtfString(sZone,&sGame);
delete sZone;
delete sGame;
Please give us a little time to study the question and fix it into the next API release

Re: Parameter (string * key)

Posted: 15 May 2013, 11:33
by bqlf1907

Code: Select all

   SFSObject *obj = new SFSObject();
   obj->PutUtfString(&string("name"),&userName);
    obj->PutUtfString(&string("sPwd"),&userName);
   obj->PutUtfString(&string("sZone"),&string("sGame"));
   //TODO: send cmd;
   delete obj;
so,i can use it this way with 0.9.5?

Re: Parameter (string * key)

Posted: 16 May 2013, 08:48
by MBagnati
Yes, you can