Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / November 2004

Tip: Looking for answers? Try searching our database.

friend CStringT operator+(const CStringT& str1, const CStringT& st

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
TonyM - 02 Nov 2004 17:20 GMT
Q: Is there a good way to overcome this apparent bug without modifying the
mfc code?
___________________________________

Info:  Although it is NOT a good idea, I seemed to have perhaps located a
bug in this function with the GetLength call whose results are sent to the
sub-call to Concatenate.  So I added the following and it asserts on
particular strings that are used.  (I am not sure if this makes any
difference, but I am using class derived from CString that ends up calling
this code...  However, that class never does anything remotely like touching
any private CString data without going through  the very normal exposed
methods.)

Code as modified in CStringT.h file (line# 2013):
friend CStringT operator+( const CStringT& str1, const CStringT& str2 )
{
 CStringT strResult( str1.GetManager() );
   
 int str1_GetLength = str1.GetLength();
 int str1_lstrlen   = lstrlen(str1);
 int str2_GetLength = str2.GetLength();
 int str2_lstrlen   = lstrlen(str2);
 ASSERT ( str1_GetLength == str1_lstrlen );
 ASSERT ( str2_GetLength == str2_lstrlen );

 Concatenate( strResult, str1, str1.GetLength(), str2, str2.GetLength() );

 return( strResult );
}
______________________________
Values are as follows as the debugger is about to eexecute the first ASSERT
statement:

str1 {0x0127a658 "<Message>"}    const
ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > > &

str2 {0x0127a4a8 "chris logged off."}const
ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > > &

str1_GetLength  9  int

str1_lstrlen  9  int

str2_GetLength  18  int

str2_lstrlen  17  int

Ignore the assert and the result is:
strResult  {0x0127a5a8 "<Message>chris logged off."}
ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > >

________________________________

Notice that in this instance, the second assert will fail.  If I ignore it,
then then next time I enter this function on this string to concatenate more,
the str1 length is wrong and the concantenation writes the added string just
after the null so that the result is incorrect.

___________________________________

str1_GetLength  27  int

str1_lstrlen  26  int

str2_GetLength  2  int

str2_lstrlen  2  int

str1  {0x0127a5a8 "<Message>chris logged off."}  const
ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > > &

str2  {0x0127a610 "</"}  const
ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > > &

strResult {0x012525b8 "<Message>chris logged off."}
ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > >
_____________________________

Tony_Morris at Teledyne dot com
doug mansell - 03 Nov 2004 07:57 GMT
The bug is probably in the construction of str2.  Can you show us that code?

> Q: Is there a good way to overcome this apparent bug without modifying the
> mfc code?
[quoted text clipped - 75 lines]
>
> Tony_Morris at Teledyne dot com
TonyM - 03 Nov 2004 14:09 GMT
Thanks Doug!

I am not sure why, but my single post to the "VC.ATL" group has been posted
here too...???

Anyhow, your question is a copy of the answer from that group:

> The bug is probably in the construction of str2.  Can you show us that code?

Right on the mark!  I had a transfer function that was doing a += into a
CString that ended up placing one extra NULL in the string.  I would have
never guessed that CString would support additional NULLs in the data but
sure enough, the following has the output of incrementing the length even
though the string passed to lstrlen() returns 0 every time!
_____________________________________

CString str("");
TCHAR ch = '\0';

MessageBox(::GetActiveWindow(), "Length: " + CTekString(str.GetLength()) ,
"str.GetLength()", MB_OK);
MessageBox(::GetActiveWindow(), "Length: " + CTekString(lstrlen(str)) ,
"str.GetLength()", MB_OK);

str += ch;

MessageBox(::GetActiveWindow(), "Length: " + CTekString(str.GetLength()) ,
"str.GetLength()", MB_OK);
MessageBox(::GetActiveWindow(), "Length: " + CTekString(lstrlen(str)) ,
"str.GetLength()", MB_OK);

str += ch;

MessageBox(::GetActiveWindow(), "Length: " + CTekString(str.GetLength()) ,
"str.GetLength()", MB_OK);
MessageBox(::GetActiveWindow(), "Length: " + CTekString(lstrlen(str)) ,
"str.GetLength()", MB_OK);
________________________________________
Output:

0
0
1
0
2
0
___________________________________________________

Thank you profusely for the insight!!!

TonyM

"Igor Tandetnik" wrote:

> "TonyM" <Tony_Morris @at Teledyne .dot com> wrote in message
> news:E0C38338-0056-4D63-8181-8C46CCB6346B@microsoft.com
[quoted text clipped - 47 lines]
> complete program, where you set up the arguments and then invoke the
> operator.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.