Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::SoundRecorder derived class  (Read 1880 times)

0 Members and 1 Guest are viewing this topic.

Visor

  • Newbie
  • *
  • Posts: 21
    • View Profile
sf::SoundRecorder derived class
« on: December 11, 2017, 08:58:26 pm »
Hello everyone,

I'm trying to make a derived class of sf::SoundRecorder, but I get an error at the start in the constructor:

Code: [Select]
class cMicRecord : public sf::SoundRecorder
{
public:
cMicRecord();
~cMicRecord();

bool isRecording();

protected:
virtual bool onStart();
virtual bool onProcessSamples(const sf::Int16* samples, std::size_t sampleCount);
virtual void onStop();

private:
bool m_bRecording;
bool m_bOperational;
};

Quote
Exception is thrown at 0x77D0A225 (ntdll.dll) in ******.exe : 0xC0000005 : Access violation when writing at 0x00000004.
(Screenshot)

I really don't understand how is it possible. I guess I did a mistake in the implementation ?
Is it impossible to make a constructor of a derived class wich has its constructor in protected ?

« Last Edit: December 13, 2017, 11:06:35 am by Visor »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: sf::SoundRecorder derivated class
« Reply #1 on: December 11, 2017, 09:23:01 pm »
Where's the implementation?

Also make sure you're not mixing libraries of different architecture (32-bits vs 64-bits).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Visor

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: sf::SoundRecorder derived class
« Reply #2 on: December 11, 2017, 09:35:02 pm »
Hi eXpl0it3r, thanks for reply.

If I delete the cMicRecord class, there is no crash. I guess it would crash even whitout my derived class if I had mixed 64 and 32 bits libraries. Also, I remember I only downloaded the x86 version. So, I don't think the problem comes from here.
I thought it wasn't usefull (there is nothing in it), there is the complete implementation:

Code: [Select]

cMicRecord::cMicRecord()
{ // <- Error here
m_bRecording = false;

std::vector<std::string> availableDevices = sf::SoundRecorder::getAvailableDevices();

if (availableDevices.size() > 0
&& this->setDevice(availableDevices[0]))
m_bOperational = true;
else
m_bOperational = false;
}

cMicRecord::~cMicRecord()
{
m_bRecording = false;
m_bOperational = false;

stop();
}

bool cMicRecord::onStart()
{
this->setProcessingInterval(sf::milliseconds(100));

if (m_bOperational
&& this->isAvailable())
{
m_bRecording = true;

return true;
}
else
return false;
}

bool cMicRecord::onProcessSamples(const sf::Int16* samples, std::size_t sampleCount)
{
//const sf::Int16* samples = reinterpret_cast<const sf::Int16*>(static_cast<const char*>(packet.getData()) + 1);
//std::size_t      sampleCount = (packet.getDataSize() - 1) / sizeof(sf::Int16);

//sf::Lock lock(SINGLETON.m_micStreamer.m_mutex);
//std::copy(samples, samples + sampleCount, std::back_inserter(SINGLETON.m_micStreamer.m_samples));

return true;
}

void cMicRecord::onStop()
{
m_bRecording = false;
}

bool cMicRecord::isRecording()
{
return m_bRecording;
}

In another class I derived the sf::SoundStream class and it produces the same problem. Here too, if I delete the constructor, it doesn't crash anymore... And even if the constructor has nothing in it, it crashes.

EDIT 1:
I downloaded the last SFML version on Git, built it, etc.
Now I can see better where it crashes:
Code: [Select]
> *****.exe!sf::priv::MutexImpl::lock() [EnterCriticalSection(&m_mutex);]
  *****.exe!sf::Mutex::lock() [m_mutexImpl->lock();]
  *****.exe!sf::Lock::Lock(sf::Mutex & mutex) [m_mutex.lock();]
  *****.exe!sf::AlResource::AlResource() [Lock lock(mutex);]
  *****.exe!sf::SoundRecorder::SoundRecorder() [CONSTRUCTOR]
  *****.exe!cMicRecord::cMicRecord() [CONSTRUCTOR]


It crashes here, it appears that m_mutex is NULL.
In my implementation I have a m_mutex too and if I remove it, it crashes anyway at this line, because of m_mutex implemented in sf::AlResource (for "Very first global device initialization").
Code: [Select]
"SFML/System/Win32/MutexImpl.hpp"

void MutexImpl::lock()
{
    EnterCriticalSection(&m_mutex); // <-- err
}


EDIT 2:
I removed everything in my class, except the (empty) constructor and the two pure virtual member functions (empty too).
It crashes anyway when locking the mutex of sf::AlResource.

EDIT 3:
Okay, it's not about a constructor. It seems that the mutex implemented in sf::AlResource isn't properly initialized. I tried to insert my derived class a way its constructor is called the later I could but even if its mutex isn't NULL, it crashes anyway. So instead of a composition model like, I tried an external agregation with a pointer of my derived class (and allocate at Singleton runtime). And it works...
So I think the derived class constructor calls the sf::AlResource constructor too soon, therefore its mutex isn't totally initialized.

I'll try to find why.

EDIT 4:
I still don't know why the mutex pre-implemented in sf::AlResource isn't properly initialized when its constructor is called, but because I need my derived class to be agregated to a class used a long time after the game's starts, the problem isn't anymore.

Anyway, if somebody know what's the problem and could explain me, I would be glad to understand.
« Last Edit: December 20, 2017, 01:02:55 pm by Visor »

 

anything