Creating objects from within a thread crashing max.

Mar 1, 2013 at 9:37am

Creating objects from within a thread crashing max.

I’m using the Max sdk for a small, networked, multi-threaded application. All these factors make it a hellish nightmare as it is, but the benefits outweigh the heartache.

I’m attempting to create an object within a thread, and upon reaching the code:

t_object* patcher;

object_obex_lookup( &m_ob, gensym( "#P" ), &patcher );

 t_object* gain = newobject_sprintf( patcher, "@maxclass gain~ @patching_rect 100. 100. 20. 100."); /* This is the line that crashes */

Within a thread, the application crashes.

When I attempt to put this code within the perform method that is common with MSP objects, it also crashes.

However, if I put the above code within one of my methods that is communicated with Max via messages (e.g., bang), the gain~ object appears.

There’s a few routes I could take with this, but I’d like to keep the multithreaded aspect alive and running.

I’ve also attempted to use critical_enter and critical_exit to no avail.

So, can Max objects not be created outside of a main thread or something?

I’m also using the C++ wrapper API provided on googlecode for this as well.

#66792
Mar 1, 2013 at 12:08pm

Hi,

1. Avoid expensive calls in the perform routine.

http://www.rossbencina.com/code/real-time-audio-programming-101-time-waits-for-nothing

2. More generally there is a lot of things that are not allowed to do from a custom thread. Actually my opinion is to not call Max function at all ;-)

http://cycling74.com/forums/topic.php?id=41800

http://cycling74.com/forums/topic.php?id=43541

3. So the trick is to wrap your code with a t_clock and schedule it from your thread with a zero delay (or use variants as qelem or defer).

http://cycling74.com/sdk/MaxSDK-6.0.4/html/chapter_scheduler.html

http://cycling74.com/sdk/MaxSDK-6.0.4/html/group__sched.html

#240442
Mar 1, 2013 at 1:59pm

+1, and I also think that creating objects in a patcher should only be done from the main thread
aa

#240443
Mar 1, 2013 at 2:58pm

Hi,

Yep ; there are behaviours that can be hard to understand if you consider the max SDK as a library, as Boost is for C++ ; AFAIK the t_dictionary seems to be mainly designed to be used in the main thread (as i discovered late) and (as pointed out by Andrea) object creations (in the patcher or not) and object destruction too (at least with the max 5 SDK).

http://cycling74.com/forums/topic.php?id=29488

Forgot to say that there is an example in the SDK about thread : “simplethread” ;-)

#240444
Mar 1, 2013 at 5:17pm

Thanks for all your guy’s responses.

I fixed it by taking Nicolas’s advice and wrapping the code around a t_clock and scheduling it very quickly.

I’m quite new to programming with Max but it’s very awesome so far. :D

#240445

You must be logged in to reply to this topic.