473,395 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Converting const char* to CString without Memory Leak

What is the correct way to convert a const char* to a CString? I'm
somewhat of a newbie and have tried several ways. While they all
convert ok, I'm using a profiler that shows a memory leak for every
option. Here's what I have tried:

const char* test;
test = getMyChar();
//CString myCString((LPCTSTR)test);
//CString myCString(test);
CString myCString = new CString(test);
delete myCString;

Thanks in advance for your help

Jul 23 '05 #1
3 9107

<ns******@aol.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
What is the correct way to convert a const char* to a CString? I'm
somewhat of a newbie and have tried several ways. While they all
convert ok, I'm using a profiler that shows a memory leak for every
option. Here's what I have tried:

const char* test;
test = getMyChar();
//CString myCString((LPCTSTR)test);
//CString myCString(test);
CString myCString = new CString(test);
delete myCString;

Thanks in advance for your help


The CString class is, I believe, and MFC class, and not topical in this
newsgroup.

However, the problem most likely lies in the fact that you're making a call
to something called getMyChar(), which, I must assume (not seing its code),
allocates storage for the string. You probably need to recover THAT memory,
most likely by calling "delete [] test;". But not seeing the code for
getMyChar, I can't say for sure.

By the way, if that IS the way that getMyChar works, I see a couple problems
with it. First, its name implies that it gets a char, not a string. It
should say precisely what it does.

But more important, it's not the best practice to have allocation done
inside a function like that. It hides the fact that you need to do the
delete yourself.

A better method is to allocate the memory yourself, and pass the array (and
its length, to be safe) to a function which fills out the array for you.

Or, your function could work directly on a CString, instead of a char*
pointer.

Or, a better idea would be to use the RAII design. Look it up on Google for
more details.

Or, even better would be to use the std::string class instead of a char
array and CString.

-Howard


Jul 23 '05 #2
On Wed, 27 Apr 2005 17:31:19 GMT, Howard wrote:
Or, even better would be to use the std::string class instead of a char
array and CString.


In general, yes, but many MFC functions and classes take CString
parameters, and it's often easier to just work with CStrings than to
keep converting back and forth.

--
Greg Schmidt gr***@trawna.com
Trawna Publications http://www.trawna.com/
Jul 23 '05 #3
Thank you both for your input and good ideas.
It seems as though there wasn't really a problem after all. The values
were being stored in cache, and the profiler was pointing at that piece
of code because that's where it was initially created. (I believe this
is how the profiler was working). It doens't show a leak when the
strings are cleared out of cache.

(The example I gave was kind of a slimmed down version of what the code
was actually doing. Like Greg Schmidt mentioned, I was calling a
different class that was returning the const char*, and I couldn't
change that)

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Fausto Lopez | last post by:
I'm getting the following error: 'strlen' : cannot convert parameter 1 from 'class CString' to 'const char *' when I try to compile the following code: HRESULT AnsiToUnicode(CString pszA,...
2
by: TonyM | last post by:
Q: Is there a good way to overcome this apparent bug without modifying the mfc code? ___________________________________ Info: Although it is NOT a good idea, I seemed to have perhaps located a...
2
by: chasgl | last post by:
I have a older C++ project from a pre .net Visual studio. I just want it to run "the old way" in the new environment, not yet wanting to do any dotnet stuff. Several conversions that worked...
7
by: jamihuq | last post by:
Hello, I would like to convert the following inline function to a macro. Can someone help? Thx Jami inline char * fromDESC(const char * &aDesC)
9
by: Gregory.A.Book | last post by:
I am interested in converting sets of 4 bytes to floats in C++. I have a library that reads image data and returns the data as an array of unsigned chars. The image data is stored as 4-byte floats....
6
by: DaTurk | last post by:
Hi, I have several interfaces in CLI that I access via c#. My problem is, is that down in the unmanaged c++, which the CLI lies on top of, I have a lot of c_str() happening. But all of my...
2
by: Sean F. Aitken | last post by:
Good afternoon, We have an app that uses a CMap with CString types for values and accepts LPCSTR as the parameter. The object being created is on the heap (created dynamically). A leak detector...
11
by: Szabolcs Nagy | last post by:
is there a reason why const is not compile-time constant? the usual example, where it'd be nice is array declaration: const int N = 4; float arr or one may want to use an enum for indices:...
7
by: Bill Davy | last post by:
I want to be able to write (const char*)v where v is an item of type Class::ToolTypeT where ToolTypeT is an enumeration and I've tried everything that looks sensible. There's an ugly solution, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.