2 개의 다른 Qt Apps와 연결된 라이브러리 (testlib-lib.so)가 있습니다. 내 문제는 그것을 초기화 한 후 공유 메모리를 업데이트 할 수 없다는 것입니다. 공유 초기화 후Qt QSharedMemory 공유 메모리 위치를 업데이트하는 방법
void Control::updateText() {
// Create seed for the random
// That is needed only once on application startup
QTime time = QTime::currentTime();
qsrand((uint)time.msec());
// Get random value between 0-100
int randomValue = qrand() % ((1000 + 1) - 2) + 2;
// Create the shared memory and load some data
TestLib loadData;
QString randomText = QString("%1 test asdf %2 test asdf %3 test asdf %4").arg(randomValue).arg(randomValue).arg(randomValue).arg(randomValue);
loadData.writeToSharedMemory(randomText);
}
을 :
// Create the shared memory and load some data
TestLib loadData;
loadData.createInitialSharedMemory();
loadData.writeToSharedMemory("12345 init shared memory");
// This is the key
QSharedMemory sharedAudioMemory("globalAudioBufferSharedMemory");
// Attempt to attach to shared memory for the audio buffer
if (!sharedAudioMemory.attach()) {
//If an attempt of reading from the shared memory before data is written
qDebug() << "ERROR: Failed to attach to shared memory...";
return -1;
}
// Define a buffer to read the data in
QBuffer buffer;
QDataStream in(&buffer);
QString text;
// Lock the memory and read the data
sharedAudioMemory.lock();
char* pointer = (char*)sharedAudioMemory.constData();
buffer.setData((char*)sharedAudioMemory.constData(), sharedAudioMemory.size()); // Sets the contents of the internal buffer to be the first size bytes of data.
buffer.open(QBuffer::ReadOnly);
in >> text;
sharedAudioMemory.unlock();
sharedAudioMemory.detach();
Qt는 응용 프로그램 B는, 수행하여 공유 메모리를 업데이트하려고 시도합니다 : 그것은() 주의에
void TestLib::createInitialSharedMemory() {
// Create shared memory
qDebug() << "Creating new shared memory...";
sharedAudioMemory.setKey("globalAudioBufferSharedMemory");
// First, test whether a shared memory segment is already attached to the process.
// If so, detach it
if (sharedAudioMemory.isAttached())
{
sharedAudioMemory.detach();
}
// Try to create memory of our required size
int sizeOfSharedData = 60 * 8000 * 2; // 60 Seconds x 8 KHz * 2 Bytes (signed short)
if (!sharedAudioMemory.create(sizeOfSharedData))
{
qDebug() << "ERROR: Failed to Allocate Shared Memory of size: " << sizeOfSharedData;
}
}
void TestLib::writeToSharedMemory(QString text) {
// Create a buffer and data stream and some text to shove in there
QBuffer buffer;
buffer.open(QBuffer::ReadWrite);
QDataStream out(&buffer);
out << text;
int size = buffer.size();
if (!sharedAudioMemory.attach()) {
// If an attempt of reading from the shared memory before data is written
qDebug() << "Cannot attach to shared memory to update!";
}
// Write into the shared memory
sharedAudioMemory.lock();
qDebug() << "Writing data to buffer: " << text;
char *to = (char*)sharedAudioMemory.data();
const char *from = buffer.data().data();
memcpy(to, from, qMin(sharedAudioMemory.size(), size));
sharedAudioMemory.unlock();
}
Qt는 응용 프로그램 A는 다음이있다 메모리를 "12345 init shared memory"로 설정하면 텍스트 저장소를 업데이트 할 수 없습니다. App B에서 updateText() 메서드를 호출하면 라이브러리 메서드 writeToSharedMemory()가 호출되고 메서드의 qDebug()가 공유 메모리에서 업데이트하려는 텍스트를 인쇄하지만 시도한 후에 다음 코드를 호출 할 때
// Attempt to attach to shared memory for the audio buffer
if (!sharedAudioMemory.attach()) {
// If an attempt of reading from the shared memory before data is written
qDebug() << "ERROR: Failed to attach to shared memory...";
}
// Define a buffer to read the data in
QBuffer buffer;
QDataStream in(&buffer);
QString text;
// Lock the memory and read the data
sharedAudioMemory.lock();
char* pointer = (char*)sharedAudioMemory.constData();
buffer.setData((char*)sharedAudioMemory.constData(), sharedAudioMemory.size()); // Sets the contents of the internal buffer to be the first size bytes of data.
buffer.open(QBuffer::ReadOnly);
in >> text;
sharedAudioMemory.unlock();
sharedAudioMemory.detach();
// Print the data
qDebug() << "SHARED TEXT IS: " << text;
왜 내가 공유 메모리를 업데이트하는 것 can'y : 공유 메모리를 업데이트, 항상 기원 inital 텍스트 "12345 초기화하기 공유 메모리를"인쇄? 내가 도대체 뭘 잘못하고있는 겁니까? 감사합니다 -
이것이 문제의 원인인지는 모르겠지만 detach()에 대한 모든 호출은 설명서에 따르면 두 프로세스가 분리되었을 때 나쁘다는 생각이 듭니다. ' 즉, 내용물이 파괴됩니다. ' –