P
P
Pavel K2016-12-26 01:12:12
Qt
Pavel K, 2016-12-26 01:12:12

Why does the link to the pointer give the wrong address?

Greetings!
There is class A

class A: public InterfaceA
{
   A() {}
   ...
   void f() {  qDebug()<<"fA"; }
}

Inherited from pure virtual InterfaceA:
class InterfaceA
{
   virtual void f()=0;
}

What I don't understand is what follows:
A * clA = new A();
InterfaceA * iA = clA;  

typedef InterfaceA * TIA;
void test(const TIA & I ) {
   //-- здесь "I" имеет не верный адрес, всегда на 10 больше, чем до вызова
} 
    
test(iA);

Now the questions are:
1. Why is the pointer by reference replaced when calling test, and why is it 10 more?
2. Amendment - does not come: Why, if instead of iA = clA cast iA = static_cast(clA); behavior returns to normal?
3. I thought that an explicit cast when downgrading a class is not required, am I wrong?
4. What could be the consequences, because if you write iA.f() the call will be correct?
5. If such a cast is not safe, why didn't the compiler issue any warnings or errors at all?
UPDT1: I
found out that if class A is inherited from QObject, then in the debugger, when viewing the clA variable, the address of QObject will be, for example , 0x9d6c78 , and the address of InterfaceA 0x9d6c70
A * clA = new A();
and the address of the clA variable itself will be 0x9d6c78
If you do InterfaceA * iA =static_cast(clA); the address of the iA variable will be 0x9d6c70 O_O this
is provided that class A is declared as: public QObject, public InterfaceA
UPDT2:
It turned out that the above feature does not happen immediately, but after a certain number of actions and inside the Qt template magic, I find out where.
UPDT3:
In general, why I started all this - I tried to catch a strange bug (a segfault in the depths of qml, where the debugger cannot crawl).
Why the pointer changes: most likely due to multiple inheritance + Qt tricks.
According to the bug, the search for a solution is in progress:
QVariant, when filling the private data structure, tries to fill the QObject * o field, but breaks off because type casting does not work (the pointer is not the same), and when somewhere in the program (most likely in the bowels of qml) you need to do variant.value(), then the segfault is caught.
It remains to find exactly where the filling is going on and somehow contribute to the correct cast.
Why does he think that InterfaceA is inherited from QObject - I forced him to work int idd= QMetaType::type("InterfaceA*"); void *myClassPtr = QMetaType::create(idd);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Adamos, 2016-12-26
@PavelK

Why, excuse me, why do you have a constant reference to a pointer in your arguments?
That's why exactly here - a constant reference?
IMHO, you are confusing yourself.
Pass the pointer by value - the code will be simplified, and you will figure it out yourself.
Now, apparently, you manage to get the address of the link itself instead of a pointer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question