_bstr_t help needed

This is a discussion on _bstr_t help needed within the C++ Programming forums, part of the General Programming Boards category; Hi all , could anyone please help , I am keeping getting app crashes with my app I suspect the ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    _bstr_t help needed

    Hi all ,
    could anyone please help ,
    I am keeping getting app crashes with my app
    I suspect the following code
    Could anyone have a look and help ,
    In addition , could any one explain what are the diffrences between the _bstr_t constructor , and if I have to free them ?

    Thanks very much

    Code:
    BOOL CMSInterface::ConvertBSTRtoCharWithoutTerminator(BSTR i_sStr,char* o_Str,long *o_Length)
    {
    	bool retVal = true;
    	_bstr_t bstrObj(i_sStr, FALSE);
    	*o_Length = bstrObj.length();
    	
    	long lLen = WideCharToMultiByte(CP_ACP, 0, i_sStr,(*o_Length-2), o_Str, *o_Length, NULL, NULL);
    
    	
    	if(lLen)
    	{
    		*o_Length-= 2;
    		retVal = true;
    	}
    	else
    	{
    		*o_Length =0;
    		retVal = false;
    	}
    	
    	return retVal;
    
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    when you use the _bstr_t constructor with the second argument FALSE, the _bstr_t class takes ownership of the BSTR object passed as the first argument. That means you can not laster use SysFreeString() to free up the memory because _bstr_t will do that when the class is destroyed. My hunch is that the function that called ConvertBSTRtoCharWithoutTerminator() is calling SystFreeString() on i_sStr, and that will crash because it has already been freed when ConvertBSTRtoCharWithoutTerminator returns.

    One of two possible solutinos:
    1. do not use the constructor that takes ownership of the string. Use one of the other constructors that will duplicate the string.
    Code:
    _bstr_t b(i_sStr);
    or
    2. Do not use _bstr_t class at all, but use the BSTR directly.
    Code:
    // get the number of bytes allocated to the BSTR
    *o_Length = *(long *)(i_sStr-2);
    // or get the string length, which may or may not be the same
    *o_Length = wcslen(i_sStr);
    Last edited by Ancient Dragon; 11-04-2005 at 08:25 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 10:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 12:55 PM
  3. Help Please
    By Moose112 in forum C++ Programming
    Replies: 9
    Last Post: 12-10-2006, 07:16 PM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-01-2004, 12:24 AM
  5. semi-colon - where is it needed
    By kes103 in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2003, 06:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21