Solved

Problems with CString class of MFC

Posted on 2003-10-26
7
664 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
thread-safe code in c++ 2 72
countX 22 70
parentbit challenge 3 52
java  and programming certification ? 4 65
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now