奇怪的字符串相互写入 [英] Weird strings getting written over each other

查看:52
本文介绍了奇怪的字符串相互写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我希望有人可以帮我这个,因为我用完了

选项转向。


我几乎解决了我的正则表达式功能。基本上它工作正常

如果定义了unicode。然而,它在ANSI模式下不能正常工作,因为它必须使用MultiByteToWideChar和WideCharToMultiByte。

我发现正则表达式部分工作正常。至于
我可以告诉正则表达式代码正确解析它接收到的内容,并以正确的格式输出它。然而,计算它将会收到的错误。

基本上,两件奇怪的事情正在发生,令我感到困惑:

1)当我使用/ Zi和/ D_DEBUG运行它时在WinDbg下定义,但*不*被调试

,然后模式扩展好了。文本扩展好了。

但是当文本扩展时,它会用最后一个字覆盖''pattern''!

以下输出为:

模式扩展为\w +;

文本扩展为单词集合

但现在模式单词


bPattern是单词

bText是单词集合


2)当它在WinDbg下调试*时运行,那么字符串

被复制好了!没有覆盖,输出为:

模式扩展为\w +>

文本扩展为单词集合

但是模式现在是\w +


bPattern是\w +

bText是" ;单词集合

匹配找到A 0为1

找到匹配项Collection在2为10

匹配找到的of在13为2

匹配找到的单词这是完全正确的 - 这是唯一的事情,当我按F10步骤

而不是任何免费的电话时 - 它崩溃了!无法进一步调试程序或

任何东西。


我担心两件事。一,为什么''免费''会让WinDbg崩溃,而不是单独打造这个程序,但更令人担忧的是,为什么MultiByteToWideChar

会覆盖我的字符串的内存,当这种行为在

WinDbg中无法重现时。我之前使用过mbstowcs_s,它完全一样

的东西。我甚至检查了WinDbg中的内存位置,它们附近每个

其他但相距足够远(IIRC相差40个字节)。


任何人都可以建议解析度?如果有人能够重现这一点,那将是非常好的。


这是当前代码:

typedef void(CALLBACK * MatchEvaluator)(LPCTSTR值,长开始,长

长度,LPVOID extradata);

BOOL MatchWords(LPCTSTR模式,LPCTSTR文本,MatchEvaluator pfnMECB,

LPVOID extradata)

{

IRegExp * regexp = NULL;

IMatchCollection * pMatches = NULL;

IMatch * pMatch = NULL;

long nMatchCount,nMatch;

HRESULT hr;

BSTR bText,bPattern;

LPWSTR wtext,wpattern;

#ifdef _UNICODE

wtext =(LPWSTR)text;

wpattern =(LPWSTR)pattern;

#else

size_t textlen = _tcslen(text)+ 1,patlen = _tcslen(pattern)+ 1,

convtext,convpat;

wtext =(LPWSTR)malloc(textlen),

wpattern =(LPWSTR)malloc(patlen);


MultiByteToWideChar(CP_ACP,0, (LPCSTR)模式,(int)patlen,wpatte rn,

(int)patlen);

wpattern [patlen - 1] = L''\''';

wprintf( L>模式扩展为\%s \\\n",wpattern);


MultiByteToWideChar(CP_ACP,0,(LPCSTR)文本,(int) textlen,wtext,

(int)textlen); //这是写在模式上的!为什么?

wtext [textlen - 1] = L''\''';

wprintf(L" Text扩展为\"%s \ " \ n",wtext);

wprintf(L"但模式现在是\"%s \" \ n",wpattern);


#endif


bText = SysAllocString((LPOLESTR)(wtext)),

bPattern = SysAllocString((LPOLESTR)(wpattern) );


wprintf(L" \ nbPattern是\"%s \" \ n",bPattern);

wprintf( L" bText是\"%s \" \ n",bText);


CoInitialize(NULL);

CoCreateInstance(& ; CLSID_RegExp,NULL,CLSCTX_INPROC_SERVER,& IID_IRegExp,

(void **)& regexp);

IRegExp_put_Global(regexp,VARIANT_TRUE);

IRegExp_put_IgnoreCase(regexp,VARIANT_TRUE);

IRegExp_put_Pattern(regexp,bPattern);

IRegExp_Execute(regexp,bText,(IDispatch **)& pMatches);

hr = IM atchCollection_get_Count(pMatches,& nMatchCount);

for(nMatch = 0; nMatch< nMatchCount; nMatch ++)

{

BSTR bValue; LPWSTR sValue;

LPTSTR tValue;

长索引,长度;

size_t stringlen,已转换;

IMatchCollection_get_Item( pMatches,nMatch,(IDispatch **)& pMatch);

IMatch_get_Value(pMatch,& bValue);

IMatch_get_FirstIndex(pMatch,& index);

IMatch_get_Length(pMatch,& length);

stringlen = 1 + SysStringLen(bValue);

sValue =(LPWSTR)malloc(stringlen);

swprintf_s(sValue,stringlen,L"%s",bValue);

sValue [stringlen - 1] = _T(''\ 0'');

#ifdef _UNICODE

tValue = sValue;

#else

tValue =(LPTSTR)malloc(stringlen);

WideCharToMultiByte(CP_ACP,0,sValue,(int)stringlen,tValue,

(int)stringlen,NULL,NULL);

# endif

pfnMECB(tValue,index,length,extradata);

#ifndef _UNICODE

// #ifndef _DEBUG

// free(tValue); //崩溃WinDbg - 为什么?

// #endif

#endif

IMatch_Release(pMatch);

//#ifndef _DEBUG

//免费(sValue);

// #endif

}


#ifndef _UNICODE

// #ifndef _DEBUG

//免费(wtext);

//免费(wpattern); < br $>
// #endif

#endif


SysFreeString(bText);

IMatchCollection_Release(pMatches) ;

IRegExp_Release(regexp);

CoUninitialize();

返回TRUE;

}

void CALLBACK MatchEval(LPCTSTR值,长开始,长度长,LPVOID

extradata)

{

_tprintf(_T(&) ;匹配找到\%s \在%d为%d \ n"),值,开始,长度);

}


int main()

{

MatchWords(_T(" \\\\ + +)),_ T(" A Words of words) ,MatchEval,NULL);

返回0;

}

Hello. I hope somebody can help me on this, because I''m running out of
options to turn to.

I have almost solved my regular expression function. Basically it works OK
if unicode is defined. It doesn''t work OK in ANSI mode however, as it has to
use MultiByteToWideChar and WideCharToMultiByte.
I''ve discovered that the regular expression part is working fine. As far as
I can tell the regular expression code is correctly parsing what it
receives, and outputting it in the correct format. However the calculation
of what it''s going to receive is going wrong.
Basically, two weird things are happening which are baffling me:
1) When I run it with /Zi and /D"_DEBUG" defined, but *not* being debugged
under WinDbg, then the pattern gets expanded OK. The text gets expanded OK.
But when the text expands, it overwrites ''pattern'' with the last word of it!
The output of the following is:
Pattern is expanded to "\w+"
Text is expanded to "A Collection of words"
But pattern is now "words"

bPattern is "words"
bText is "A Collection of words"

2) When it''s run when it *is* being debugged under WinDbg, then the strings
get copied fine! No overwriting, and the output is:
Pattern is expanded to "\w+"
Text is expanded to "A Collection of words"
But pattern is now "\w+"

bPattern is "\w+"
bText is "A Collection of words"
Match found "A" at 0 for 1
Match found "Collection" at 2 for 10
Match found "of" at 13 for 2
Match found "words" at 16 for 5

which is exactly correct - the only thing being, when I press F10 to step
over any call to ''free'' - it crashes! Can''t further debug the program or
anything.

I''m worried about two things. One, why ''free'' is crashing WinDbg, and not
the program on its own, but more worryingly, why is MultiByteToWideChar
overwriting my string''s memory, when this behaviour is not reproducible in
WinDbg. I previously had it using mbstowcs_s and it did exactly the same
thing. I even checked the memory locations in WinDbg and they were near each
other but plenty far enough apart (40 bytes apart IIRC).

Can anyone suggest a resolution? It would be really good if somebody could
reproduce this.

This is the current code:
typedef void (CALLBACK* MatchEvaluator)(LPCTSTR value, long start, long
length, LPVOID extradata);
BOOL MatchWords(LPCTSTR pattern, LPCTSTR text, MatchEvaluator pfnMECB,
LPVOID extradata)
{
IRegExp* regexp = NULL;
IMatchCollection* pMatches = NULL;
IMatch* pMatch = NULL;
long nMatchCount, nMatch;
HRESULT hr;
BSTR bText, bPattern;
LPWSTR wtext, wpattern;
#ifdef _UNICODE
wtext = (LPWSTR)text;
wpattern = (LPWSTR)pattern;
#else
size_t textlen = _tcslen(text) + 1, patlen = _tcslen(pattern) + 1,
convtext, convpat;
wtext = (LPWSTR)malloc(textlen),
wpattern = (LPWSTR)malloc(patlen);

MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pattern, (int)patlen, wpattern,
(int)patlen);
wpattern[patlen - 1] = L''\0'';
wprintf(L"Pattern is expanded to \"%s\"\n", wpattern);

MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, (int)textlen, wtext,
(int)textlen); //THIS WRITES OVER pattern! why?
wtext[textlen - 1] = L''\0'';
wprintf(L"Text is expanded to \"%s\"\n", wtext);
wprintf(L"But pattern is now \"%s\"\n", wpattern);

#endif

bText = SysAllocString((LPOLESTR)(wtext)),
bPattern = SysAllocString((LPOLESTR)(wpattern));

wprintf(L"\nbPattern is \"%s\"\n", bPattern);
wprintf(L"bText is \"%s\"\n", bText);

CoInitialize(NULL);
CoCreateInstance(&CLSID_RegExp, NULL, CLSCTX_INPROC_SERVER, &IID_IRegExp,
(void**)&regexp);
IRegExp_put_Global(regexp, VARIANT_TRUE);
IRegExp_put_IgnoreCase(regexp, VARIANT_TRUE);
IRegExp_put_Pattern(regexp, bPattern);
IRegExp_Execute(regexp, bText, (IDispatch**)&pMatches);
hr = IMatchCollection_get_Count(pMatches, &nMatchCount);
for(nMatch = 0; nMatch < nMatchCount; nMatch++)
{
BSTR bValue; LPWSTR sValue;
LPTSTR tValue;
long index, length;
size_t stringlen, converted;
IMatchCollection_get_Item(pMatches, nMatch, (IDispatch**)&pMatch);
IMatch_get_Value(pMatch, &bValue);
IMatch_get_FirstIndex(pMatch, &index);
IMatch_get_Length(pMatch, &length);
stringlen = 1 + SysStringLen(bValue);
sValue = (LPWSTR)malloc(stringlen);
swprintf_s(sValue, stringlen, L"%s", bValue);
sValue[stringlen - 1] = _T(''\0'');
#ifdef _UNICODE
tValue = sValue;
#else
tValue = (LPTSTR)malloc(stringlen);
WideCharToMultiByte(CP_ACP, 0, sValue, (int)stringlen, tValue,
(int)stringlen, NULL, NULL);
#endif
pfnMECB(tValue, index, length, extradata);
#ifndef _UNICODE
// #ifndef _DEBUG
// free(tValue); //crashes WinDbg - why?
// #endif
#endif
IMatch_Release(pMatch);
// #ifndef _DEBUG
// free(sValue);
// #endif
}

#ifndef _UNICODE
// #ifndef _DEBUG
// free(wtext);
// free(wpattern);
// #endif
#endif

SysFreeString(bText);
IMatchCollection_Release(pMatches);
IRegExp_Release(regexp);
CoUninitialize();
return TRUE;
}
void CALLBACK MatchEval(LPCTSTR value, long start, long length, LPVOID
extradata)
{
_tprintf(_T("Match found \"%s\" at %d for %d\n"), value, start, length);
}

int main()
{
MatchWords(_T("\\w+"), _T("A Collection of words"), MatchEval, NULL);
return 0;
}

推荐答案

抱歉 - 请忽略 - 即使对我来说这也是一个很棘手的问题...

我只有malloc''我实际需要的字节数的一半。 ..


仍然关于免费崩溃的windbg虽然... ...


" Bonj" < benjtaylor at hotpop d0t com>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP09.phx.gbl ...
SORRY - Please ignore - this is SILLY question even for me...
i''ve only malloc''ed half the amount of bytes I actually require...

still weirded out about the free crashing windbg though...

"Bonj" <benjtaylor at hotpop d0t com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
你好。我希望有人能帮助我,因为我已经没有
选项可以转向。

我几乎解决了我的正则表达式功能。基本上它工作正常
如果定义了unicode。然而,它在ANSI模式下无法正常工作,因为它使用MultiByteToWideChar和WideCharToMultiByte。
我发现正则表达式部分工作正常。到目前为止,我可以告诉正则表达式代码正确解析它收到的内容,并以正确的格式输出它。然而,计算它将会收到的是错误的。
基本上,两件奇怪的事情正在发生,令我感到困惑:
1)当我用/ Zi运行它时/ d" _DEBUG"在WinDbg下定义但不*正在调试
,然后模式扩展OK。文字扩大了
好的。但是当文本扩展时,它会用最后一个单词
覆盖''pattern''!以下输出为:
模式扩展为\w +
文本扩展为单词集合
但模式现在是单词。

bPattern是单词
bText是单词集合

2)当它被*调试时运行在WinDbg下,然后
字符串被复制好了!没有覆盖,输出是:
模式扩展为\w +
文本扩展为A Collection of words
但模式现在是\\ \\ w +"

bPattern是\w +
bText是A Collection of words
匹配找到A和A。在0为1
匹配找到收集在2为10
匹配找到的在13为2
匹配找到单词这是完全正确的 - 唯一的事情就是,当我按F10步骤
而不是任何免费的电话时 - 它会崩溃!无法进一步调试程序或
任何事情。

我担心两件事。一,为什么''free''会让WinDbg崩溃,而不是单独打开程序,但更令人担忧的是,为什么MultiByteToWideChar会覆盖我的字符串的内存,当这种行为无法重现时
WinDbg。我以前使用过mbstowcs_s,它完全一样。我甚至检查了WinDbg中的内存位置,它们彼此相邻,但相距足够远(相隔40个字节IIRC)。

任何人都可以提出解决方案吗?如果有人能够重现这一点真的很好。

这是当前代码:

typedef void(CALLBACK * MatchEvaluator)(LPCTSTR值,长期启动,长
长度,LPVOID extradata);

BOOL MatchWords(LPCTSTR模式,LPCTSTR文本,MatchEvaluator pfnMECB,
LPVOID extradata)
{
IRegExp * regexp = NULL;
IMatchCollection * pMatches = NULL;
IMatch * pMatch = NULL;
long nMatchCount,nMatch;
HRESULT hr;
BSTR bText,bPattern;
LPWSTR wtext,wpattern;
#ifdef _UNICODE
wtext =(LPWSTR)文本;
wpattern =(LPWSTR)模式;
#else
size_t textlen = _tcslen(text)+ 1,patlen = _tcslen(pattern)+ 1,
convtext,convpat;
wtext =(LPWSTR)malloc(textlen),
wpattern =(LPWSTR)malloc(patlen MultiByteToWideChar(CP_ACP,0,(LPCSTR)模式,(int)patlen,wpattern,
(int)patlen);
wpattern [patlen - 1] = L ''\ 0'';
wprintf(L" Pattern扩展为\%s \\\n",wpattern);

MultiByteToWideChar(CP_ACP,0,(LPCSTR)文本,(int)textlen,wtext,(int)textlen); //这是写在模式上的!为什么?
wtext [textlen - 1] = L''\ 0'';
wprintf(L" Text扩展为\"%s \" \ n",wtext );
wprintf(L"但模式现在是\%s \\\n",wpattern);

#endif

bText = SysAllocString((LPOLESTR)(wtext)),
bPattern = SysAllocString((LPOLESTR)(wpattern));

wprintf(L" \ nbPattern is \"%s \" \ n",bPattern);
wprintf(L" bText是\"%s \" \ n",bText);

CoInitialize( NULL);
CoCreateInstance(& CLSID_RegExp,NULL,CLSCTX_INPROC_SERVER,& IID_IRegExp,
(void **)& regexp);
IRegExp_put_Global(regexp,VARIANT_TRUE);
IRegExp_put_IgnoreCase(regexp,VARIANT_TRUE);
IRegExp_put_Pattern(regexp,bPattern);
IRegExp_Execute(regexp,bText,(IDispatch **)& pMatches);
hr = IMatchCollection_get_Count(pMatches,& nMatchCount);
for(nMatch = 0; nMat ch< nMatchCount; nMatch ++)
{
BSTR bValue; LPWSTR sValue;
LPTSTR tValue;
长索引,长度;
size_t stringlen,转换;
IMatchCollection_get_Item(pMatches,nMatch,(IDispatch **)& pMatch);
IMatch_get_Value(pMatch,& bValue);
IMatch_get_FirstIndex(pMatch,& index);
IMatch_get_Length(pMatch,& length);
stringlen = 1 + SysStringLen(bValue);
sValue =(LPWSTR)malloc(stringlen);
swprintf_s(sValue,stringlen,L"%s",bValue);
sValue [stringlen - 1] = _T(''\ 0'');
#ifdef _UNICODE
tValue = sValue;
#else
tValue =(LPTSTR)malloc(stringlen);
WideCharToMultiByte(CP_ACP,0, sValue,(int)stringlen,tValue,
(int)stringlen,NULL,NULL);
#endif
pfnMECB(tValue,index,length,extradata);
#ifndef _UNICODE
// #ifndef _DEBUG
//免费(tValue); //崩溃WinDbg - 为什么?
// #endif
#endif
IMatch_Release(pMatch);
// #ifndef _DEBUG
// free(sValue);
// #endif
}
#ifndef _UNICODE
// #ifndef _DEBUG
//免费(wtext);
// free(wpattern);
// #endif
#endif

SysFreeString(bText);
IMatchCollection_Release(pMatches);
IRegExp_Release(regexp);
CoUninitialize();
返回TRUE;
}

void CALLBACK MatchEval(LPCTSTR值,长启动,长度长,LPVOID
extradata)
{
_tprintf(_T(匹配找到\%s \在%d处为%d \ n"),值,开始,长度);
}

int main()
{MatchWords(_T(" \\\\ +),_T(" A Collection of words)),MatchEval,NULL );
返回0;
}
Hello. I hope somebody can help me on this, because I''m running out of
options to turn to.

I have almost solved my regular expression function. Basically it works OK
if unicode is defined. It doesn''t work OK in ANSI mode however, as it has
to use MultiByteToWideChar and WideCharToMultiByte.
I''ve discovered that the regular expression part is working fine. As far
as I can tell the regular expression code is correctly parsing what it
receives, and outputting it in the correct format. However the calculation
of what it''s going to receive is going wrong.
Basically, two weird things are happening which are baffling me:
1) When I run it with /Zi and /D"_DEBUG" defined, but *not* being debugged
under WinDbg, then the pattern gets expanded OK. The text gets expanded
OK. But when the text expands, it overwrites ''pattern'' with the last word
of it! The output of the following is:
Pattern is expanded to "\w+"
Text is expanded to "A Collection of words"
But pattern is now "words"

bPattern is "words"
bText is "A Collection of words"

2) When it''s run when it *is* being debugged under WinDbg, then the
strings get copied fine! No overwriting, and the output is:
Pattern is expanded to "\w+"
Text is expanded to "A Collection of words"
But pattern is now "\w+"

bPattern is "\w+"
bText is "A Collection of words"
Match found "A" at 0 for 1
Match found "Collection" at 2 for 10
Match found "of" at 13 for 2
Match found "words" at 16 for 5

which is exactly correct - the only thing being, when I press F10 to step
over any call to ''free'' - it crashes! Can''t further debug the program or
anything.

I''m worried about two things. One, why ''free'' is crashing WinDbg, and not
the program on its own, but more worryingly, why is MultiByteToWideChar
overwriting my string''s memory, when this behaviour is not reproducible in
WinDbg. I previously had it using mbstowcs_s and it did exactly the same
thing. I even checked the memory locations in WinDbg and they were near
each other but plenty far enough apart (40 bytes apart IIRC).

Can anyone suggest a resolution? It would be really good if somebody could
reproduce this.

This is the current code:
typedef void (CALLBACK* MatchEvaluator)(LPCTSTR value, long start, long
length, LPVOID extradata);
BOOL MatchWords(LPCTSTR pattern, LPCTSTR text, MatchEvaluator pfnMECB,
LPVOID extradata)
{
IRegExp* regexp = NULL;
IMatchCollection* pMatches = NULL;
IMatch* pMatch = NULL;
long nMatchCount, nMatch;
HRESULT hr;
BSTR bText, bPattern;
LPWSTR wtext, wpattern;
#ifdef _UNICODE
wtext = (LPWSTR)text;
wpattern = (LPWSTR)pattern;
#else
size_t textlen = _tcslen(text) + 1, patlen = _tcslen(pattern) + 1,
convtext, convpat;
wtext = (LPWSTR)malloc(textlen),
wpattern = (LPWSTR)malloc(patlen);

MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pattern, (int)patlen, wpattern,
(int)patlen);
wpattern[patlen - 1] = L''\0'';
wprintf(L"Pattern is expanded to \"%s\"\n", wpattern);

MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, (int)textlen, wtext,
(int)textlen); //THIS WRITES OVER pattern! why?
wtext[textlen - 1] = L''\0'';
wprintf(L"Text is expanded to \"%s\"\n", wtext);
wprintf(L"But pattern is now \"%s\"\n", wpattern);

#endif

bText = SysAllocString((LPOLESTR)(wtext)),
bPattern = SysAllocString((LPOLESTR)(wpattern));

wprintf(L"\nbPattern is \"%s\"\n", bPattern);
wprintf(L"bText is \"%s\"\n", bText);

CoInitialize(NULL);
CoCreateInstance(&CLSID_RegExp, NULL, CLSCTX_INPROC_SERVER, &IID_IRegExp,
(void**)&regexp);
IRegExp_put_Global(regexp, VARIANT_TRUE);
IRegExp_put_IgnoreCase(regexp, VARIANT_TRUE);
IRegExp_put_Pattern(regexp, bPattern);
IRegExp_Execute(regexp, bText, (IDispatch**)&pMatches);
hr = IMatchCollection_get_Count(pMatches, &nMatchCount);
for(nMatch = 0; nMatch < nMatchCount; nMatch++)
{
BSTR bValue; LPWSTR sValue;
LPTSTR tValue;
long index, length;
size_t stringlen, converted;
IMatchCollection_get_Item(pMatches, nMatch, (IDispatch**)&pMatch);
IMatch_get_Value(pMatch, &bValue);
IMatch_get_FirstIndex(pMatch, &index);
IMatch_get_Length(pMatch, &length);
stringlen = 1 + SysStringLen(bValue);
sValue = (LPWSTR)malloc(stringlen);
swprintf_s(sValue, stringlen, L"%s", bValue);
sValue[stringlen - 1] = _T(''\0'');
#ifdef _UNICODE
tValue = sValue;
#else
tValue = (LPTSTR)malloc(stringlen);
WideCharToMultiByte(CP_ACP, 0, sValue, (int)stringlen, tValue,
(int)stringlen, NULL, NULL);
#endif
pfnMECB(tValue, index, length, extradata);
#ifndef _UNICODE
// #ifndef _DEBUG
// free(tValue); //crashes WinDbg - why?
// #endif
#endif
IMatch_Release(pMatch);
// #ifndef _DEBUG
// free(sValue);
// #endif
}

#ifndef _UNICODE
// #ifndef _DEBUG
// free(wtext);
// free(wpattern);
// #endif
#endif

SysFreeString(bText);
IMatchCollection_Release(pMatches);
IRegExp_Release(regexp);
CoUninitialize();
return TRUE;
}
void CALLBACK MatchEval(LPCTSTR value, long start, long length, LPVOID
extradata)
{
_tprintf(_T("Match found \"%s\" at %d for %d\n"), value, start, length);
}

int main()
{
MatchWords(_T("\\w+"), _T("A Collection of words"), MatchEval, NULL);
return 0;
}



尽管有关于免费崩溃的windbg仍然很奇怪...



SA我发布了!现在工作正常...试图一次释放两个。


虽然奇怪的是它*如何工作*在WinDbg中很好.....即使我

不要malloc(stringlen * sizeof(wchar_t))...


" Bonj" < benjtaylor at hotpop d0t com>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP09.phx.gbl ...
still weirded out about the free crashing windbg though...

SAME ISSUE! works fine now... was trying to free two at once.

Although what IS weird is how it *works* fine in WinDbg.....even when I
don''t malloc (stringlen * sizeof(wchar_t))...

"Bonj" <benjtaylor at hotpop d0t com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
你好。我希望有人能帮助我,因为我已经没有
选项可以转向。

我几乎解决了我的正则表达式功能。基本上它的工作原理
如果定义了unicode就行了。然而,它在ANSI模式下无法正常工作,因为它必须使用MultiByteToWideChar和WideCharToMultiByte。
我发现正则表达式部分工作正常。到目前为止,我可以告诉正则表达式代码正确解析它收到的内容,并以正确的格式输出它。然而,计算它将会收到的东西是错误的。
基本上,有两件奇怪的事情正在发生,令我感到困惑:
1)当我用/ Zi运行它时/ d" _DEBUG"定义,但*不在WinDbg下调试,然后模式扩展好了。文本扩展好了。但是当文本扩展时,它会用
的最后一个字覆盖''pattern''!以下输出为:
模式扩展为\w +
文本扩展为单词集合
但模式现在是单词。

bPattern是单词
bText是单词集合

2)当它被*调试时运行在WinDbg下,然后
字符串被复制好了!没有覆盖,输出是:
模式扩展为\w +
文本扩展为A Collection of words
但模式现在是\\ \\ w +"

bPattern是\w +
bText是A Collection of words
匹配找到A和A。在0为1
匹配找到收集在2为10
匹配找到的在13为2
匹配找到单词这是完全正确的 - 唯一的事情就是,当我按F10步骤
而不是任何免费的电话时 - 它会崩溃!无法进一步调试程序或
任何事情。

我担心两件事。一,为什么''free''会让WinDbg崩溃,而不是单独使用程序,但更令人担忧的是,为什么MultiByteToWideChar会覆盖我的字符串的内存,当这种行为不可重现时<在WinDbg中。我以前使用过mbstowcs_s,它确实完全相同。我甚至检查了WinDbg中的内存位置,它们彼此相近但相距足够远(相隔40个字节IIRC)。

任何人都可以提出解决方案吗?如果某人能够重现这一点真的很好。

这是当前的代码:

typedef void(CALLBACK * MatchEvaluator)(LPCTSTR值,长期启动,长
长度,LPVOID extradata);

BOOL MatchWords(LPCTSTR模式,LPCTSTR文本,MatchEvaluator pfnMECB,
LPVOID extradata)
{
IRegExp * regexp = NULL;
IMatchCollection * pMatches = NULL;
IMatch * pMatch = NULL;
long nMatchCount,nMatch;
HRESULT hr;
BSTR bText,bPattern;
LPWSTR wtext,wpattern;
#ifdef _UNICODE
wtext =(LPWSTR)文本;
wpattern =(LPWSTR)模式;
#else
size_t textlen = _tcslen(text)+ 1,patlen = _tcslen(pattern)+ 1,
convtext,convpat;
wtext =(LPWSTR)malloc(textlen),
wpattern =(LPWSTR)malloc(patlen MultiByteToWideChar(CP_ACP,0,(LPCSTR)模式,(int)patlen,wpattern,
(int)patlen);
wpattern [patlen - 1] = L ''\ 0'';
wprintf(L" Pattern扩展为\%s \\\n",wpattern);

MultiByteToWideChar(CP_ACP,0,(LPCSTR)文本,(int)textlen,wtext,(int)textlen); //这是写在模式上的!为什么?
wtext [textlen - 1] = L''\ 0'';
wprintf(L" Text扩展为\"%s \" \ n",wtext );
wprintf(L"但模式现在是\%s \\\n",wpattern);

#endif

bText = SysAllocString((LPOLESTR)(wtext)),
bPattern = SysAllocString((LPOLESTR)(wpattern));

wprintf(L" \ nbPattern is \"%s \" \ n",bPattern);
wprintf(L" bText是\"%s \" \ n",bText);

CoInitialize( NULL);
CoCreateInstance(& CLSID_RegExp,NULL,CLSCTX_INPROC_SERVER,
& IID_IRegExp,(void **)& regexp);
IRegExp_put_Global(regexp,VARIANT_TRUE);
IRegExp_put_IgnoreCase(regexp,VARIANT_TRUE);
IRegExp_put_Pattern(regexp,bPattern);
IRegExp_Execute(regexp,bText,(IDispatch **)& pMatches);
hr = IMatchCollection_get_Count(pMatches,& nMatchCount);
for(nMatch = 0; nMat ch< nMatchCount; nMatch ++)
{
BSTR bValue; LPWSTR sValue;
LPTSTR tValue;
长索引,长度;
size_t stringlen,转换;
IMatchCollection_get_Item(pMatches,nMatch,(IDispatch **)& pMatch);
IMatch_get_Value(pMatch,& bValue);
IMatch_get_FirstIndex(pMatch,& index);
IMatch_get_Length(pMatch,& length);
stringlen = 1 + SysStringLen(bValue);
sValue =(LPWSTR)malloc(stringlen);
swprintf_s(sValue,stringlen,L"%s",bValue);
sValue [stringlen - 1] = _T(''\ 0'');
#ifdef _UNICODE
tValue = sValue;
#else
tValue =(LPTSTR)malloc(stringlen);
WideCharToMultiByte(CP_ACP,0, sValue,(int)stringlen,tValue,
(int)stringlen,NULL,NULL);
#endif
pfnMECB(tValue,index,length,extradata);
#ifndef _UNICODE
// #ifndef _DEBUG
//免费(tValue); //崩溃WinDbg - 为什么?
// #endif
#endif
IMatch_Release(pMatch);
// #ifndef _DEBUG
// free(sValue);
// #endif
}
#ifndef _UNICODE
// #ifndef _DEBUG
//免费(wtext);
// free(wpattern);
// #endif
#endif

SysFreeString(bText);
IMatchCollection_Release(pMatches);
IRegExp_Release(regexp);
CoUninitialize();
返回TRUE;
}

void CALLBACK MatchEval(LPCTSTR值,长启动,长度长,LPVOID
extradata)
{
_tprintf(_T(匹配找到\%s \在%d处为%d \ n"),值,开始,长度);
}

int main()
{MatchWords(_T(" \\\\ +),_T(" A Collection of words)),MatchEval,NULL );
返回0;
}
Hello. I hope somebody can help me on this, because I''m running out of
options to turn to.

I have almost solved my regular expression function. Basically it works
OK if unicode is defined. It doesn''t work OK in ANSI mode however, as it
has to use MultiByteToWideChar and WideCharToMultiByte.
I''ve discovered that the regular expression part is working fine. As far
as I can tell the regular expression code is correctly parsing what it
receives, and outputting it in the correct format. However the
calculation of what it''s going to receive is going wrong.
Basically, two weird things are happening which are baffling me:
1) When I run it with /Zi and /D"_DEBUG" defined, but *not* being
debugged under WinDbg, then the pattern gets expanded OK. The text gets
expanded OK. But when the text expands, it overwrites ''pattern'' with the
last word of it! The output of the following is:
Pattern is expanded to "\w+"
Text is expanded to "A Collection of words"
But pattern is now "words"

bPattern is "words"
bText is "A Collection of words"

2) When it''s run when it *is* being debugged under WinDbg, then the
strings get copied fine! No overwriting, and the output is:
Pattern is expanded to "\w+"
Text is expanded to "A Collection of words"
But pattern is now "\w+"

bPattern is "\w+"
bText is "A Collection of words"
Match found "A" at 0 for 1
Match found "Collection" at 2 for 10
Match found "of" at 13 for 2
Match found "words" at 16 for 5

which is exactly correct - the only thing being, when I press F10 to step
over any call to ''free'' - it crashes! Can''t further debug the program or
anything.

I''m worried about two things. One, why ''free'' is crashing WinDbg, and not
the program on its own, but more worryingly, why is MultiByteToWideChar
overwriting my string''s memory, when this behaviour is not reproducible
in WinDbg. I previously had it using mbstowcs_s and it did exactly the
same thing. I even checked the memory locations in WinDbg and they were
near each other but plenty far enough apart (40 bytes apart IIRC).

Can anyone suggest a resolution? It would be really good if somebody
could reproduce this.

This is the current code:
typedef void (CALLBACK* MatchEvaluator)(LPCTSTR value, long start, long
length, LPVOID extradata);
BOOL MatchWords(LPCTSTR pattern, LPCTSTR text, MatchEvaluator pfnMECB,
LPVOID extradata)
{
IRegExp* regexp = NULL;
IMatchCollection* pMatches = NULL;
IMatch* pMatch = NULL;
long nMatchCount, nMatch;
HRESULT hr;
BSTR bText, bPattern;
LPWSTR wtext, wpattern;
#ifdef _UNICODE
wtext = (LPWSTR)text;
wpattern = (LPWSTR)pattern;
#else
size_t textlen = _tcslen(text) + 1, patlen = _tcslen(pattern) + 1,
convtext, convpat;
wtext = (LPWSTR)malloc(textlen),
wpattern = (LPWSTR)malloc(patlen);

MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pattern, (int)patlen, wpattern,
(int)patlen);
wpattern[patlen - 1] = L''\0'';
wprintf(L"Pattern is expanded to \"%s\"\n", wpattern);

MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, (int)textlen, wtext,
(int)textlen); //THIS WRITES OVER pattern! why?
wtext[textlen - 1] = L''\0'';
wprintf(L"Text is expanded to \"%s\"\n", wtext);
wprintf(L"But pattern is now \"%s\"\n", wpattern);

#endif

bText = SysAllocString((LPOLESTR)(wtext)),
bPattern = SysAllocString((LPOLESTR)(wpattern));

wprintf(L"\nbPattern is \"%s\"\n", bPattern);
wprintf(L"bText is \"%s\"\n", bText);

CoInitialize(NULL);
CoCreateInstance(&CLSID_RegExp, NULL, CLSCTX_INPROC_SERVER,
&IID_IRegExp, (void**)&regexp);
IRegExp_put_Global(regexp, VARIANT_TRUE);
IRegExp_put_IgnoreCase(regexp, VARIANT_TRUE);
IRegExp_put_Pattern(regexp, bPattern);
IRegExp_Execute(regexp, bText, (IDispatch**)&pMatches);
hr = IMatchCollection_get_Count(pMatches, &nMatchCount);
for(nMatch = 0; nMatch < nMatchCount; nMatch++)
{
BSTR bValue; LPWSTR sValue;
LPTSTR tValue;
long index, length;
size_t stringlen, converted;
IMatchCollection_get_Item(pMatches, nMatch, (IDispatch**)&pMatch);
IMatch_get_Value(pMatch, &bValue);
IMatch_get_FirstIndex(pMatch, &index);
IMatch_get_Length(pMatch, &length);
stringlen = 1 + SysStringLen(bValue);
sValue = (LPWSTR)malloc(stringlen);
swprintf_s(sValue, stringlen, L"%s", bValue);
sValue[stringlen - 1] = _T(''\0'');
#ifdef _UNICODE
tValue = sValue;
#else
tValue = (LPTSTR)malloc(stringlen);
WideCharToMultiByte(CP_ACP, 0, sValue, (int)stringlen, tValue,
(int)stringlen, NULL, NULL);
#endif
pfnMECB(tValue, index, length, extradata);
#ifndef _UNICODE
// #ifndef _DEBUG
// free(tValue); //crashes WinDbg - why?
// #endif
#endif
IMatch_Release(pMatch);
// #ifndef _DEBUG
// free(sValue);
// #endif
}

#ifndef _UNICODE
// #ifndef _DEBUG
// free(wtext);
// free(wpattern);
// #endif
#endif

SysFreeString(bText);
IMatchCollection_Release(pMatches);
IRegExp_Release(regexp);
CoUninitialize();
return TRUE;
}
void CALLBACK MatchEval(LPCTSTR value, long start, long length, LPVOID
extradata)
{
_tprintf(_T("Match found \"%s\" at %d for %d\n"), value, start, length);
}

int main()
{
MatchWords(_T("\\w+"), _T("A Collection of words"), MatchEval, NULL);
return 0;
}






" ; BONJ" < benjtaylor at hotpop d0t com>在消息中写道

新闻:%2 **************** @ tk2msftngp13.phx.gbl ...

"Bonj" <benjtaylor at hotpop d0t com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
虽然是什么奇怪的是它在WinDbg中如何工作*很好.....即使我没有malloc(stringlen * sizeof(wchar_t))...
Although what IS weird is how it *works* fine in WinDbg.....even when I
don''t malloc (stringlen * sizeof(wchar_t))...



这个空间不够 - 没有类型的空间 - 而且你不应该明确地命名类型(wchar_t)
。你可以说sizeof(TCHAR),或者(甚至更好)
从指向对象中提取大小:


TCHAR * p = malloc(( stringlen + 1)* sizeof(* p));


Steve



That''s not enough space - no room for the type - and you should not be
naming the type (wchar_t) explicitly. You could say sizeof(TCHAR), or (even
better) extract the size from the pointed-to object:

TCHAR *p = malloc( (stringlen + 1) * sizeof(*p) );

Steve


这篇关于奇怪的字符串相互写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆