[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 457
  • Last Modified:

char to CString assignement - causes assertion failure

Hi experts,

look at the code snippet below. Any ideas ?

class CParser  
{
private:
      CString m_varBuffer;
      bool m_Comment;
...

}

int CParser::parseVariable(CRichEditCtrl *pCtrl,long iStart)
{
    int open = 0;
    int close = 0;
    int pos = 0;
      
         // Select where string detected
      pCtrl->SetSel(iStart,-1);
      // Create dynamic buf
      char* lpszBuf = new char[pCtrl->GetTextLength() + 1];
      
      if (lpszBuf == NULL)
            return -1;

      strcpy(lpszBuf,pCtrl->GetSelText());
          
      // Iterate each char sign
                while(lpszBuf[pos])
      {
                       if(lpszBuf[pos] == '{')
                {
            open++;
                }
                  
                if(lpszBuf[pos] == '}')
                {
            close++;
                }
                m_varBuffer += lpszBuf[pos];      <---      In this line I get assertion failure
                      pos++;      
                // ready to leave ?
                if (open > 0 && (open == close))
            break;
      }
 
      delete lpszBuf;

      return 0;
}


Thanks in advance
Martin
0
martinmoessner
Asked:
martinmoessner
  • 5
  • 4
  • 4
  • +3
3 Solutions
 
jkrCommented:
What is the exact message of the assertion, the line number in particular?
0
 
rcarlanCommented:
There are various performance problems in your code, but I can't really see the reason for a failed assert.

Does it assert on the very first iteration, or after a few loops? How many (i.e. what is the value of pos when the assertion occurs)?

What file and line does it assert in? What version of VS are you using?
0
 
grg99Commented:
What's m_varbuffer?  

If it's a C++ String, is it ever initialized?



0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
_corey_Commented:
m_varBuffer is a CString.

I couldn't find any real assertions in the CString code being called, so there must be one really deep in the allocation or character check.

I think the issue is the value of lpszBuf[pos] at the time of failure.

corey
0
 
grg99Commented:
Is m_varBuffer ever created and initialized?


How big is the text coming in?  

 What's the value of pos when the assertion fails?
0
 
grg99Commented:
Are you sure getTextLength() gets you the length of what GetSelText() returns?

How about you use strncpy() just to be safe

0
 
rcarlanCommented:
Under normal circumstances, nothing in the code should cause an assertion failure.
Please provide the following information at the time the assertion fails:
- value of pos
- value of lpszBuf[pos]
- line number and filename where the assertion fails (not the call in your code, but the actual ASSERT line in the MFC file)

Radu
0
 
martinmoessnerAuthor Commented:
Debug Asserion Failed!
Program c:\develop\jat\debug\jat.exe
File: strcore.cpp
Line 457

const CString& CString::operator+=(LPCTSTR lpsz)
{
Line 457 --> ASSERT(lpsz == NULL || AfxIsValidString(lpsz));
        ConcatInPlace(SafeStrlen(lpsz), lpsz);
        return *this;
}
pos value = 0
lpszBuf[pos] = 86 'V'

Incoming text ...
Textlength is the incoming text length

VARIABLE var_dig_in_status3            /*for CMD 48*/
{
      HELP str_dig_in_status_help;
      CLASS DISCRETE & SERVICE & DYNAMIC;
      LABEL str_dig_in_status3;
      HANDLING READ;       
      TYPE ENUMERATED  
       {
             {0,      str_digin_st0},                  /*open*/
             {1,       str_digin_st1}                  /*shorted*/
       }            
}

Martin
0
 
grg99Commented:
Oh, it looks like it's calling the wrong concat operator.  You want it to append the character, and it isntead is trying to pass it as a LPCTSTR, whatever that is.

try extracting the char, which is a good idea anyway for all the tests before it: char ch;    ch =  lpszBuf[pos];

then the if statements get clearer and simpler:   if( ch == '{' ) open++; else if( ch == '}') close++; m_varbuffer += ch;

give that a shot;

0
 
_corey_Commented:
Yea, it looks the old CString doesn't always define a concat with 'char' but instead only 'TCHAR' by default.  I don't think that conversion is happening properly.  You'll probably need to cast it to a TCHAR.

corey
0
 
jkrCommented:
Try

m_varBuffer += *(lpszBuf + pos);

instead
0
 
rcarlanCommented:
Is your project configured for _UNICODE or for _MBCS/_SBCS?

The code snippet shown looks like _MBCS/_SBCS. But in this case CString::operator+=(TCHAR) would resolve to CString::operator+=(char) and should have been called successfully. However, your assert shows that CString::operator+=(LPCTSTR) is called instead. This should obviously not happen, unless m_varBuffer is not really a CString but a class derived from CString that defines its own operator+= that forwards to the CString implementation.

What version of VC++ are you using? Looks like V6 based on your strcore.cpp.
Are you linking dynamically or statically to MFC?

Have you tried a re-build all?
How about stepping into m_varBuffer += lpszBuf[pos]. Does it go straight to CString::operator+=(LPCTSTR)?

Radu

0
 
_corey_Commented:
Yea, the confusion that the compiler is having seems really odd.  His code shows the definition directly as a CString.  

However, if he had _UNICODE  defined, then the extra (in VC6) CString::operator+=(char) overload would be defined specifically.

I'd like to see if jkr's method or a cast makes any difference.

corey
0
 
rcarlanCommented:
>>I'd like to see if jkr's method or a cast makes any difference.
Not more than a re-build all would, I imagine.

Radu
0
 
rcarlanCommented:
Also, a cast may make a difference, but for the wrong reasons.
It may also lead to crashes or unexpected behaviour. Especially in _UNICODE, where you'd be casting char (one byte) to wchar_t (two bytes) without actually doing any kind of character conversion.

Radu
0
 
_corey_Commented:
None of this should be happening anyway, so I guess we can only wait to see if the results are as predicted.

corey
0
 
waysideCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:

Split between grg99, jkr, rcarlan

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

wayside
EE Cleanup Volunteer
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 4
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now