Issue
In my app, I use of C++ code to complie.There is a function in xxx.cpp like this:
void Cache::addRequest(SREQUEST req)
{
m_reqs.push_back(req);
}
When my app call this function, it's throw the run-time error:
Fatal signal 7 (SIGBUS), code 1, fault addr 0xb8509ce1 in tid 32190 (com.example)
The attribute "m_reqs" is a vector object, which decleared in head file(xxx.h):
vector<SREQUEST> m_reqs;
struct SREQUEST
{
SREQUEST()
{
code = "";
oldLen = 0;
}
TimeRound tr; // enum(int type)
std::string code;
int oldLen;
};
I use singleton mode with the class "Cache".
Cache* Cache::getInstance()
{
static Cache* _instance;
// init
if (_instance == NULL)
{
_instance = new Cache();
}
// return the pointer
return _instance;
}
The error is only throwed by real phone, not simulator.I found the solution, but I didn't know what causes this question.I mean it's always appear:
SREQUEST req;
req.code = "123";
req.tr = 1;
req.oldLen = 0;
Cache::getInstance()->addRequest(req); // cause error
I fix it like:
typedef vector<SREQUEST> VEC_SREQUEST;
VEC_SREQUEST* Cache::getVector()
{
if(v_reqs == 0) // int type
{
vector<SREQUEST>* TEMP = new vector<SREQUEST>();
void* x = (void*)TEMP;
v_reqs = (int)x;
}
void* temp = (void*)v_reqs;
VEC_SREQUEST* res = ((VEC_SREQUEST*)temp);
return res;
}
void Cache::addRequest(SREQUEST req)
{
VEC_SREQUEST* vec = getVector();
vec->push_back(req);
}
In my cpp, I have been extensively using the code "xxx.push_back(something)". Use this solution the job that needs a lot of time.
So what causes "xxx.push_back" can not work? And without a large modify how to fix it?
Solution
I get the reason.
I declear the vector object at the bottom of xxx.h file.The function statement is wroted at first.
Exchanged the postion, everything is ok!
Answered By - user1010439
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.