Strange behavior of shape's model

Oct 26, 2012 at 5:34 PM
Edited Oct 26, 2012 at 5:49 PM

Hi,

I'm observing a strange behavior of my shape's model.

Imagine we placed a MyShape shape onto the diagram. This shape has a model - of type MyModel. It's not null, and MyModel.SomeProperty is not null as well: we see this in the "Model" PropertyGrid. Now we start dragging our shape with a SelectionTool and (!) MyModel.SomeProperty becomes null, then becomes not null, then becomes null again etc etc etc.

Here is the code of my shape:

public class MyShape : PredefinedProcessSymbol
{
    ...
    public override void Draw(Graphics graphics)
    {
        base.Draw(graphics);
        MyModel model = this.ModelObject as MyModel;
        if (model != null)
        {
            Console.WriteLine("MyShape :: SomeProperty is null? --> {0}", model.SomeProperty == null);
        }
    }
}

And here is the console output:

MyShape :: SomeProperty is null? --> False
MyShape :: SomeProperty is null? --> True
MyShape :: SomeProperty is null? --> False
MyShape :: SomeProperty is null? --> True
...
MyShape :: SomeProperty is null? --> False
MyShape :: SomeProperty is null? --> True
MyShape :: SomeProperty is null? --> False

How can this happen?!

[UPDATE] I placed a breakpoint into the setter of the MyModel.SomeProperty property so that it will be hit if value == null, but this breakpoint is never hit!

[UPDATE] Well, now I see the reason: shape's model is recreated many times while shape is being moved!!! However, when movement completes, the initial model object comes back to scene. To understand this, I extended the console output with model object's hash code:

Console.WriteLine("MyShape :: SomeProperty is null? --> {0}, hash = {1}", model.SomeProperty == null, model.GetHashCode());

And here's the new console output:

MyShape :: SomeProperty is null? --> False, hash = 59265182
MyShape :: SomeProperty is null? --> True, hash = 3172476
MyShape :: SomeProperty is null? --> False, hash = 59265182
MyShape :: SomeProperty is null? --> True, hash = 50272769
MyShape :: SomeProperty is null? --> False, hash = 59265182
MyShape :: SomeProperty is null? --> True, hash = 48153161
MyShape :: SomeProperty is null? --> False, hash = 59265182

That's all very bad.

Oct 29, 2012 at 8:04 AM

I found out the answer. As I move my shape, its preview shape is being recreated all the time. In another topic I found out how to determine if we are dealing with a preview shape; using the IsPreview property I proved that this "strange" hash code is printed for preview shapes.

Coordinator
Oct 29, 2012 at 10:42 AM

For darwing the movement preview, the original shape is cloned and transformed into a 'preview shape' (in most cases, this means that it has transparent 'preview' versions of its styles instead of the normal styles).

May I regard this problem as solved or is there still an issue?

Oct 29, 2012 at 10:47 AM

Well, I consider it solved - but only because I managed to determine if the shape is the preview one. However, why isn't the model cloned too?