Append BStr
-
Below is the code to append Bstr, but in debug mode it gives error ' ::SysFreeString(dest); ' and in release mode if i dont write ' ::SysFreeString(dest); ' it corrupts heap. What optimization i do. BSTR StringAppend(const BSTR dest,const BSTR src) { long destLen=::SysStringByteLen(dest); long srcLen=::SysStringByteLen(src); BSTR st3 = ::SysAllocStringByteLen(NULL,destLen+srcLen); try { if(st3==NULL) { throw "Insufficient Memory"; } else { wcscpy(st3, dest); wcscat(st3, src); ::SysFreeString(dest);// here it gives error in debug mode } } catch(char * error) { throw error; } return st3; }
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
-
Why are you freeing
dest
? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]CPallini wrote:
Why are you freeing dest?
Because if i do not free, it gives error that heap is violated in release mode and when i traced down it goes in the NULL condition.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
-
why are you modifying a const input parameter ie deleting :mad:
Press F1 for help or google it. Greetings from Germany
KarstenK wrote:
why are you modifying a const input parameter ie deleting Mad
if i do not free all the memory gets exhausted.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
-
CPallini wrote:
Why are you freeing dest?
Because if i do not free, it gives error that heap is violated in release mode and when i traced down it goes in the NULL condition.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
Please show us the calling code. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Please show us the calling code. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Here is my calling code. value variable is of type BSTR BSTR GetNewRow() { value=GetLoggedInUser(); BSTR strComma=::SysAllocString(L","); BSTR strNewLine=::SysAllocString(L"\n"); value=StringAppend(value,strComma); value=StringAppend(value,strCategory); value=StringAppend(value,strComma); value=StringAppend(value,strItemName); value=StringAppend(value,strComma); value=StringAppend(value,strInstallDate); value=StringAppend(value,strComma); value=StringAppend(value,strVersion); value=StringAppend(value,strComma); value=StringAppend(value,strValue1); value=StringAppend(value,strComma); value=StringAppend(value,strValue2); value=StringAppend(value,strComma); value=StringAppend(value,strValue3); value=StringAppend(value,strNewLine); ::SysFreeString(strComma); ::SysFreeString(strNewLine); return value; }
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
-
KarstenK wrote:
why are you modifying a const input parameter ie deleting Mad
if i do not free all the memory gets exhausted.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
If it had to be released from within the function, why does it come into the function as a
const
parameter? Isn't this strikingly flashy, yelling "Hey look! I'm the silliest!"?It is a crappy thing, but it's life -^ Carlo Pallini
-
KarstenK wrote:
why are you modifying a const input parameter ie deleting Mad
if i do not free all the memory gets exhausted.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
-
To free up the strings is correct and necessary, BUT NOT in a subroutine. Make it after the sub routine call. :doh:
Press F1 for help or google it. Greetings from Germany
This my calling code tell me where do i free BSTR GetNewRow() { value=GetLoggedInUser(); BSTR strComma=::SysAllocString(L","); BSTR strNewLine=::SysAllocString(L"\n"); value=StringAppend(value,strComma); value=StringAppend(value,strCategory); value=StringAppend(value,strComma); value=StringAppend(value,strItemName); value=StringAppend(value,strComma); value=StringAppend(value,strInstallDate); value=StringAppend(value,strComma); value=StringAppend(value,strVersion); value=StringAppend(value,strComma); value=StringAppend(value,strValue1); value=StringAppend(value,strComma); value=StringAppend(value,strValue2); value=StringAppend(value,strComma); value=StringAppend(value,strValue3); value=StringAppend(value,strNewLine); ::SysFreeString(strComma); ::SysFreeString(strNewLine); return value; }
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
-
This my calling code tell me where do i free BSTR GetNewRow() { value=GetLoggedInUser(); BSTR strComma=::SysAllocString(L","); BSTR strNewLine=::SysAllocString(L"\n"); value=StringAppend(value,strComma); value=StringAppend(value,strCategory); value=StringAppend(value,strComma); value=StringAppend(value,strItemName); value=StringAppend(value,strComma); value=StringAppend(value,strInstallDate); value=StringAppend(value,strComma); value=StringAppend(value,strVersion); value=StringAppend(value,strComma); value=StringAppend(value,strValue1); value=StringAppend(value,strComma); value=StringAppend(value,strValue2); value=StringAppend(value,strComma); value=StringAppend(value,strValue3); value=StringAppend(value,strNewLine); ::SysFreeString(strComma); ::SysFreeString(strNewLine); return value; }
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
Mogaambo wrote:
BSTR strComma=::SysAllocString(L","); BSTR strNewLine=::SysAllocString(L"\n");
What about changing
BSTR
to_bstr_t
? Defined in comutil.h The MSDN link says that, A _bstr_t object encapsulates the BSTR data type. The class manages resource allocation and deallocation through function calls to SysAllocString and SysFreeString and other BSTR APIs when appropriate. The _bstr_t class uses reference counting to avoid excessive overhead. --++++++--- Use the+= operator
of_bstr_t
to append characters to the end. ifvalue
is also declared as a type of_bstr_t
then code can be re-written as,value += strComma;
value += strNewLine;:thumbsup:
modified on Wednesday, May 13, 2009 5:12 AM
-
Here is my calling code. value variable is of type BSTR BSTR GetNewRow() { value=GetLoggedInUser(); BSTR strComma=::SysAllocString(L","); BSTR strNewLine=::SysAllocString(L"\n"); value=StringAppend(value,strComma); value=StringAppend(value,strCategory); value=StringAppend(value,strComma); value=StringAppend(value,strItemName); value=StringAppend(value,strComma); value=StringAppend(value,strInstallDate); value=StringAppend(value,strComma); value=StringAppend(value,strVersion); value=StringAppend(value,strComma); value=StringAppend(value,strValue1); value=StringAppend(value,strComma); value=StringAppend(value,strValue2); value=StringAppend(value,strComma); value=StringAppend(value,strValue3); value=StringAppend(value,strNewLine); ::SysFreeString(strComma); ::SysFreeString(strNewLine); return value; }
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
It looks like the problem is in your bad mixing of char and wchar calls: with the following line
BSTR st3 = ::SysAllocStringByteLen(NULL,destLen+srcLen);
you're allocating
(destLen + srcLen + 1)
bytes. while in the following callswcscpy(st3, dest);
wcscat(st3, src);you need,
(destLen + srcLen + 2)
bytes. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
This my calling code tell me where do i free BSTR GetNewRow() { value=GetLoggedInUser(); BSTR strComma=::SysAllocString(L","); BSTR strNewLine=::SysAllocString(L"\n"); value=StringAppend(value,strComma); value=StringAppend(value,strCategory); value=StringAppend(value,strComma); value=StringAppend(value,strItemName); value=StringAppend(value,strComma); value=StringAppend(value,strInstallDate); value=StringAppend(value,strComma); value=StringAppend(value,strVersion); value=StringAppend(value,strComma); value=StringAppend(value,strValue1); value=StringAppend(value,strComma); value=StringAppend(value,strValue2); value=StringAppend(value,strComma); value=StringAppend(value,strValue3); value=StringAppend(value,strNewLine); ::SysFreeString(strComma); ::SysFreeString(strNewLine); return value; }
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
-
Mogaambo wrote:
BSTR strComma=::SysAllocString(L","); BSTR strNewLine=::SysAllocString(L"\n");
What about changing
BSTR
to_bstr_t
? Defined in comutil.h The MSDN link says that, A _bstr_t object encapsulates the BSTR data type. The class manages resource allocation and deallocation through function calls to SysAllocString and SysFreeString and other BSTR APIs when appropriate. The _bstr_t class uses reference counting to avoid excessive overhead. --++++++--- Use the+= operator
of_bstr_t
to append characters to the end. ifvalue
is also declared as a type of_bstr_t
then code can be re-written as,value += strComma;
value += strNewLine;:thumbsup:
modified on Wednesday, May 13, 2009 5:12 AM
Thanks to all programming bees in helping me to solve my problems.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
-
It looks like the problem is in your bad mixing of char and wchar calls: with the following line
BSTR st3 = ::SysAllocStringByteLen(NULL,destLen+srcLen);
you're allocating
(destLen + srcLen + 1)
bytes. while in the following callswcscpy(st3, dest);
wcscat(st3, src);you need,
(destLen + srcLen + 2)
bytes. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Thanks working great now . i don't know the +2 funda , actually i am c# developer but my firm wants me to develop project in c++. ::SysFreeString(dest); can i free this in StringAppend function itself or after my function returns.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
-
Thanks working great now . i don't know the +2 funda , actually i am c# developer but my firm wants me to develop project in c++. ::SysFreeString(dest); can i free this in StringAppend function itself or after my function returns.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
Technically you can do it inside. However you probably should revise your design, for instance using a prototype like the following for your function:
HRESULT StringAppend( BSTR * pdst, const BSTR src);
or, better, using, as suggested, the handy
_bstr_t
wrapper. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Technically you can do it inside. However you probably should revise your design, for instance using a prototype like the following for your function:
HRESULT StringAppend( BSTR * pdst, const BSTR src);
or, better, using, as suggested, the handy
_bstr_t
wrapper. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]1. BSTR * pdst why pointer here. BSTR is itself a pointer. 2. What code you have write for StringAppend function ? I want to take some idea from you.It would be very helpful for me.It's a humble request from a programmer to a professional.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
-
1. BSTR * pdst why pointer here. BSTR is itself a pointer. 2. What code you have write for StringAppend function ? I want to take some idea from you.It would be very helpful for me.It's a humble request from a programmer to a professional.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
Mogaambo wrote:
1. BSTR * pdst why pointer here. BSTR is itself a pointer.
Because the function will internally change the value of the original pointer, i.e.
pdst
is aINOUT
parameter (this way you state clearly that the function may alter the originaldst
string).Mogaambo wrote:
2. What code you have write for StringAppend function ?
Something like
HRESULT StringAppend(BSTR * pdest, const BSTR src)
{
if (pdest == NULL) return E_FAIL;
long destLen=::SysStringLen(*pdest);
long srcLen=::SysStringLen(src);BSTR tmp = ::SysAllocStringLen(NULL,destLen+srcLen);
if( tmp == NULL) return E_FAIL;
wcscpy(tmp, *pdest);
wcscat(tmp, src);
::SysFreeString(*pdest);pdest = &tmp;
*pdest = tmp;
return S_OK;
}Beware: I didn't check this code (moreover I've used just the very basic
E_FAIL
failure return value, a good function should be more informative on failure.If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]modified on Wednesday, May 13, 2009 8:02 AM
-
Mogaambo wrote:
1. BSTR * pdst why pointer here. BSTR is itself a pointer.
Because the function will internally change the value of the original pointer, i.e.
pdst
is aINOUT
parameter (this way you state clearly that the function may alter the originaldst
string).Mogaambo wrote:
2. What code you have write for StringAppend function ?
Something like
HRESULT StringAppend(BSTR * pdest, const BSTR src)
{
if (pdest == NULL) return E_FAIL;
long destLen=::SysStringLen(*pdest);
long srcLen=::SysStringLen(src);BSTR tmp = ::SysAllocStringLen(NULL,destLen+srcLen);
if( tmp == NULL) return E_FAIL;
wcscpy(tmp, *pdest);
wcscat(tmp, src);
::SysFreeString(*pdest);pdest = &tmp;
*pdest = tmp;
return S_OK;
}Beware: I didn't check this code (moreover I've used just the very basic
E_FAIL
failure return value, a good function should be more informative on failure.If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]modified on Wednesday, May 13, 2009 8:02 AM
BSTR tmp = ::SysAllocStringLen(NULL,destLen+srcLen); dude you haven't added +2 bytes, it is crashing as that of mine, but when i added +2 it is running fine, so whenever i want allocate BSTR do i have 2 add + bytes. BSTR tmp = ::SysAllocStringLen(NULL,destLen+srcLen+2);
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
-
this is very lame code. You better make at first the output string with CString (or otherwise) and at last ::SysAllocString() :rolleyes:
Press F1 for help or google it. Greetings from Germany
What code you have written if you have to append the BSTR. It's a humble request from programmer to professional.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
-
BSTR tmp = ::SysAllocStringLen(NULL,destLen+srcLen); dude you haven't added +2 bytes, it is crashing as that of mine, but when i added +2 it is running fine, so whenever i want allocate BSTR do i have 2 add + bytes. BSTR tmp = ::SysAllocStringLen(NULL,destLen+srcLen+2);
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
Mogaambo wrote:
dude you haven't added +2 bytes
I know, I don't need to (I'm using
SysAllocStringLen
, notSysAllocStringByteLen
).Mogaambo wrote:
it is crashing as that of mine, but when i added +2 it is running fine
This is very strange.
Mogaambo wrote:
so whenever i want allocate BSTR do i have 2 add + bytes
Nope. Even if your point about the crash is true (and is true, I know) this is a wrong conclusion. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
What code you have written if you have to append the BSTR. It's a humble request from programmer to professional.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford