469,323 Members | 1,575 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

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 11546
>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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Rajesh Garg | last post: by
8 posts views Thread by Foxy Kav | last post: by
3 posts views Thread by Duncan Winn | last post: by
5 posts views Thread by Paul | last post: by
reply views Thread by Silly | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.