Strange crush
-
I'll show it but is a bit complicated. I wrote
f
in order to simplify the stuff. Look:template<class V, class T>
T* StructFromVector(V *pvect,
string szName)
{
if (!pvect) return FALSE;for (V::iterator it=pvect->begin();
it!=pvect->end(); it++)
if ((*it)->m_szName==szName)
return *it;return NULL;
}where
string
is the STL one andpvect
will be a pointer to an STLvector
containing pointers to some structures. The real call looks like this:if (!StructFromVector<RIGHTVECTOR2, RightStruct>(&(pUser->
m_vectRights), (char *)bstRight))
{
//...
}Please help! If you can... :(( rechi
Bogdan Rechi wrote: (char *)bstRight) This is not safe, and maybe your problem. A NULL BSTR is often used to represent empty strings and std::string's == operator will generate an access violation on "if ((*it)->m_szName==szName)". Don't know if this is your problem, but soon will be :) Use always (char *)_bstr_t(bstRight) or _com_util::ConvertBSTRToString(bstRight) My latest articles: XOR tricks for RAID data protection Win32 process suspend/resume tool
-
from you "Hungarian" I'd say bstRight is a bstr, can you really cast it to (char*)? And shouldn't you really cast it to a
string
? Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music" -
Steen Krogsgaard wrote: bstRight is a bstr Actually, is
_bstr_t
. But you're right, i could have cast it directly tostring
. Thanx for observation. Unfortunatelly, it doesn't solve the crush problem... rechiI'm still not sure you can cast a
_bstr_t
to astring
(at least not using an explicit cast), they have totally different binary representations, but then again, I'm no expert on either. Anyway, how about stepping through your code, especially through the function, and find the offending line? I'm pretty sure it's not in the line calling the function, it must be somewhere within the function. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music" -
I'll show it but is a bit complicated. I wrote
f
in order to simplify the stuff. Look:template<class V, class T>
T* StructFromVector(V *pvect,
string szName)
{
if (!pvect) return FALSE;for (V::iterator it=pvect->begin();
it!=pvect->end(); it++)
if ((*it)->m_szName==szName)
return *it;return NULL;
}where
string
is the STL one andpvect
will be a pointer to an STLvector
containing pointers to some structures. The real call looks like this:if (!StructFromVector<RIGHTVECTOR2, RightStruct>(&(pUser->
m_vectRights), (char *)bstRight))
{
//...
}Please help! If you can... :(( rechi
If bstRight is no longer needed try doing: if (!StructFromVector(&(pUser-> m_vectRights), (char *)bstRight.Detach() )){//...}
-
Just a hunch.... You are passing the function a BSTR. What BSTR class are you using? It coud be that you now have two objects pointing to the the same string. 1. The object in your vector and 2. The bstring container. When one goes out of scope it deletes the string. Then when the other goes out of scope it tries to delete or use the string which has just been deleted.
-
I'm still not sure you can cast a
_bstr_t
to astring
(at least not using an explicit cast), they have totally different binary representations, but then again, I'm no expert on either. Anyway, how about stepping through your code, especially through the function, and find the offending line? I'm pretty sure it's not in the line calling the function, it must be somewhere within the function. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"Steen Krogsgaard wrote: how about stepping through your code I did it before posting here. I even stepped through the assembly lines; they look like this:
012DDBCF mov dword ptr [ebp-68h],eax
012DDBD2 cmp dword ptr [ebp-68h],0
012DDBD6 jne CRolesUsers2::UserHasRight+1C3h (012ddc53)
33: {
34: for (it=pUser->m_vectRoles.begin(); it!=pUser->m_vectRoles.
012DDBD8 mov ecx,dword ptr [ebp-1Ch]It fails on
jne
when - very intersting! - tries to jump to012DDBD8
and not to012ddc53
. rechi -
Bogdan Rechi wrote: (char *)bstRight) This is not safe, and maybe your problem. A NULL BSTR is often used to represent empty strings and std::string's == operator will generate an access violation on "if ((*it)->m_szName==szName)". Don't know if this is your problem, but soon will be :) Use always (char *)_bstr_t(bstRight) or _com_util::ConvertBSTRToString(bstRight) My latest articles: XOR tricks for RAID data protection Win32 process suspend/resume tool
-
Steen Krogsgaard wrote: how about stepping through your code I did it before posting here. I even stepped through the assembly lines; they look like this:
012DDBCF mov dword ptr [ebp-68h],eax
012DDBD2 cmp dword ptr [ebp-68h],0
012DDBD6 jne CRolesUsers2::UserHasRight+1C3h (012ddc53)
33: {
34: for (it=pUser->m_vectRoles.begin(); it!=pUser->m_vectRoles.
012DDBD8 mov ecx,dword ptr [ebp-1Ch]It fails on
jne
when - very intersting! - tries to jump to012DDBD8
and not to012ddc53
. rechiOoh, my asm is a little rusty! I don't get this - I assume 012DDBCF to 012DDBD6 is the asm for
if (!pvect) return FALSE
but from the asm it seems at it is testing the opposite: jne (jump not equal), that is if dword ptr[ebp-68h] != 0 - or am I remembering jne wrong? Anyway, assuming the asm is right, the "jumping" to 012DDBD8 is because the test failed - that is, pvect != NULL. And the access violation comes as line 34 is executed. Try splitting the line into seperate statements (my stl is even worse than my asm, so this may not work):
V* vstart = pvect->begin();
V* vend = pvect->end();
for (V::iterator it= vstart; it!=vend; it++)
{
...blablablagotta go home now, see you tomorrow. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"
-
Ooh, my asm is a little rusty! I don't get this - I assume 012DDBCF to 012DDBD6 is the asm for
if (!pvect) return FALSE
but from the asm it seems at it is testing the opposite: jne (jump not equal), that is if dword ptr[ebp-68h] != 0 - or am I remembering jne wrong? Anyway, assuming the asm is right, the "jumping" to 012DDBD8 is because the test failed - that is, pvect != NULL. And the access violation comes as line 34 is executed. Try splitting the line into seperate statements (my stl is even worse than my asm, so this may not work):
V* vstart = pvect->begin();
V* vend = pvect->end();
for (V::iterator it= vstart; it!=vend; it++)
{
...blablablagotta go home now, see you tomorrow. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"
Steen Krogsgaard wrote: my asm is a little rusty And so is mine... :rolleyes: Steen Krogsgaard wrote: I assume 012DDBCF to 012DDBD6 is the asm for... I'm sorry, i have been confusing. It was about
if (!StructFromVector<...>(...)
. So, that assembling occurs after the function call ends! Observation: it only crushes when i try to debug it. If i call it without debugging, everything seems ok but i have logical errors and got to end in the step by step hell. X| rechi -
Daniel Turini wrote: Use always (char *)_bstr_t(bstRight) I did. bstRight is
_bstr_t
... rechiBSTR are wide chars. Unless you are compiling in UNICODE this is a very bad cast. Check out WideCharToMultiByte to convert from WCHAR/BSTR to char
-
BSTR are wide chars. Unless you are compiling in UNICODE this is a very bad cast. Check out WideCharToMultiByte to convert from WCHAR/BSTR to char
-
Steen Krogsgaard wrote: my asm is a little rusty And so is mine... :rolleyes: Steen Krogsgaard wrote: I assume 012DDBCF to 012DDBD6 is the asm for... I'm sorry, i have been confusing. It was about
if (!StructFromVector<...>(...)
. So, that assembling occurs after the function call ends! Observation: it only crushes when i try to debug it. If i call it without debugging, everything seems ok but i have logical errors and got to end in the step by step hell. X| rechiThis is about your program accessing random memory, so sometimes it will go right, sometimes wrong, so I wouldn't put too much into it only crashing when you debug it. Anyway, if the program crashes after the template function returns there's two possibilites: your return value is garbage, or your parameters are garbage (or produced garbage during cleanup). I still very much suspect your (char*) cast - try moving it out of the
if
statement and see if the crash-point moves. I would copy/convert the_bstr_t
to astring
before the function call. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"