Weird behaviors of handle classes and/or double linked nodes

6 views (last 30 days)
Hi,
I have been pretty frustrated recently about not having a straight skeleton generator for 2D polygon. So I decided to embarked on an adventure to implement one!
Anyways, while doing so, I come across a bug that is probably one of the strangest one I have ever seen in Matlab. I attached the file. I'm sorry if it is containing lots of code, but I have to put it all in so that you guys can try and run it.
Now, here's the description of my bug:
I have an Object, which I named "SLAV". The object has a properties called "intersectStruct". This properties has 2 field, but the important one (to this bug) is an array of handles objects. The array is called "intersectObject_array", and the object is called "Intersection".
Each of the "Intersection" object also has 2 properties, which is two double linked node, called "LAVertexNode_prev" and "LAVertexNode_next", which is a Matlab's double linked node (I modified it so it do a couple more thing like being a circular double linked and so on).
The weird thing is for every loop iteration in the main loop, I would call a method of "SLAV" called "popIntersectionQueue" which basically return the first "Intersection" object in the array and then delete it from the array. Here's my implementation:
function [intersection] = popIntersectionQueue(obj)
intersection = obj.intersectStruct.intersectObject_array(1);
fprintf("Pop intersection [%d, %d]\n", intersection.point);
fprintf("Vertex prev: [%d %d]\n", intersection.LAVertexNode_prev.Data.point);
fprintf("Edge next: [%d %d]\n", intersection.LAVertexNode_next.Data.point);
obj.intersectStruct.intersectObject_array(1) = [];
obj.intersectStruct.distance2sort(1) = [];
end
And for some reason, at one particular instance, the "intersection" variable in the function is still have the correct value. But after it gets out of the function (get "returned"), the value is not correct anymore... In another words, "intersection" inside the function and outside the function are different. And the different comes from LAVertexNode_next and LAVertexNode_prev, which is 2 double linked node (a handle object). In the function, those two value is linked to other nodes, while after get returned by this function, they are not linked to anything anymore.
I have tried stepping the code line by line, but as soon as it got return, the value change!
So to test out this weird bug by yourself, all you need to do is:
  1. Of course included all the folder inside the Straight Skeleton zip file.
  2. Run test_bench_straight_skeleton function, with the input set to 3.
  3. Set a breakpoint right at the pop function. You should be able to call the function three times before the error comes up (the error is on the fourth time).
  4. You can step throught the code in the function "popIntersectionQueue" to see the value I'm talking about. Pay attention to "intersection.LAVertexNode_prev" or "intersection.LAVertexNode_next". While the "Data" is not changing, Next and Prev get set to empty after the function/method return.
Man, if someone can solve this mystery for me, I will be of debt to that person! Thanks!!!

Answers (1)

Nikita Rudenko
Nikita Rudenko on 25 Dec 2019
>> In another words, "intersection" inside the function and outside the function are different.
I don't see where this is the case. Intersection is the same inside and outside of the function. I added printf to show this (see attachments)
This is the output:
>> testbench_straight_skeleton(3)
.....
Pop intersection before pop [-9.500000e+00, 3]
Pop intersection after pop [-9.500000e+00, 3]
Pop intersection after return [-9.500000e+00, 3]
Edge event!
Insufficient number of outputs from right hand side of equal sign to satisfy assignment.
  1 Comment
Anh Tran
Anh Tran on 25 Dec 2019
Sorry I should have made it clearer. I have attached the 2 files SLAV.m and skeletonise.m in here so that you can try it.
Note how before return, "intersection.LAVertexNode_prev" is linked to 2 nodes, while after return, "intersection.LAVertexNode_prev" is not linked to anything...

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!