# MessageManagerLock problem

**URL:** <https://forum.juce.com/t/messagemanagerlock-problem/45831>\
**Category:** General JUCE discussion\
**Created:** [May 4, 2021, 10:46am UTC](https://forum.juce.com/t/messagemanagerlock-problem/45831 "2021-05-04T10:46:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PieterS](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pieters/32/10588_2.png) [@PieterS](https://forum.juce.com/u/PieterS)\
**Post date:** [May 4, 2021, 10:46am UTC](https://forum.juce.com/t/messagemanagerlock-problem/45831/1 "2021-05-04T10:46:19Z")

</div>

I have a thread that needs to do some gui stuff and then ends. The run method looks something like this:

```
void MyDialog::run()
{
    MessageManagerLock mmlock;

    label1->setText("some text", dontSendNotification);
}

```

I get an assertion here:

```
void Font::getGlyphPositions (const String& text, Array<int>& glyphs, Array<float>& xOffsets) const
{
    // This call isn't thread-safe when there's a message thread running
    jassert (MessageManager::getInstanceWithoutCreating() == nullptr
               || MessageManager::getInstanceWithoutCreating()->currentThreadHasLockedMessageManager());

```

Debugging this, it seems that getInstanceWithoutCreating is returning a non zero value and that the currentThreadHasLockedMessageManager is false.

Am I doing something wrong? Why isn’t the Message thread locked?

---

<div class="post-metadata">

**Author:** ![otristan](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@otristan](https://forum.juce.com/u/otristan)\
**Post date:** [May 4, 2021, 11:15am UTC](https://forum.juce.com/t/messagemanagerlock-problem/45831/2 "2021-05-04T11:15:08Z")

</div>

Better use MessageManager::callAsync instead no ?

[https://docs.juce.com/master/classMessageManager.html#acfe6da5271c2ab76fdb0414b235ed095](https://docs.juce.com/master/classMessageManager.html#acfe6da5271c2ab76fdb0414b235ed095)

---

<div class="post-metadata">

**Author:** ![PieterS](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pieters/32/10588_2.png) [@PieterS](https://forum.juce.com/u/PieterS)\
**Post date:** [May 4, 2021, 11:49am UTC](https://forum.juce.com/t/messagemanagerlock-problem/45831/3 "2021-05-04T11:49:59Z")

</div>

yes, that seems to work, but why doesn’t locking the Message thread work? What is the best practice here?

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [May 4, 2021, 12:02pm UTC](https://forum.juce.com/t/messagemanagerlock-problem/45831/4 "2021-05-04T12:02:17Z")

</div>

I can only agree to avoid the MessageManagerLock whenever possible, especially in plugins, where you share the message-thread with the host, it is very easy to accidentally cause a deadlock (or not being able to lock at all). Better use async-callbacks whenever possible. (and check the existence of the objects which you are calling in the callback with a safe-pointer)
