Problems listening to vtkMRMLCommandLineNode events from loadable module

Hi all:

I am working on a loadable module that does background processing using CLIs. I am connecting callback functions in the widget to the end of the CLI processing using qvtkReconnect (as shown):

    qvtkReconnect(d->cmdNode, vtkMRMLCommandLineModuleNode::StatusModifiedEvent, 

Unfortunately, I am running into access violation errors in vtkObject when it tries to process the StatusModified Event when the CLI is completed. This exception occurs in:

vtkSubjectHelper::InvokeEvent(unsigned long event, void *callData,vtkObject *self)

at line 601. It appears that one of the observers linked to the node is bad/invalid, but I do not know how it happens. Nothing is being done with the node between invoking the CLI and picking up the Completed signal.
Also, this error does not occur every time. It is very intermittent.

Is there a better/more reliable way that I should be listening for the end of CLI processing?


Could you check if your callback called in the main thread or in the CLI’s background processing thread?

The callback is on the main thread.

Without seeing the source code, it is really difficult to guess what the problem can be. Is there a chance that you can upload the source code of your extension to GitHub and copy-paste the link here?

Sure. It is already on GitHub:

You can see one of the command line node connections here:

I’m in the middle of bugfixes and refactoring, so some of the code is rather… convoluted. I was really just hoping that someone had seen a similar error.

Great, thank you!

In the line that you highlighted, you should probably use qvtkReconnect instead of qvtkConnect, otherwise the connection to the old node may not be removed when you connect a new node.

Every simple C pointer as a class member variable is a potential point of failure and you have many of them. If you own the object or want to make sure a referenced object is not destroyed while you still want to use it, then you can use vtkNew<> or vtkSmartPointer<>. If you don’t want to prevent deletion of the object but you want to make sure that the object is valid, then use vtkWeakPointer<> and always check if the pointer is valid before using it.

Since Subject hierarchy node is introduced, model hierarchy has become kind of obsolete, since Subject hierarchy is much more flexible (can deal with any kind of nodes, can be extended using custom plugins, etc.) and simpler to use (no need to create nodes to just create folders or add a node to the subject hierarchy tree). I would recommend to switch to that instead of using model hierarchy.

If you’ve got rid of simple C pointers from member variables and finished up with your cleanup and you still have this problem then let me know and I have a look at the code again.

Thanks! Will let you know how it goes.