?
Solved

Problems with CString class of MFC

Posted on 2003-10-26
7
Medium Priority
?
729 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
[X]
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
  • 2
7 Comments
 
LVL 23

Accepted Solution

by:
Roshan Davis earned 672 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 664 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 664 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Simple Linear Regression
Starting up a Project
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

765 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