How/why are objects created?

StephenTStephenT Fabric for MotionBuilder Posts: 77
edited February 16 in Canvas Standalone

I am trying to develop something with a unique singleton.

In KL, I define a function to create the object. In the constructor, I setError if the singleton already exists.

In canvas, I create a function node to return the singleton.

function ScatterBrain getScatterBrain(){
// check if we can get the singleton
ScatterBrain brain = Singleton_get('ScatterBrain');
if(brain== null) {
brain = ScatterBrain(); // this will set the singleton pointer
}
return brain;
}

However, I'm seeing multiple instances of my class being created. However, new instances see to be created frequently - seemingly mostly whenever I add new presets that have the singleton as an argument.

The stack trace is as below
[FABRIC:MT] [ST] 1 kl.internal.String.SetErrorDataPtrAndLength.AS0()
[FABRIC:MT] [ST] 2 function.setError.R.St()
[FABRIC:MT] [ST] 3 method.init.L.Uo11ScatterBrain.R.Bo() ScatterBrain.kl:102
[FABRIC:MT] [ST] 4 create.L.OO_ScatterBrain() ScatterBrain.kl:75
[FABRIC:MT] [ST] 5 __createWithArgs() createArgs.kl:1
[FABRIC:MT] +++ Creating ScatterBrain +++
[FABRIC:MT] +++ Released ScatterBrain +++

Any idea why this is happening? What can I do to stop it? I want to expose this instance in my graph, but I cannot have new instances being created constantly.

Tagged:

Best Answer

Answers

  • HelgeHelge Moderator, Fabric Employee Posts: 314 Fabric Employee

    In your code you don't seem to be setting the singleton ever. After creating it once you will have to also set it using Singleton_set.

    Research Engineer @ Fabric Software

  • StephenTStephenT Fabric for MotionBuilder Posts: 77

    I set the singleton from within the constructor. Idea being that you can't create an instance without setting the singleton.

    I'm working around this for the moment by moving the sington_set to in the getScatterBrain method, so even if I do create other instances it doesn't mess up my system.

    In the long run though, I want/need to get rid of the singleton access, and I'll need to understand how/why an instance might get created.

    Why is this function being called?

    [FABRIC:MT] [ST] 5 __createWithArgs() createArgs.kl:1

  • StephenTStephenT Fabric for MotionBuilder Posts: 77

    Thanks for the quick responses Helge & Peter, as always its very appreciated!

    I am passing the entity through ports. I found that when I finally connected the whole assembly to a maya attribute (and it is actually evaluated) my instance seemed to be replaced.

    SB ->- SBAgent ->- Maya

    Its bit of a shonky design, reflecting the fact I'm still much more comfortable with functional programming than node graphs, but it still bothers me that I don't know why its not working.

    The SB allocates the data, then the SBAgent acts on the data. Everything seems to be fine until I connect SBAgent to Maya, at which point I see a ++SBrain++ allocated and SBrain deleted notifications.

    Is it possible for a port to be allocating an instance of my brain? Can I prevent it? It could be quite a heavy structure

  • StephenTStephenT Fabric for MotionBuilder Posts: 77

    Ok - so after a few tests it seems that a port will always create a default instance of an object. Or at least, my objects are created by a default constructor (which has led to many surprises :))

    Now I know this, its fine - I changed the design to remove my scatterbrain from the graph entirely. It's now managed by python (where it should be), and each agent is set directly on the graph via setArgValue.

    In many senses this is the cleanest approach, and I can see the value in having graphs automatically instancing arguments (did that change recently though? I thought that the default value for ports was 'null').

Sign In or Register to comment.