Click here to Skip to main content
15,893,722 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Program executes automatically Pin
Christian Graus31-May-05 11:10
protectorChristian Graus31-May-05 11:10 
GeneralRe: Program executes automatically Pin
FlyingTinman31-May-05 14:24
FlyingTinman31-May-05 14:24 
GeneralWhy BSTR conversion not working Pin
Imtiaz Murtaza30-May-05 8:18
Imtiaz Murtaza30-May-05 8:18 
GeneralRe: Why BSTR conversion not working Pin
Christian Graus30-May-05 12:05
protectorChristian Graus30-May-05 12:05 
GeneralRe: Why BSTR conversion not working Pin
S. Senthil Kumar30-May-05 20:33
S. Senthil Kumar30-May-05 20:33 
GeneralRe: Why BSTR conversion not working Pin
Christian Graus31-May-05 10:59
protectorChristian Graus31-May-05 10:59 
GeneralRe: Why BSTR conversion not working Pin
S. Senthil Kumar31-May-05 17:17
S. Senthil Kumar31-May-05 17:17 
GeneralRe: Why BSTR conversion not working Pin
S. Senthil Kumar30-May-05 19:57
S. Senthil Kumar30-May-05 19:57 
The reason why this is happening is because you're not using _bstr_t correctly. When you say
s.firstName = _bstr_t(firstName.c_str());

what's happenings is, _bstr_t constructor gets called with firstname.c_str() as the argument. It does a SysAllocString and creates a BSTR internally (let's call it m_BSTR). Because you have the BSTR. on the LHS of the assignment statement, the conversion operator
operator BSTR()
{
   return m_BSTR;
}

executes and you get a BSTR in s.firstName. What next happens is the reason why you're getting incorrect values. The destructor for _bstr_t runs (because it's lifetime ends after the assigment statement completes) and it does a SysFreeString on m_BSTR.

So what you have in s.firstName is essentially a dangling pointer, it's pointing to freed memory. You're just lucky that it didn't crash, it's reusing that freed memory and maybe that's why you're getting the same BSTR value.

The same case applies to CComBSTR also. The solution is simple, you just replace your plain BSTRs in the Student structure with a CComBSTR or _bstr_t. That way, the copy constructor (or the assignment operator) runs and does a SysAllocString for you, so you end up with a valid BSTR.

Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
GeneralBode diagram Pin
alvarillo30-May-05 7:39
alvarillo30-May-05 7:39 
GeneralRe: Bode diagram Pin
Ravi Bhavnani30-May-05 7:55
professionalRavi Bhavnani30-May-05 7:55 
GeneralCComboBox & MSVS .NET 2003 Pin
oldbayray30-May-05 6:13
oldbayray30-May-05 6:13 
GeneralRe: CComboBox & MSVS .NET 2003 Pin
Jack Puppy30-May-05 6:57
Jack Puppy30-May-05 6:57 
GeneralRe: CComboBox & MSVS .NET 2003 Pin
oldbayray30-May-05 7:45
oldbayray30-May-05 7:45 
GeneralRe: CComboBox & MSVS .NET 2003 Pin
Ravi Bhavnani30-May-05 7:56
professionalRavi Bhavnani30-May-05 7:56 
GeneralRe: CComboBox & MSVS .NET 2003 Pin
Tom Archer30-May-05 11:29
Tom Archer30-May-05 11:29 
GeneralRe: CComboBox & MSVS .NET 2003 Pin
Ravi Bhavnani30-May-05 11:35
professionalRavi Bhavnani30-May-05 11:35 
GeneralRe: CComboBox & MSVS .NET 2003 Pin
Jack Puppy30-May-05 12:43
Jack Puppy30-May-05 12:43 
GeneralRe: CComboBox & MSVS .NET 2003 Pin
oldbayray30-May-05 12:52
oldbayray30-May-05 12:52 
GeneralRe: CComboBox & MSVS .NET 2003 Pin
oldbayray30-May-05 12:51
oldbayray30-May-05 12:51 
GeneralDisplay file!!! Pin
Anonymous30-May-05 5:41
Anonymous30-May-05 5:41 
GeneralRe: Display file!!! Pin
Tom Archer30-May-05 11:36
Tom Archer30-May-05 11:36 
QuestionHow to crop an image in an application based on dialog?(VC++6.0) Pin
qqworm30-May-05 4:43
qqworm30-May-05 4:43 
AnswerRe: How to crop an image in an application based on dialog?(VC++6.0) Pin
Christian Graus30-May-05 12:06
protectorChristian Graus30-May-05 12:06 
Questionwhat is the proper way to OnInitDialog? Pin
IlanTal30-May-05 4:42
IlanTal30-May-05 4:42 
AnswerRe: what is the proper way to OnInitDialog? Pin
Jack Puppy30-May-05 7:01
Jack Puppy30-May-05 7:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.