Skip to content

Commit 2fae66a

Browse files
committed
Clean up MATLABCommands on disposal
The DefaultMATLABService will now remove all command variables when its dispose method is called. The MATLABBindings were updated to handle null set calls.
1 parent 7a7982f commit 2fae66a

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/main/java/org/scijava/plugins/scripting/matlab/DefaultMATLABService.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ public void initialize() {
9393
scriptService.addAlias("matrix", MatlabNumericArray.class);
9494
}
9595

96+
@Override
97+
public void dispose() {
98+
removeCommandVariables();
99+
}
100+
96101
// -- Typed methods --
97102

98103
@Override
@@ -116,4 +121,16 @@ private synchronized void createCommandVariables() {
116121
initializedCommands = true;
117122
}
118123
}
124+
125+
/**
126+
* Helper method to remove variables for each {@link MATLABCommands} within
127+
* MATLAB.
128+
*/
129+
private void removeCommandVariables() {
130+
for (final MATLABCommands command : getInstances()) {
131+
final String name = command.getInfo().getName();
132+
133+
makeMATLABVariable(name, null);
134+
}
135+
}
119136
}

src/main/java/org/scijava/plugins/scripting/matlab/MATLABBindings.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,12 @@ public Object put(final String name, final Object value) {
111111
final MatlabProxy proxy = MATLABControlUtils.proxy(opts());
112112

113113
// Try special MATLAB data types
114-
if (MatlabNumericArray.class.isAssignableFrom(value.getClass())) {
114+
if (value != null &&
115+
MatlabNumericArray.class.isAssignableFrom(value.getClass()))
116+
{
115117
// Convert the dataset to a MATLAB array and set it as a local variable
116118
// within MATLAB.
117-
final MatlabTypeConverter converter =
118-
new MatlabTypeConverter(proxy);
119+
final MatlabTypeConverter converter = new MatlabTypeConverter(proxy);
119120
try {
120121
converter.setNumericArray(sanitize(name), (MatlabNumericArray) value);
121122
return value;

0 commit comments

Comments
 (0)