Skip to content

Commit 98ac3ce

Browse files
committed
Call unloadlibrary when nothing is using it
The implementation uses a reference count through a persistent local variable in `DSS_MATLAB.librefcount`. Required for a clean exit (otherwise MATLAB may keep the process open).
1 parent e65e3b5 commit 98ac3ce

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

+DSS_MATLAB/APIUtil.m

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
classdef APIUtil < handle
2-
properties (Access = protected)
3-
libraryWasLoaded = 0
4-
end
52
properties
63
%DataPtr_PDouble
74
%DataPtr_PInteger
@@ -13,6 +10,7 @@
1310
end
1411
methods
1512
function obj = APIUtil()
13+
1614
if getenv('DSS_EXTENSIONS_DEBUG') == '1'
1715
warnings.warn('Environment variable DSS_EXTENSIONS_DEBUG=1 is set: loading the debug version of the DSS C-API library')
1816
obj.libname = 'dss_capid';
@@ -23,6 +21,7 @@
2321
MfilePath = fileparts(mfilename('fullpath'));
2422
DLLfilePath = fullfile(MfilePath, obj.libname);
2523
PropertiesMOfilePath = fullfile(MfilePath, 'messages', 'properties-en-US.mo');
24+
DSS_MATLAB.librefcount(1);
2625
if libisloaded(obj.libname)
2726
return;
2827
end
@@ -37,17 +36,18 @@
3736
end
3837
calllib(obj.libname, 'DSS_Start', 0);
3938
calllib(obj.libname, 'DSS_SetPropertiesMO', PropertiesMOfilePath);
40-
obj.libraryWasLoaded = 1;
4139
warning(orig_state);
4240
end
4341

4442
function delete(obj)
45-
% Don't unload the library anymore for better compatibility with
46-
% the COM behavior
47-
48-
% if (obj.libraryWasLoaded ~= 0)
49-
% unloadlibrary(obj.libname);
50-
% end
43+
% If nothing is using the library, unload it.
44+
% Required to properly exit MATLAB.
45+
cnt = DSS_MATLAB.librefcount(-1);
46+
if (cnt == 0)
47+
calllib(obj.libname, 'Text_Set_Command', 'Set Parallel=no');
48+
calllib(obj.libname, 'DSS_ClearAll');
49+
unloadlibrary(obj.libname);
50+
end
5151
end
5252

5353
function obj = InitBuffers(obj)

+DSS_MATLAB/librefcount.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function out = librefcount(data)
2+
persistent refcount;
3+
if isempty(refcount)
4+
refcount = 0;
5+
end
6+
if data > 0
7+
refcount = refcount + 1;
8+
end
9+
if data < 0
10+
refcount = refcount - 1;
11+
end
12+
out = refcount;
13+
end

0 commit comments

Comments
 (0)