FCriticalSection Lock causes crash

Hi there,

I got trouble on using FCriticalSection::Lock in my project… It just causes crash.

Here is how I use it :

void FStreamingContent::Lock()
{
    if (mutex)
        mutex->Lock();
}

mutex is a FCriticalSection*, and is initialized in my constructor by a simple new.

Anyone have an idea of what is going on ?

Thanks !

Without a callstack and the actual crash message it is impossible to say what happened. That being said, it looks like mutex is a pointer to a critical section - did you actually create an instance? Perhaps the pointer is still null or invalid, and dereferencing it in mutex-lock() causes the crash?

By the way, the most convenient way to use critical sections is in conjunction with scope locks:

class FStreamingContext
{
    FCriticalSection CriticalSection; // <-- not a pointer, but an actual instance!
}

void FStreamingContext::Lock()
{
    // unprotected things here...

    {
        FScopeLock Lock(&CriticalSection);
        // do protected things here...
    }

    // unprotected things here...
}

Hi gmpreussner,

Thanks for the answer. I changed my code to use now FScopeLock, and a instance instead of a pointer, but I still have the same problem :confused:

Here is the crash report

My function SendData just try to lock for now.

To help understand what is going on, I’ll explain what my code actually does

My ContentStreaming class is a FRunnable child :

class FStreamingContent : public FRunnable
{
public:
	FStreamingContent(FCriticalSection &mutex, std::queue<s_Content> &queue);
	virtual ~FStreamingContent();
	
	FRunnableThread*			thread;
	std::queue<s_Content>		data;
	static FStreamingContent*	inst;
	FCriticalSection			mutex;

	static FStreamingContent*	CreateInstance(const std::string &fileName, FCriticalSection &mutex, std::queue<s_Content> &queue);
       // Implements FRunnable functions
};

Here is how I call createInstance :

FCriticalSection mutex;
std::queue<s_Content> data;

FStreamingContent* streamingContent = FStreamingContent::CreateInstance("myfile.twd", mutex, data);

Finally in SendData(), I try to access to my mutex and lock it via FScopeLock

Ok, I moved my mutex to a global and now it works fine.

Thanks for the help !

i know its been years but for people who have this problem i think this is a better solution than global mutex
source
mutable FCriticalSection DataGuard;