(svn r13424) [NoAI] -Codechange: strdup strings returned by Squirrel when it's called from openttd.
--- a/src/ai/ai_info.cpp Sun Jun 08 21:36:03 2008 +0000
+++ b/src/ai/ai_info.cpp Sun Jun 08 22:01:32 2008 +0000
@@ -17,6 +17,10 @@
AIInfo::~AIInfo()
{
this->engine->ReleaseObject(this->SQ_instance);
+ free((void *)this->author);
+ free((void *)this->name);
+ free((void *)this->description);
+ free((void *)this->date);
free(this->script_name);
free(this->dir_name);
free(this->SQ_instance);
@@ -24,17 +28,20 @@
const char *AIInfo::GetAuthor()
{
- return this->engine->CallStringMethod(*this->SQ_instance, "GetAuthor");
+ if (this->author == NULL) this->author = this->engine->CallStringMethodStrdup(*this->SQ_instance, "GetAuthor");
+ return this->author;
}
const char *AIInfo::GetName()
{
- return this->engine->CallStringMethod(*this->SQ_instance, "GetName");
+ if (this->name == NULL) this->name = this->engine->CallStringMethodStrdup(*this->SQ_instance, "GetName");
+ return this->name;
}
const char *AIInfo::GetDescription()
{
- return this->engine->CallStringMethod(*this->SQ_instance, "GetDescription");
+ if (this->description == NULL) this->description = this->engine->CallStringMethodStrdup(*this->SQ_instance, "GetDescription");
+ return this->description;
}
int AIInfo::GetVersion()
@@ -44,7 +51,8 @@
const char *AIInfo::GetDate()
{
- return this->engine->CallStringMethod(*this->SQ_instance, "GetDate");
+ if (this->date == NULL) this->date = this->engine->CallStringMethodStrdup(*this->SQ_instance, "GetDate");
+ return this->date;
}
bool AIInfo::AllowStartup()
@@ -54,8 +62,10 @@
AIController *AIInfo::CreateInstance()
{
- const char *class_name = this->engine->CallStringMethod(*this->SQ_instance, "CreateInstance");
- return new AIController(this->script_name, class_name);
+ const char *class_name = this->engine->CallStringMethodStrdup(*this->SQ_instance, "CreateInstance");
+ AIController *ai_controller = new AIController(this->script_name, class_name);
+ free((void *)class_name);
+ return ai_controller;
}
const char *AIInfo::GetDirName()
--- a/src/ai/ai_info.hpp Sun Jun 08 21:36:03 2008 +0000
+++ b/src/ai/ai_info.hpp Sun Jun 08 22:01:32 2008 +0000
@@ -7,6 +7,7 @@
class AIInfo {
public:
+ AIInfo() : author(NULL), name(NULL), description(NULL), date(NULL) {};
~AIInfo();
/**
@@ -70,6 +71,10 @@
char *script_name;
char *dir_name;
class AISquirrel *base;
+ const char *author;
+ const char *name;
+ const char *description;
+ const char *date;
};
#endif /* AI_INFO */
--- a/src/squirrel.hpp Sun Jun 08 21:36:03 2008 +0000
+++ b/src/squirrel.hpp Sun Jun 08 22:01:32 2008 +0000
@@ -91,7 +91,7 @@
*/
void CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret);
void CallMethod(HSQOBJECT instance, const char *method_name) { this->CallMethod(instance, method_name, NULL); }
- const char *CallStringMethod(HSQOBJECT instance, const char *method_name) { HSQOBJECT ret; this->CallMethod(instance, method_name, &ret); return ObjectToString(&ret); }
+ const char *CallStringMethodStrdup(HSQOBJECT instance, const char *method_name) { HSQOBJECT ret; this->CallMethod(instance, method_name, &ret); return strdup(ObjectToString(&ret)); }
int CallIntegerMethod(HSQOBJECT instance, const char *method_name) { HSQOBJECT ret; this->CallMethod(instance, method_name, &ret); return ObjectToInteger(&ret); }
/**