473,387 Members | 1,485 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,387 software developers and data experts.

CString::AllocSysString() needs to be free'd?

Hello!

I'm using VC++ 7.0 (.net 2002) and MFC.

I have a function (foo) that uses a CString variable (strCommand) as a
parameter. foo is accessing a database via ADO. Among the code is this:

pCommand->put_CommandText(strCommand.AllocSysString());
Do I need to free the memory allocated to string or will it do so
automatically?

// Anders

Crossposted to: microsoft.public.dotnet.languages.vc,microsoft.pub lic.vc.language,microsoft.public.vc.mfc
Replies will go to: microsoft.public.dotnet.languages.vc
Nov 17 '05 #1
10 11947
>Do I need to free the memory allocated to string or will it do so
automatically?


Anders,

The documentation says so:

"Commonly, if this string is passed to a COM function (as an [in]
parameter) this requires the caller to free the string. This can be
done by using SysFreeString, as described in the Platform SDK. See
Strings: Allocating and Releasing Memory for a BSTR for more
information on determining when the string is freed by the caller.
"

Dave
Nov 17 '05 #2
This actually depends upon the object you are using it with. In most cases,
the caller of the function is responsible for freeing it. Otherwise, the
object has no way of knowing whether you are finished with the BSTR object
or not.

So, you can use ::SysFreeString().

I always prefer using CComBSTR instead of this approach.

Cheers
Jagadeesh

"Anders Eriksson" <an*************@morateknikutveckling.se> wrote in message
news:kd**************@morateknikutveckling.se...
Hello!

I'm using VC++ 7.0 (.net 2002) and MFC.

I have a function (foo) that uses a CString variable (strCommand) as a
parameter. foo is accessing a database via ADO. Among the code is this:

pCommand->put_CommandText(strCommand.AllocSysString());
Do I need to free the memory allocated to string or will it do so
automatically?

// Anders

Crossposted to: microsoft.public.dotnet.languages.vc,microsoft.pub lic.vc.language,microsoft.
public.vc.mfc Replies will go to: microsoft.public.dotnet.languages.vc

Nov 17 '05 #3
You must free it.
--
Rodrigo Corral González [MVP]

microsoft.public.es.vc FAQ
http://vcfaq.europe.webmatrixhosting.net
Nov 17 '05 #4
"Anders Eriksson" <an*************@morateknikutveckling.se> wrote in
message news:kd**************@morateknikutveckling.se...
I have a function (foo) that uses a CString variable (strCommand) as a
parameter. foo is accessing a database via ADO. Among the code is this:
pCommand->put_CommandText(strCommand.AllocSysString());
Do I need to free the memory allocated to string or will it do so
automatically?


You do. It won't.
--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken
Nov 17 '05 #5
You need to free it (with SysFreeString). In COM, the caller is responsible
for freeing [in] parameters.

http://msdn.microsoft.com/library/de...csysstring.asp
says:

Use ::SysFreeString in the rare case that you need to deallocate the
returned string.

(but, you're not returning a string in this case.)

Better, use ATL::CComBSTR or _bstr_t rather than using
CString::AllocSysString. It will be freed when it goes out of scope. e.g.

pCommand->put_CommandText(_bstr_t(strCommand));

S.

"Anders Eriksson" <an*************@morateknikutveckling.se> wrote in message
news:kd**************@morateknikutveckling.se...
Hello!

I'm using VC++ 7.0 (.net 2002) and MFC.

I have a function (foo) that uses a CString variable (strCommand) as a
parameter. foo is accessing a database via ADO. Among the code is this:

pCommand->put_CommandText(strCommand.AllocSysString());
Do I need to free the memory allocated to string or will it do so
automatically?

// Anders

Crossposted to: microsoft.public.dotnet.languages.vc,microsoft.pub lic.vc.language,microsoft.
public.vc.mfc Replies will go to: microsoft.public.dotnet.languages.vc

Nov 17 '05 #6
"Anders Eriksson" <an*************@morateknikutveckling.se> wrote in message
news:kd**************@morateknikutveckling.se...
Hello!

I'm using VC++ 7.0 (.net 2002) and MFC.

I have a function (foo) that uses a CString variable (strCommand) as a
parameter. foo is accessing a database via ADO. Among the code is this:

pCommand->put_CommandText(strCommand.AllocSysString());
Do I need to free the memory allocated to string or will it do so
automatically?
// Anders


You still have to free it. The 'Alloc' in 'AllocSysString' should have said
it all :)

Tom.
Nov 17 '05 #7
Yes, You must free it.
--
Un saludo
Rodrigo Corral González [MVP]

microsoft.public.es.vc FAQ
http://vcfaq.europe.webmatrixhosting.net
Nov 17 '05 #8
Hej Anders,

It will not be freed automatically. It might, however, be freed by
put_CommandText, you will have to check this.

Johan Rosengren
Abstrakt Mekanik AB

"Anders Eriksson" <an*************@morateknikutveckling.se> a écrit dans le
message de news:kd**************@morateknikutveckling.se...
Hello!

I'm using VC++ 7.0 (.net 2002) and MFC.

I have a function (foo) that uses a CString variable (strCommand) as a
parameter. foo is accessing a database via ADO. Among the code is this:

pCommand->put_CommandText(strCommand.AllocSysString());
Do I need to free the memory allocated to string or will it do so
automatically?

// Anders

Crossposted to: microsoft.public.dotnet.languages.vc,microsoft.pub lic.vc.language,microsoft.
public.vc.mfc Replies will go to: microsoft.public.dotnet.languages.vc

Nov 17 '05 #9
On Fri, 13 Feb 2004 11:56:43 +0100, Anders Eriksson wrote:
pCommand->put_CommandText(strCommand.AllocSysString());

Do I need to free the memory allocated to string or will it do so
automatically?


Ok, so I need to free the memory. This is what I thought, but since the
class was written by someone else that claims to be more of an expert than
I, I had to ask.

If I do this then it will be ok or?

pCommand->put_CommandText(_bstr_t(strCommand));
// Anders
Nov 17 '05 #10
>Ok, so I need to free the memory. This is what I thought, but since the
class was written by someone else that claims to be more of an expert than
I, I had to ask.

If I do this then it will be ok or?

pCommand->put_CommandText(_bstr_t(strCommand));


Yes, that's OK Anders. The temporary _bstr_t will be destroyed after
the function call returns.

Dave
Nov 17 '05 #11

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

Similar topics

11
by: Rajesh Garg | last post by:
Why and in what situations is CString not preferred? RVG
8
by: Foxy Kav | last post by:
Hi everyone, Im currently doing first year UNI, taking a programming course in C++, for one project i have to create a simple array manipulator... that i have done, but i cant figure out how to...
2
by: Bnc119 | last post by:
Hello, I have written a C# COM server that has a few methods and a property called DataItems that returns an ArrayList. During the course of execution the ArrayList gets populated with several...
2
by: nobrain | last post by:
Release Version Only Problem.. this simple.. class include member variable CString, make many object and free. but programe using memory size is big and big..(only release version, debug...
3
by: Duncan Winn | last post by:
I am trying to convert a CString to a double using atof() however my nice CString of "0.58" gets converted to 0.579999223213147 etc making it impossible to compare with a different double...
5
by: Paul | last post by:
Hi, Any one knows how to convert CString to LPCWSTR? Please advice, thanks! -P
0
by: Silly | last post by:
I am writing a MC++ wrapper for a native dll, that will be called by C# applications. One of the functions returns a CString parameter: long GetProductStringDescriptor(int DevID, CString&...
16
by: Norman Diamond | last post by:
In an antique obsolete version of MFC, a CString expression could be subscripted in order to retrieve one element. Visual Studio 2005 defines CSimpleStringT::operator. At first glance it looks...
12
by: sas | last post by:
hi, i need that because the path functions for windows, like PathAppend and PathRemoveFileExt accept a writable zero terminated char*, but i didn't find that for std::string, with CString, i...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.