When i run this code, i have a problem with the memory so i think i should use PY_DECREF()
in order to free the memory, but i don't know where to put it ? Any help ? I've tried to put it at the end of the code, just before returning pArgs
but it doesn't seem to work.
This code prepares the argument that are sent to a python function so it fills pArgs
with count lists. Each list is an argument of the python function.
PyObject * Ecrire::getArgumentsbis(PythonRetour * pr){
int j = 0 ;
PyObject * pArgs = NULL;
int count=pr->numberargs;
pArgs = PyTuple_New(count);
PyObject * pValue;
PyObject ** tuplelist = new PyObject*[count];
for(j = 0; j < pr->numberargs; j++){
std::string argument = pr->nom_args[j];
int buffer = pr->buffer[j]+1;
tuplelist[j] = PyList_New(buffer);
if(ends_with_string(argument,"%#C#%"))
argument = argument.substr(0, argument.size()-5);
if(valeurs.size() >= buffer){
int l;
for(l = 0; l < buffer; l++){
map<std::string,pvalues>::const_iterator it = valeurs[valeurs.size() - 1 - l].find(argument);
if (it != valeurs[valeurs.size() - 1 - l].end()){
if(ends_with_string(pr->nom_args[j], "%#C#%")){
if((*it).second.type == "enumere"){
std::string valueread = (*it).second.val;
unsigned long long numberread;
istringstream(valueread) >> numberread;
std::map<std::string,inf_analyse>::const_iterator iter=mat->liste_analyse.find(argument);
if (iter != mat->liste_analyse.end()){
bool check = false;
std::string valuecorr = "";
int k = 0;
for(k=0;k<(*iter).second.nombre_valeurs;k++){
if((*iter).second.valeurs[k] == numberread) {
check = true;
valuecorr = (*iter).second.correspondances[k];
break;
}
}
if(check) {
pValue = PyString_FromString(valuecorr.c_str());
PyList_SetItem(tuplelist[j], buffer - l - 1, pValue);
}
else
return NULL;
}
}
}
else {
if((*it).second.type == "enumere"){
std::string valueread = (*it).second.val;
unsigned long long numberread;
istringstream(valueread) >> numberread;
pValue = PyInt_FromLong(numberread);
PyList_SetItem(tuplelist[j], buffer - l - 1, pValue);
}
else if((*it).second.type == "autre") {
std::string valueread = (*it).second.val;
double numberread;
istringstream(valueread) >> numberread;
pValue = Py_BuildValue("d", numberread);
PyList_SetItem(tuplelist[j], buffer - l - 1, pValue);
}
else if((*it).second.type == "chaine"){
std::string valueread = (*it).second.val;
pValue = PyString_FromString(valueread.c_str());
PyList_SetItem(tuplelist[j], buffer - l - 1, pValue);
}
}
}
else
return NULL;
}
}
else
return NULL;
PyTuple_SetItem(pArgs,j, tuplelist[j]);
}
return pArgs;
}