Why did my code break after adding a class definition?
Show older comments
I have some code that interfaces with external hardware via loadlibrary to a vendor-supplied DLL. After getting everything working the way I wanted, I decided to refactor to make future maintainability easier. As part of this process, I defined a class params.m that holds experimental parameters (e.g. image height, image width, number of frames,...). Previously, I had just made a params struct and added stuff to it as needed (without a class definition). I also added arguments to my function definitions, e.g.:
function configure(inParams, boardNumber)
arguments
inParams (1,1) params
boardNumber (1,1) double {mustBeInteger, mustBePositive}
end
% call hardware functions from DLL
end
In doing so, this broke my experiment. The Matlab code is shared between two systems, one that has a single board and one that has two boards so configure is called in a for loop:
for boardId = 1 : numBoards
configure(inParams,boardId);
end
% do stuff
The code refactor broke my experiment, but only on the system that has two boards. While debugging, I noticed that inserting an arbitrary pause made the two-board system work every other time the code was run (which is better than never, but still 50%). After that realization, I tried commenting out the arguments definition:
function configure(inParams, boardNumber)
% arguments
% inParams (1,1) params
% boardNumber (1,1) double {mustBeInteger, mustBePositive}
% end
% call hardware functions from DLL
end
Which removed the need for the arbitrary pause, but still only got the two-board system to work correctly on every other function call (always works on first try, and from then on any odd-numbered run will work). It seems as though there is some kind of under-the-hood optimization(?) that is ocurring after I added a class definition that breaks things.
Does anyone have other ideas what the issue might be and/or how I can disable optimizations for that one function call (which appears to be impossible based on my reading). I recognize this is an exceptionally odd error and I'm happy to provide more information if that would be useful. I have also reached out to the hardware manufacturer to see if they have any suggestions for what might be causing this.
---------- EDIT ------------
After getting in touch with the hardware vendor, they instructed me to enable some debug features on the board(s) to help diagnose it from that side. This apparently fixed the issue as now it works reliably (even with debugging turned off again). This is somewhat unsatisfying to me as the code would work reliably with the old (pre-class) version of the code and fail reliably with the new version, so I don't think it was exclusively a hardware problem.
8 Comments
Walter Roberson
on 11 Sep 2024
Is it correct that you created a handle class, rather than a value class ?
Benjamin
on 11 Sep 2024
Steven Lord
on 11 Sep 2024
What does "broke" mean in the context of "The code refactor broke my experiment, but only on the system that has two boards."?
- Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
- Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
- Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support so we can investigate.
Benjamin
on 11 Sep 2024
Steven Lord
on 11 Sep 2024
Okay, how do you create the inParams variable that you pass into the configure method? Is it an instance of the params class? Or is it data that you hope/expect to be used to construct a params class inside the arguments block?
Those properties of the boards that are not set appropriately are set using some function from a DLL that the board manufacturer or MathWorks has provided to interface with that board?
Ah, reading more closely your params class makes me think I know something that may be contributing to or causing the behavior you're seeing. The boards and controlBox properties contain handle objects. The "Expression Evaluation in Handle and Value Classes" section on this documentation page shows what happens when you define a property whose default value is a value class and a handle class. Make sure the behavior it describes when a property's default value is a handle class is the behavior you expect for your class.
Benjamin
on 11 Sep 2024
Walter Roberson
on 12 Sep 2024
configure(inParams,boardId);
Your class is a value class. Any changes to the class that you make will get discarded, as you are not saving the resulting object back to inParams .
I can't say I am completely sure what happens if you just change handle objects within the value class.. I guess that would work?
Benjamin
on 12 Sep 2024
Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!