Clear java object referred in matlab nested callba

2019-09-17 07:43发布

问题:

My code is a GUI with several java-based UI ( JIDE components, custom java classes and jTable).

Here is a snippet of my code:

%% 1st Table.
%Loading my custom java class ParameterTableModel (extends DefaultTableModel)
% ...

%% 2nd Table - Building JIDE property table
PropertyTableList = java.util.ArrayList();

% Creating 1st property
MyProp = javaObjectEDT(com.jidesoft.grid.DefaultProperty());
MyProp.setName('TypeOfAnalysis');
set(handle(MyProp,'CallbackProperties'),'PropertyChangeCallback',{@PropCB});
PropertyTableList.add(MyProp);

% Building table
PropertyTableModel = javaObjectEDT(com.jidesoft.grid.PropertyTableModel(PropertyTableList));
PropertyTableGrid = javaObjectEDT(com.jidesoft.grid.PropertyTable(PropertyTableModel));
PropertyPane = javaObjectEDT(com.jidesoft.grid.PropertyPane(PropertyTableGrid));  
[~, mhPropertyPane] =javacomponent(PropertyPane,[0,0,1,1], figure );
set(mhPropertyPane,'Units','normalized','Position',[0,0,1,1])

% fake callback, property changed
function PropCB( hobj, hev )
    disp('PropCB')
end

When I close the main figure and call " clear java" in the command window, this warning is issued:

Warning: Objects of ParameterTableModel class exist - not clearing java

If I don't attached a callback to MyProp, the warning is not issued anymore. The only workaround I found is to call:

set(MyProp.handle('CallbackProperties'),'PropertyChangeCallback',[])

in the CloseRequestFcn of the figure. But that is not satisfactory as I have hundreds of such properties in my complete GUI.

I found this question related to my problem, but no real answer: clear Java memory