Issue #2476 delete local references of arguments to free up memory fast and free memory if someone decides to infinite loop in python
Change-Id: I944d16f506647b138d08c0a93c9b6164a9d55d6e Former-commit-id:1e620fde68
[formerly 016bcf45a28c1f99bee930a12d30a72beb9e4dd3] Former-commit-id:f08ee91f20
This commit is contained in:
parent
64fdaa70d7
commit
f88dd78056
2 changed files with 26 additions and 46 deletions
|
@ -448,7 +448,7 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
|
|||
JNIEnv *env = NULL;
|
||||
int pos = 0;
|
||||
jvalue *jargs = NULL;
|
||||
int *jrelease; // added by njensen
|
||||
int *jargTypes = NULL; // added by njensen
|
||||
int foundArray = 0; /* if params includes pyjarray instance */
|
||||
PyThreadState *_save;
|
||||
|
||||
|
@ -478,7 +478,7 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
|
|||
}
|
||||
|
||||
jargs = (jvalue *) PyMem_Malloc(sizeof(jvalue) * self->lenParameters);
|
||||
jrelease = (int *) PyMem_Malloc(sizeof(int) * self->lenParameters);
|
||||
jargTypes = (int *) PyMem_Malloc(sizeof(int) * self->lenParameters);
|
||||
|
||||
// ------------------------------ build jargs off python values
|
||||
|
||||
|
@ -494,14 +494,14 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
|
|||
param = PyTuple_GetItem(args, pos); /* borrowed */
|
||||
if(PyErr_Occurred()) { /* borrowed */
|
||||
PyMem_Free(jargs);
|
||||
PyMem_Free(jrelease);
|
||||
PyMem_Free(jargTypes);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pclazz = (*env)->GetObjectClass(env, paramType);
|
||||
if(process_java_exception(env) || !pclazz) {
|
||||
PyMem_Free(jargs);
|
||||
PyMem_Free(jrelease);
|
||||
PyMem_Free(jargTypes);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -511,38 +511,15 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
|
|||
if(paramTypeId == JARRAY_ID)
|
||||
foundArray = 1;
|
||||
|
||||
// njensen set up the following ifs related to converting args
|
||||
// we are creating primitive java arrays from numpy arrays,
|
||||
// so as soon as we done passing the argument through we
|
||||
// need to free it. jrelease exists to track which must be released.
|
||||
jrelease[pos] = 0;
|
||||
if(paramTypeId == JARRAY_ID && !pyjarray_check(param))
|
||||
{
|
||||
jvalue arg = convert_pynumpyarg_jvalue(env, param, paramType, paramTypeId, pos);
|
||||
if(arg.l != NULL)
|
||||
{
|
||||
jargs[pos] = arg;
|
||||
jrelease[pos] = 1;
|
||||
}
|
||||
|
||||
if(PyErr_Occurred()) { /* borrowed */
|
||||
PyMem_Free(jargs);
|
||||
PyMem_Free(jrelease);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if(jrelease[pos] != 1)
|
||||
{
|
||||
jargs[pos] = convert_pyarg_jvalue(env,
|
||||
jargTypes[pos] = paramTypeId;
|
||||
jargs[pos] = convert_pyarg_jvalue(env,
|
||||
param,
|
||||
paramType,
|
||||
paramTypeId,
|
||||
pos);
|
||||
}
|
||||
if(PyErr_Occurred()) { /* borrowed */
|
||||
PyMem_Free(jargs);
|
||||
PyMem_Free(jrelease);
|
||||
PyMem_Free(jargTypes);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -622,7 +599,7 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
|
|||
|
||||
// added by njensen to keep memory down
|
||||
if(obj != NULL)
|
||||
(*env)->DeleteLocalRef(env, obj);
|
||||
(*env)->DeleteLocalRef(env, obj);
|
||||
|
||||
break;
|
||||
}
|
||||
|
@ -656,7 +633,7 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
|
|||
|
||||
// added by njensen to keep memory down
|
||||
if(obj != NULL)
|
||||
(*env)->DeleteLocalRef(env, obj);
|
||||
(*env)->DeleteLocalRef(env, obj);
|
||||
|
||||
break;
|
||||
}
|
||||
|
@ -690,7 +667,8 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
|
|||
|
||||
// added by njensen to keep memory down
|
||||
if(obj != NULL)
|
||||
(*env)->DeleteLocalRef(env, obj);
|
||||
(*env)->DeleteLocalRef(env, obj);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -958,14 +936,14 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
|
|||
|
||||
// added by njensen to keep memory usage down
|
||||
for(pos = 0; pos < self->lenParameters; pos++) {
|
||||
if(jrelease[pos] == 1 && jargs[pos].l != NULL)
|
||||
{
|
||||
(*env)->DeleteLocalRef(env, jargs[pos].l);
|
||||
}
|
||||
if(jargs[pos].l != NULL && jargTypes[pos] != JARRAY_ID)
|
||||
{
|
||||
(*env)->DeleteLocalRef(env, jargs[pos].l);
|
||||
}
|
||||
}
|
||||
|
||||
PyMem_Free(jargs);
|
||||
PyMem_Free(jrelease);
|
||||
PyMem_Free(jargTypes);
|
||||
|
||||
if(PyErr_Occurred())
|
||||
return NULL;
|
||||
|
@ -978,7 +956,7 @@ PyObject* pyjmethod_call_internal(PyJmethod_Object *self,
|
|||
pyjarray_pin((PyJarray_Object *) param);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(result == NULL) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
|
|
|
@ -1385,22 +1385,23 @@ PyObject* convert_jobject(JNIEnv *env, jobject val, int typeid) {
|
|||
}
|
||||
|
||||
|
||||
// added by njensen, had to go separate from convert_pyarg_jvalue to delete
|
||||
// the local references
|
||||
// added by njensen, this converts a numpy array to a java primitive array
|
||||
// Returns null if the python object is a pyjarray
|
||||
// null and error indicator if the object can't be transformed to a java primitive array
|
||||
// a java primitive array corresponding to the numpy array
|
||||
jvalue convert_pynumpyarg_jvalue(JNIEnv *env,
|
||||
PyObject *param,
|
||||
jclass paramType,
|
||||
int paramTypeId,
|
||||
int pos) {
|
||||
jvalue ret;
|
||||
jarray arr;
|
||||
jclass arrclazz;
|
||||
jarray arr;
|
||||
jclass arrclazz;
|
||||
|
||||
ret.l = NULL;
|
||||
arr = NULL;
|
||||
arr = NULL;
|
||||
|
||||
if(param != Py_None && !pyjarray_check(param)) {
|
||||
// this pyarray -> jarray added by njensen
|
||||
initUtil();
|
||||
|
||||
if(PyArray_Check(param))
|
||||
|
@ -1506,7 +1507,8 @@ jvalue convert_pyarg_jvalue(JNIEnv *env,
|
|||
;
|
||||
else {
|
||||
PyJarray_Object *ar;
|
||||
// njensen shifted this code
|
||||
// njensen changed this code to try and convert it from a numpy array,
|
||||
// it will return null if the parameter is already a pyjarray
|
||||
ret = convert_pynumpyarg_jvalue(env, param, paramType, paramTypeId, pos);
|
||||
if(ret.l != NULL)
|
||||
{
|
||||
|
|
Loading…
Add table
Reference in a new issue