MATLAB Answers

Handle object does not beheave as expected

조회 수: 1(최근 30일)
Jannis Carstens
Jannis Carstens 2018년 10월 31일
댓글: Jannis Carstens 2018년 11월 1일
classdef myClass < handle
properties (Access = private)
pointerObjects = [];
methods (Access = public)
function obj = myClass()
function setPointers(obj)
obj.setPointers.p1 = libstruct( [..]);
obj.setPointers.p2 = libpointer( [..]);
function clearPointers(obj)
clear obj.setPointers.p1;
clear obj.setPointers.p2;
clear obj.setPointers;
I have a custom class myClass, which is defined as a handle class. The method setPointers creates some libstruct and libpointer objects and stores them in a property. This works fine. However, I can not clear/free these objects to successfully unload the DLL, to which the structs and pointers belong.
If I call the method clearPointers, the objects are cleared in clearPointers workspace, but when the the function is left, they are still present in the instance of the myClass object. This causes unloadlibray to terminate with the error "outstanding objects".
I expected, that the clear does clear the objects in the myClass instance and not only in the methods workspace because myClass is derived from the handle class.
How can I clear those objects (and only those)?
Regards Jannis

채택된 답변

Philip Borghesani
Philip Borghesani 2018년 10월 31일
To clear a property set it to [] instead of using the clear function. Clear only works for variables in the workspace not part of a variable.
obj.pointerObjects.p1=[]; ... % or
obj.pointerObjects=[]; %clear/release all pointerObjects
I am guessing here, your example has at least one glaring error: you have a method named setPointers and a property named pointerObjects I think you meant to use the property inside of your methods?
Libpointers and libstructs are a form of handle object. Any copies of them will add a reference to the same object.
  댓글 수: 1
Jannis Carstens
Jannis Carstens 2018년 11월 1일
I reduced the example to the only aspect, which did not work. Using the structs/pointers in other methods works as expected.
Setting pointerObjects = []; worked out! Thanks ;)

댓글을 달려면 로그인하십시오.

추가 답변(0개)




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by