CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2012
    Location
    Redmond, WA
    Posts
    4

    Using _bstr_t and _variant_t in a C++ application

    Hi there,

    I'm very new to COM programming, so my apologies in advance for the basic question. I've written a code in C++ that uses COM to access another application from out-of-process. This generally works well, but occasionally (20-30% of the time) my code causes a "crash" of the other application. I've determined the part of the code that is failing, but cannot figure out why.

    Basically what I'm trying to do is obtain some information from the application that I'm calling out-of-process. This information is stored as _bstr_t and _variant_t data in smart pointers. Here is the basics of how I'm getting at the data:

    Code:
    CComPtr<Document> MyDoc;
    CComQIPtr<Part> MyPart(MyDoc);
    
    int ii, ndim;
    double dvalue;
    _bstr_t bsName;
    _variant_t tempValue;
    
    ndim = MyDoc->Count;
    for (ii = 1; ii <= ndim; ii++)
       {
       bsName = MyPart->Name;
       tempValue = MyPart->Value;
       if (tempValue.vt == VT_R8) dvalue = tempValue.dblVal;
       else dvalue = 0.0;
       }
    
    MyPart.Release();
    MyDoc.Release();
    I'm not doing anything to "release" or "free" the _bstr_t or _variant_t variables, but my understanding is that I don't have to. But is this the potential problem? Or is there anything else that I might be doing wrong?

  2. #2
    Join Date
    Mar 2004
    Location
    Central Florida
    Posts
    293

    Re: Using _bstr_t and _variant_t in a C++ application

    It is very difficult to debug code snippets. Have you tried making a test application that only exercises the code that is failing?

    All I'm seeing ehre is data retrieval. A COM app should never cause the remote app to fail, unless it is passing bad data to the remote app.

    Are you passing any data to the remote app?

    Have you validated the data being passed to the remote app?
    "Effective teaching is the essence of leadership..."

    "There is no substitute for a carefully thought-out design."

    If you have found this post to be useful, please Rate it.

  3. #3
    Join Date
    Apr 2012
    Location
    Redmond, WA
    Posts
    4

    Re: Using _bstr_t and _variant_t in a C++ application

    I have made a test application that demonstrates the failure, although again it is intermittent.

  4. #4
    Join Date
    Apr 2012
    Location
    Redmond, WA
    Posts
    4

    Re: Using _bstr_t and _variant_t in a C++ application

    Sorry, the last post was not complete! I have also validated the data, and all looks fine.

    I should note that all of the data the are returned back from the COM pointers are returned correctly, regardless of whether the application crashes.

    So, the crash appears to occur upon termination of my code, suggesting that I'm doing something wrong with memory management as I leave the application. But the code is simple, and I just can't see any errors. I must be missing something.

  5. #5
    Join Date
    Mar 2004
    Location
    Central Florida
    Posts
    293

    Re: Using _bstr_t and _variant_t in a C++ application

    What you are doing for memory management in the COM client should have no affect on the COM server. Unless you are issuing commands to the COM server after retrieving your data. Are you?

    Are you sending data to the COM server app?
    "Effective teaching is the essence of leadership..."

    "There is no substitute for a carefully thought-out design."

    If you have found this post to be useful, please Rate it.

  6. #6
    Join Date
    Apr 2012
    Location
    Redmond, WA
    Posts
    4

    Re: Using _bstr_t and _variant_t in a C++ application

    The main thrust of what I'm doing with COM is provided in the snippet that I attached in my earlier email. Once I get the data that I'm interested in, all I do is use the Release() methods for the COM pointers and exit. Even if I do not use the Release() methods - as some have argued I should not, since I'm using smart pointers - the application will still intermittently crash.

    I'm going to try and work with the support staff for the application that I'm accessing to see if I can make progress on this issue. I just came here to make sure that I wasn't doing anything obviously wrong, and I don't think that I am. Thank you!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured