Parameter (string * key)

Post here your questions about the C++ API for SFS2X

Moderators: Lapo, Bax, MBagnati

Post Reply
scofy
Posts: 79
Joined: 08 Apr 2012, 09:04

Parameter (string * key)

Post 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.
scofy
Posts: 79
Joined: 08 Apr 2012, 09:04

Re: Parameter (string * key)

Post 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"));
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Parameter (string * key)

Post 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
Lapo
--
gotoAndPlay()
...addicted to flash games
bqlf1907
Posts: 25
Joined: 13 May 2013, 10:28

Re: Parameter (string * key)

Post 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.
MBagnati
Posts: 126
Joined: 12 Feb 2013, 10:57

Re: Parameter (string * key)

Post 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
bqlf1907
Posts: 25
Joined: 13 May 2013, 10:28

Re: Parameter (string * key)

Post 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?
MBagnati
Posts: 126
Joined: 12 Feb 2013, 10:57

Re: Parameter (string * key)

Post by MBagnati »

Yes, you can
Post Reply