I create a CScriptArray in engine and return it to script like this
Function registration is this,
r = engine->RegisterObjectMethod("DataFile", "array<string>@ GetValuesOfChildrenWithName(const string &in, const string &in)", asMETHOD(DataFile, GetValuesOfChildrenWithName), asCALL_THISCALL);assert( r >= 0 );
GetValuesOfChildrenWithName function in C++
(horrible function naming it gets values of children nodes with the given name)
CScriptArray *DataFile::GetValuesOfChildrenWithName(const string &location, const string &name)
{
std::vector<string> allvalues;
ptree &tree = pt.get_child(location);
BOOST_FOREACH(boost::property_tree::ptree::value_type &v, tree)
{
if(v.first == name )
{
allvalues.push_back(v.second.get_value<string>());
}
}
asIObjectType* t = ScriptManager::getSingletonPtr()->engine->GetObjectTypeById(ScriptManager::getSingletonPtr()->engine->GetTypeIdByDecl("array<string>"));
CScriptArray* arr = new CScriptArray(allvalues.size(), t); // i do not Add Ref, since i dont need this array in C++
;
for (unsigned int i = 0; i< allvalues.size() ; ++i)
{
arr->InsertAt(i, new string(allvalues)); // this works, but would it cause a memory leak ?
}
return arr;
}
Script function,
called about 600 times a second.
void Routine()
{
DataFile @df = DFM.GetDataFile("../../gamedata/newfile.xml");
array<string> arr = df.GetValuesOfChildrenWithName("Root", "SomeNode");
for( uint n = 0; n < arr.length; n++ )
{
//nothing happens here, for now.
}
}
here is DFM.GetDataFile function, in case you suspect it
DataFile * DataFileManager::GetDataFile( const string &filename )
{
if(!dataFiles.count(filename))
{
return 0;
}
return &dataFiles[filename];
}
DataFile is a asOBJ_REF | asOBJ_NOCOUNT type.
I don't have any leak detector programs at the moment, but program's memory usage is increasing at a rapid rate.
Is there a better way to send an array to script?
if not, What did i do wrong?
thank you.