Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Problems with CString class of MFC

Posted on 2003-10-26
7
695 Views
Last Modified: 2013-12-14
Hi all

My application is an MFC dialog based server which calls ATL based dll's on client requests.

I have noticed that my application crashes when many user requests come and many times it gives unknown errors. My friend had said that there is a problem with MFC's CString class having memory leaks.

When i changed the ATL Based dll's to VB dll's it started working fine.

Can anyone please tell me if there is any problem in MFC's CString class or any other class.

Thanks.
0
Comment
Question by:sudeshsh
  • 2
7 Comments
 
LVL 23

Accepted Solution

by:
Roshan Davis earned 168 total points
ID: 9625525
Some problems Listed

1. An access violation may result after using CString::operator+= where the string on the right hand side of the operator is an empty string.

2. GP Fault when Use CString Objects Inside TRACE<x> Macros
eg:
    CString t1("output1");
   
    // Incorrect use of the %s format specifier with a CString object:
    TRACE1("TRACE1 %s\n", t1);

    // Correct is
    TRACE1("TRACE1 %s\n", (LPCTSTR)t1);


3. Chances of GP in *CString::Format* function- while using * (asterik) etc

4. If the CString equality operators (==, !=) are used in an attempt to test whether a CString is NULL, a general protection (GP) fault may occur. The following code fragment demonstrates this:


   CString x = "This is a sample CString";
   if (x != NULL)  // GP Fault for (x == NULL) condition, as well
      AfxMessageBox("The CString is not EMPTY");
   else
      AfxMessageBox("The CString is EMPTY");
Because x is a CString object, not a pointer, it is inappropriate to compare x to a null pointer.

Rosh :)
0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 166 total points
ID: 9625535
>> When i changed the ATL Based dll's to VB dll's it started working fine.

This means, the problem is in dll's and not in your application?

About MFC CString class - you just try to guess. Whu CString and not something else? I never had problems with CString class.
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9625560
I think you are building the components in *Shared* DLLs, this makes problems.

Wrong versions of MFC DLLS (like MSVCRTxx.dll etc) in Client and Development machine makes problems.

To avoid this, Build your ATL DLL in *STATIC*

Rosh :)
0
 
LVL 8

Assisted Solution

by:martynjpearson
martynjpearson earned 166 total points
ID: 9625687
You say you have problems when "many users requests come". Could it be that you are not coordinating access to a CString so that two threads are accessing the same CString at the same time?

One other possible pitfall with CString is the use of GetBuffer() - after calling GetBuffer(), you must call ReleaseBuffer() before you do anything other CString operations.

Hope this helps
Martyn
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Base1 Encode/Decode 3 81
Basic Java Case or If-Else statement... 3 57
Regular Expression Calculator Tester 2 79
Query Syntax 17 41
A short article about problems I had with the new location API and permissions in Marshmallow
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.

766 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question