地址0x20ec8348e5894855未堆叠,malloc'd或(最近)免费

时间:2017-04-17 14:20:31

标签: c++ callback shared-libraries

我有两个自己的库,例如“IDA”和“XIDA”。我希望让他们通过回调函数进行通信。

IDA:
ida.h,ida.cpp,ida_a.h,ida_a.cpp

西达:
xida.h,xida.cpp,xida_a.h,xida_a.cpp

IDA:
ida_a.h:

class IDA_A
{
private:
    XIDA *mXIDA;
public:
    static int IDA_Callback(void* this_ptr, void *vPara ){
        std::cout << "IDA -- " << "INCOMING CALLBACK == NULL" << std::endl;
    }
}

ida_a.cpp:

void IDA_A::Init(std::string vIdent){
    this->mXIDA = new XIDA( (void*) &this->IDA_Callback);
}

现在XIDA:

xida.h:

class XIDA{
public:
    XIDA(void *vPara);
};

xida.cpp:

XIDA_A m_XIDA_A;

XIDA::XIDA(void *vPara){
    std::cout << "TEST XIDA ---- A" << std::endl;
    m_XIDA_A.Init(vPara);
}

xida_a.h:

typedef int(*IDACallback) (void*, void*);

class XIDA_A{
private:
    IDACallback mIDACallback;
public:
    void Init(void *vPara);
}

xida_a.cpp:

1 void XIDA_A::Init(void *vPara){
2 int nErr = 0;
3    cout << "XIDA -- INIT A **************************************************  " << vPara << endl;
4   try{
5       this->mIDACallback = *static_cast < IDACallback* > (vPara);
6   } catch (...){
7       nErr = 1;
8    }
9   if(nErr == 0){
10      cout << "XIDA -- INIT B **************************************  " << this->mIDACallback << endl;
11      this->mIDACallback(NULL, NULL); // <------ HERE IS THE ERROR
12  }
13 }

正常呼叫(通过终端和路径/ bin)时,错误为:

Speicherzugriffsfehler (engl. memory access error <?>)

Valgrind:

XIDA -- INIT A **************************************************  0x4e40a19
XIDA -- INIT B **************************************  1
==20001== Jump to the invalid address stated on the next line
==20001==    at 0x20EC8348E5894855: ???
==20001==    by 0x504C690: XIDA::XIDA(void*) (xida.cpp:10)
==20001==    by 0x4E405CB: IDA_A::Init(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) (ida_a.cpp:37)
==20001==    by 0x4E3FE9F: IDA::IDA(char const*) (ida.cpp:13)
==20001==    by 0x401932: main (main.cpp:13)
==20001==  Address 0x20ec8348e5894855 is not stack'd, malloc'd or (recently) free'd

所以,现在我知道错误在哪里,但我不知道如何解决它。

1 个答案:

答案 0 :(得分:1)

IDA_A::Init实际上并未声明,但除此之外,您将vPara(包含指向函数的指针)转换为指向函数的指针,然后取消引用它:

this->mIDACallback = *static_cast < IDACallback* > (vPara);

应该是

this->mIDACallback = static_cast < IDACallback > (vPara);