Skip to content

Commit 0c2eca0

Browse files
committed
improve reference counting
1 parent d2cabbf commit 0c2eca0

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

src/applications/mne_scan/plugins/fieldline/fieldline_acqsystem.cpp

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ FieldlineAcqSystem::FieldlineAcqSystem(Fieldline* parent)
284284
{
285285
printLog("Initializing Python");
286286
printLog(libPythonBugFix);
287+
287288
void*const libBugFix = dlopen(libPythonBugFix, RTLD_LAZY | RTLD_GLOBAL);
288289

289290
Py_Initialize();
@@ -327,13 +328,9 @@ FieldlineAcqSystem::FieldlineAcqSystem(Fieldline* parent)
327328
} else{
328329
printLog("fServiceInstance ok!");
329330
}
330-
PyObject* openMethod = PyObject_GetAttrString(fServiceInstance, "open");
331-
if (openMethod == NULL)
332-
{
333-
printLog("openMethod wrong!");
334-
} else{
335-
printLog("openMethod ok!");
336-
}
331+
332+
Py_DECREF(ipList);
333+
337334

338335

339336
m_pThreadState = (void*)PyEval_SaveThread();
@@ -343,30 +340,45 @@ FieldlineAcqSystem::FieldlineAcqSystem(Fieldline* parent)
343340
PyGILState_STATE gstate;
344341
gstate = PyGILState_Ensure();
345342

346-
PyObject* pResult = PyObject_CallNoArgs(openMethod);
347-
if (pResult == NULL)
343+
PyObject* openMethod = PyObject_GetAttrString(fServiceInstance, "open");
344+
if (openMethod == NULL)
348345
{
349-
printLog("pResult wrong!");
346+
printLog("openMethod wrong!");
350347
} else{
351-
printLog("pResult ok!");
352-
}
348+
printLog("openMethod ok!");
349+
}
350+
351+
PyObject* openMethodCall = PyObject_CallNoArgs(openMethod);
352+
if (openMethodCall == NULL)
353+
{
354+
printLog("openMethodCall wrong!");
355+
} else{
356+
printLog("openMethodCall ok!");
357+
}
358+
Py_DECREF(openMethodCall);
353359

354360
PyGILState_Release(gstate);
355361

356362
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
357363
// PyGILState_STATE gstate;
358364
gstate = PyGILState_Ensure();
359365

366+
// =========================================================================
367+
// =========================================================================
360368

361369
PyObject* setCloseLoop = PyObject_GetAttrString(fServiceInstance, "set_closed_loop");
362370
PyObject* trueTuple = PyTuple_New(1);
363371
PyTuple_SetItem(trueTuple, 0, Py_True);
364372
PyObject* pResult2 = PyObject_CallObject(setCloseLoop, trueTuple);
373+
Py_DECREF(setCloseLoop);
374+
Py_DECREF(trueTuple);
375+
Py_DECREF(pResult2);
365376

366377
PyObject* loadSensors = PyObject_GetAttrString(fServiceInstance, "load_sensors");
367378
PyObject* sensors = PyObject_CallNoArgs(loadSensors);
368-
// =========================================================================
369-
// =========================================================================
379+
Py_INCREF(loadSensors);
380+
Py_INCREF(sensors);
381+
370382
PyObject* restartAllSensors = PyObject_GetAttrString(fServiceInstance, "restart_sensors");
371383

372384
if (restartAllSensors == NULL)
@@ -441,9 +453,7 @@ FieldlineAcqSystem::FieldlineAcqSystem(Fieldline* parent)
441453
// =========================================================================
442454
gstate = PyGILState_Ensure();
443455

444-
445456
PyObject* coarseZeroAllSensors = PyObject_GetAttrString(fServiceInstance, "coarse_zero_sensors");
446-
447457
if (coarseZeroAllSensors == NULL)
448458
{
449459
printLog("coarse zero sensors broken");
@@ -589,10 +599,13 @@ FieldlineAcqSystem::FieldlineAcqSystem(Fieldline* parent)
589599
// =========================================================================
590600
// =========================================================================
591601

592-
//
593602
// PyGILState_STATE gstate;
594603
gstate = PyGILState_Ensure();
595604

605+
Py_DECREF(loadSensors);
606+
Py_XDECREF(loadSensors);
607+
Py_DECREF(sensors);
608+
Py_XDECREF(sensors);
596609

597610

598611
PyObject* readDataFcn = PyObject_GetAttrString(fServiceInstance, "read_data");
@@ -632,14 +645,8 @@ FieldlineAcqSystem::FieldlineAcqSystem(Fieldline* parent)
632645
Py_DECREF(parseCallbacksModule);
633646
Py_DECREF(FieldlineModule);
634647
Py_DECREF(fService);
635-
Py_DECREF(ipList);
636648
Py_DECREF(fServiceInstance);
637649
Py_DECREF(openMethod);
638-
Py_DECREF(pResult);
639-
Py_DECREF(setCloseLoop);
640-
Py_DECREF(trueTuple);
641-
Py_DECREF(pResult2);
642-
Py_DECREF(loadSensors);
643650
Py_DECREF(readDataFcn);
644651
Py_DECREF(parserCallback);
645652
Py_DECREF(argsSetDataParser);

0 commit comments

Comments
 (0)