Solved

Problems with CString class of MFC

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This is about my first experience with programming Arduino.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

770 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