InterprocessConnection crashes occasionally during destruction

Sorry, I’m a bit busy this week but I will try to take another look at this next week.

Added some comments to the PR

I’ve pushed some fixes for the NamedPipe implementations. I think the specific crash you were seeing should be fixed by this commit:

Sorry for the delay in getting this fixed - this was quite tricky to track down!

Thank you very much, however…
I still have problems after testing…
The crash problem seems to be solved now, but it seems that a deadlock problem has been introduced…
Change the previous test code to the following (still based on the Hello World GUI project, modify its MainComponent.h), it will deadlock after running for less than 10 seconds, and the main thread will be stuck…
The following is the complete content of the new MainComponent.h:

#pragma once

#include <JuceHeader.h>

class TestPipeConnection : public juce::InterprocessConnection
{
public:
    TestPipeConnection(bool isCreator) : InterprocessConnection(true)
    {
        if (isCreator) createPipe("1234", -1, true);
        else
        {
            connectToPipe("1234", -1);
            sendMessage(juce::MemoryBlock("1234", 4));
        }
    }

    ~TestPipeConnection() override
    {
        disconnect(-1, Notify::no);
    }

    void connectionMade() override{}
    void connectionLost() override {}

    void messageReceived(const juce::MemoryBlock &message) override
    {
        sendMessage(message);
    }
};

class TestConnections
{
public:
    TestConnections()
    {
        creatorPipe.reset(new TestPipeConnection(true));
        connectorPipe.reset(new TestPipeConnection(false));
    }

    ~TestConnections()
    {
        connectorPipe = nullptr;
        creatorPipe = nullptr;
    }

private:
    std::unique_ptr<TestPipeConnection> creatorPipe, connectorPipe;
};

class TestThread : public juce::Thread
{
public:
    TestThread() : Thread("TestThread")
    {
        startThread();
    }

    ~TestThread() override
    {
        stopThread(-1);
    }

    void run() override
    {
        while (!threadShouldExit())
        {
            TestConnections conn;
            sleep(50);
        }
    }
};

//==============================================================================
/*
    This component lives inside our window, and this is where you should put all
    your controls and content.
*/
class MainComponent  : public juce::Component
{
public:
    //==============================================================================
    MainComponent();
    ~MainComponent() override;

    //==============================================================================
    void paint (juce::Graphics&) override;
    void resized() override;

private:
    //==============================================================================
    // Your private member variables go here...
    TestThread testThread;

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};

I still hope that you can continue to solve this problem, thank you very much.

Thanks for reporting. I think this patch should resolve the deadlock:

2 Likes

Now it looks like all these problems are solved, thank you very much. :grinning:

1 Like