473,382 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

_bstr_t leads to a crash


Hi,

I am using _bstr_t class in a function. This is used in an application
that is used in an multi-threaded environment. The function is
implemented as follows:

int Function(wchar_t *sqlCmd)
{
_bstr_t cmd ;

cmd = sqlCmd ;
CoInitialize(NULL);
..
..
..
return 0;
}
The above function uses ADO functions to execute SQL command. Here, in
statement

cmd = sqlCmd

while executing sqlCmd "Sometimes" an exception is thrown this happens
because though sqlCmd contains the sqlCmd to be executed. It is not
assigned to cmd which is passed to Execute function of ADODB.

Also, it crashes sometimes when the function returns and destructor for
_bstr_t is called.

Please, help me find out the answers to the following questions.
1) Is there any limitation for the overloaded '=' operation in _bstr_t,
due to which somestimes the sqlCmd is not assigned to cmd.

2)Before calling the destructor for _bstr_t is there any operation that
should be used to avoid the crash?

Thanks for you time.

Regards,
Ashish Choudhary

--
ashish_chap
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Apr 6 '06 #1
4 5290
ashish_chap wrote:
I am using _bstr_t class in a function. This is used in an application
that is used in an multi-threaded environment. The function is
implemented as follows:
Also, it crashes sometimes when the function returns and destructor for
_bstr_t is called.

Please, help me find out the answers to the following questions.
1) Is there any limitation for the overloaded '=' operation in _bstr_t,
due to which somestimes the sqlCmd is not assigned to cmd.

2)Before calling the destructor for _bstr_t is there any operation that
should be used to avoid the crash?

as _bstr_t is simply encapsulation for COM support functions (SysAllocString,
SysFreeString etc. ) You should not use these function when COM susbsystem is
(not yet or already) unavailable. What you do is:

1. initialize _bstr_t before COM is available (_bstr_t may delay call to
SysAllocString till you actually put something in it, but you are still in danger)

2. I also guess that you call CoUninitialize before _bstr_t destructor is
called. This means that when SysFreeString is called, there is no COM
sybsystem to handle your call.

What you should do is to initialize COM before you start any COM-related
activity and un-initialize when you are 100% done. Like here:

#include <cstdio>
#include <stdexcept>

#include <comdef.h>
#include <comutil.h>

int main()
{
int result = 13;
if (FAILED(CoInitialize(NULL)))
{
puts("Failed to initialize COM");
return result;
}

try
{
_bstr_t a = "blablabla";
// .... call your functions that use COM
result = 0;
}
catch(std::exception& e)
{
puts(e.what());
result = 1;
}
catch(_com_error e)
{
puts(static_cast<const char*>(e.Description()));
result = 2;
}

CoUninitialize();
return result;
}
B.
Apr 6 '06 #2
No, this is not true. The _bstr_t wrapper does not depend on the COM library
(initialized by a CoInitialize call), you can use this class without calling
into COM.

Willy.

"Bronek Kozicki" <br**@rubikon.pl> wrote in message
news:ee**************@TK2MSFTNGP05.phx.gbl...
| ashish_chap wrote:
| > I am using _bstr_t class in a function. This is used in an application
| > that is used in an multi-threaded environment. The function is
| > implemented as follows:
| > Also, it crashes sometimes when the function returns and destructor for
| > _bstr_t is called.
| >
| > Please, help me find out the answers to the following questions.
| > 1) Is there any limitation for the overloaded '=' operation in _bstr_t,
| > due to which somestimes the sqlCmd is not assigned to cmd.
| >
| > 2)Before calling the destructor for _bstr_t is there any operation that
| > should be used to avoid the crash?
|
|
| as _bstr_t is simply encapsulation for COM support functions
(SysAllocString,
| SysFreeString etc. ) You should not use these function when COM susbsystem
is
| (not yet or already) unavailable. What you do is:
|
| 1. initialize _bstr_t before COM is available (_bstr_t may delay call to
| SysAllocString till you actually put something in it, but you are still in
danger)
|
| 2. I also guess that you call CoUninitialize before _bstr_t destructor is
| called. This means that when SysFreeString is called, there is no COM
| sybsystem to handle your call.
|
| What you should do is to initialize COM before you start any COM-related
| activity and un-initialize when you are 100% done. Like here:
|
| #include <cstdio>
| #include <stdexcept>
|
| #include <comdef.h>
| #include <comutil.h>
|
| int main()
| {
| int result = 13;
| if (FAILED(CoInitialize(NULL)))
| {
| puts("Failed to initialize COM");
| return result;
| }
|
| try
| {
| _bstr_t a = "blablabla";
| // .... call your functions that use COM
| result = 0;
| }
| catch(std::exception& e)
| {
| puts(e.what());
| result = 1;
| }
| catch(_com_error e)
| {
| puts(static_cast<const char*>(e.Description()));
| result = 2;
| }
|
| CoUninitialize();
| return result;
| }
|
|
| B.
Apr 6 '06 #3
Is this function a thread procedure? Then you need to CoUnitialize before
returning.
If it's not a thread proc, you should not CoInitialize here.

Willy.

"ashish_chap" <as****************@mail.codecomments.com> wrote in message
news:as****************@mail.codecomments.com...
|
| Hi,
|
| I am using _bstr_t class in a function. This is used in an application
| that is used in an multi-threaded environment. The function is
| implemented as follows:
|
| int Function(wchar_t *sqlCmd)
| {
| _bstr_t cmd ;
|
| cmd = sqlCmd ;
| CoInitialize(NULL);
| .
| .
| .
| return 0;
| }
|
|
| The above function uses ADO functions to execute SQL command. Here, in
| statement
|
| cmd = sqlCmd
|
| while executing sqlCmd "Sometimes" an exception is thrown this happens
| because though sqlCmd contains the sqlCmd to be executed. It is not
| assigned to cmd which is passed to Execute function of ADODB.
|
| Also, it crashes sometimes when the function returns and destructor for
| _bstr_t is called.
|
| Please, help me find out the answers to the following questions.
| 1) Is there any limitation for the overloaded '=' operation in _bstr_t,
| due to which somestimes the sqlCmd is not assigned to cmd.
|
| 2)Before calling the destructor for _bstr_t is there any operation that
| should be used to avoid the crash?
|
| Thanks for you time.
|
| Regards,
| Ashish Choudhary
|
|
|
| --
| ashish_chap
| ------------------------------------------------------------------------
| Posted via http://www.codecomments.com
| ------------------------------------------------------------------------
|
Apr 6 '06 #4
Willy Denoyette [MVP] wrote:
No, this is not true. The _bstr_t wrapper does not depend on the COM library
(initialized by a CoInitialize call), you can use this class without calling


indeed, you are right, thanks for rectifying my mistake. However, if there is
any use of other COM wrappers (#import etc), these will release COM objects at
the point of their destruction (end of scope). If that comes after
CoUninitialize, memory access violation will happen. The other thing coming to
my mind is misuse of _bstr_t , I gave explanation and code samples in this thread:
http://groups.google.com/group/micro...a3e407b6b25f0/
B.
Apr 7 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Eric Brunel | last post by:
Hi all, I was creating a Tkinter widget in the style of the reversed tabs below Excel worksheets and I stepped in a serious problem: the code I made makes python crash with a seg fault, bus...
10
by: xixi | last post by:
i have db2 udb v8.1 on windows 64 bit 2003 server, after db2 server start , i found this in the db2diag.log, is this error? 2004-05-05-15.28.30.780000 Instance:DB2 Node:000...
1
by: Paul Drummond | last post by:
Hi, I hope I am not posting in the wrong place! I have posted to the MFC group but thought I might get more of a response here as the question is C++ specific. If I am wrong to post here or...
5
by: bluter | last post by:
We have server components which were created by a third party and compiled in VC++5 (sp3). They run fine on NT4 and 2000, however during testing of our migration to Server 2003, these components...
0
by: ashish_chap | last post by:
Hi All, Sorry, for the delay in reply. The _bstr_t variable used in my code contains a SQL command and is then passed to ADO function. An Execute() fuction of _ConnectionPtr in ADO is used to...
34
by: NewToCPP | last post by:
Hi, Why does a C/C++ programs crash? When there is access to a null pointer or some thing like that programs crash, but why do they crash? Thanks.
12
by: benjamin.krulewitch | last post by:
I'm debugging an issue with a C program that causes the computer to crash, and I'm attempting to log information immediately before the crash occurs. I us my BKprintLog function (see below) to...
110
by: alf | last post by:
Hi, is it possible that due to OS crash or mysql itself crash or some e.g. SCSI failure to lose all the data stored in the table (let's say million of 1KB rows). In other words what is the worst...
11
by: =?Utf-8?B?R2VyaGFyZA==?= | last post by:
I have run into a situation that if a page/tab that uses the Ajax toolkit (using .net version 3.5) is closed before the Ajax enable controls complete loading, then IE locks up. Does it in both IE7...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.