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

  • Thread starter Thread starter Johan Rosengren
  • Start date Start date
J

Johan Rosengren

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
 
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.public.vc.language,microsoft.public.vc.mfc
Replies will go to: microsoft.public.dotnet.languages.vc
 
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
 
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 said:
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
 
You need to free it (with SysFreeString). In COM, the caller is responsible
for freeing [in] parameters.

http://msdn.microsoft.com/library/d...c98/html/_mfc_cstring.3a3a.allocsysstring.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 said:
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.
 
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
 
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
 
Back
Top