Question

I have a problem with returning a const char* from two functions from a class, by some reason the first value is a replica of the second value or some values is wrong, but both values returned are from different pointers, in this case two QMap, the following is the code of the two functions:

const char* SomeClass::getSignal(QString signalName)
{
    QString signalsignature = this->signals.value(signalName);
    signalsignature.prepend(QString::number(QSIGNAL_CODE));
    QByteArray ba = signalsignature.toLatin1(); // i try toUTF8 and to Local8Bit
    const char* signalformated = ba.constData();
    return signalformated;
}

const char* SomeClass::getSlot(QString slotName)
{
    QString slotsignature = this->slots.value(slotName);
    slotsignature.prepend(QString::number(QSLOT_CODE));
    QByteArray ba = slotsignature.toLatin1();
    const char* slotformated = ba.constData();
    return slotformated;
}

The this->slot and the this->signals are QMap<QString, QString> that save slots and signals signatures ( somesignal(int) or someslot(bool) with keys somesignal, someslot respectively).

The class that i use is loaded from a DLL by QLibrary using an interface, all works fine using other functions from it, but using these functions like this:

const char* signal = someclassinstance->getSignal(tr("clicked"));
const char* slot = someclassinstance->getSlot(tr("onclicked"));

cout << "connecting signal " << signal << " to slot " << slot << endl;

this show me:

connecting signal clicked to slot rc

and the error when i use QObject::connect

Object::connect: Use the SLOT or SIGNAL macro to connect NovaRadioButton:: rc

I fill the QMaps is some function with:

signals.insert(methodname,QString(metamethod.signature()));

I don't know what i am doing wrong or if maybe is a bug in Qt functions from QString, thanks for your help.

Was it helpful?

Solution

When your functions exit, the ba objects are destroyed. The pointers you return point inside memory locations of no longer existing objects. This is the basic (and well known) error of returning pointers to locals:

char* foo()
{
    char c = 'a';
    return &c;
}

The fact that you're dealing with a QByteArray doesn't matter; it's still an automatic, local variable and thus gets destroyed on function exit.

You should instead return a QByteArray directly, not a char*. This is still efficient, since QByteArray uses implicit sharing.

OTHER TIPS

No bug, you are returning a pointer to the internals of an object that has been destroyed.

Rewrite your functions like this

QByteArray SomeClass::getSignal(QString signalName)
{
    QString signalsignature = this->signals.value(signalName);
    signalsignature.prepend(QString::number(QSIGNAL_CODE));
    return signalsignature.toLatin1(); // i try toUTF8 and to Local8Bit
}

QByteArray should be preferred to char*. From the Qt docs

The QByteArray class provides an array of bytes.

QByteArray can be used to store both raw bytes (including '\0's) and traditional 8-bit '\0'-terminated strings. Using QByteArray is much more convenient than using const char *. Behind the scenes, it always ensures that the data is followed by a '\0' terminator, and uses implicit sharing (copy-on-write) to reduce memory usage and avoid needless copying of data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top