Rhino crash with multithreaded dll


#1

I’m developing a Rhino plugin and I’m using a multithreaded dll. The dll doesn’t manipulate rhino objects at all - once the objects are passed to the multithreaded part of it, it does its thing, and then passes them back. However, I’m getting a very consistent crash…has any of this come up before?


(Menno Deij - van Rijswijk) #2

Which programming language? What multithreading model? Can you post the code? Have you tried simplifying the code to pinpoint the problem?

A consistent crash in my experience (using C# with RhinoCommon) is usually caused by a stack overflow due to infinite recursion or due to an access violation where protected memory is being accessed.


(Dale Fugier) #3

Hi Danial,

It sounds like you know that Rhino is not thread safe. When adding objects to Rhino, make sure you do so on the main application thread.

– Dale


#4

So, after some testing, it turns out that a part of the problem has to do with the use of System.Timer. For instance, I’d like to create an infinite loop:

while(true)
{
  SimulateObjects()
  UpdateObjectPositions()
}

and I’d like to be able to interact with Rhino while this is going on. I set up a callback with System.Timer which allowed me to run the simulation while interacting with the UI, but I started to get all sorts of crashes (Memory Access Violation). I suspect this is because I was creating Rhino objects out of the timer callback function, which was probably on a different thread.

Any thoughts on how to work around this?


(Menno Deij - van Rijswijk) #5

You may have more luck with DispatcherTimer (System.Windows.DispatcherTimer). This timer was introduced with WPF and will put events on the Dispatcher queue for evaluation on the main (GUI) thread.

I haven’t tried this with putting objects in Rhino in the way you describe though.

DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(10);
timer.Tick += OnTimerTick;
timer.Start();

private void OnTick()
{
   // do stuff
   // call timer.Stop() to stop firing tick events
}

more info see MSDN http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer(v=vs.110).aspx


#6

Just tried it out - works much better. Thanks!


(Menno Deij - van Rijswijk) #7

Ok, cool. Good to know that works with DispatcherTimer.