Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8107473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:47:04+00:00 2026-06-06T00:47:04+00:00

The following code worked just fine thank you with one COM client, but with

  • 0

The following code worked just fine thank you with one COM client, but with a new client (the updated version of the same software) string_array_to_bstr_safearray_variant throws an access violation and everything dies.

Can anyone tell me if I’m doing something wrong that I was getting away with before..? Am I failing to allocate memory properly?

#include "comutil.h"

void string_array_to_bstr_safearray_variant(long arraylength,char ** in_array,VARIANT *out_variant)
{
    CComSafeArray<BSTR> out_array;
    ATLENSURE_SUCCEEDED(out_array.Create(arraylength));
    for (int i=0;i<arraylength;i++)
        ATLENSURE_SUCCEEDED(out_array.SetAt(i,_com_util::ConvertStringToBSTR(in_array[i])));
    CComVariant ccv(out_array);
    HRESULT hr = ccv.Detach(out_variant);
    ATLENSURE_SUCCEEDED(hr);
}

//names: output parameter to contain variant holding safearray of bstrs
STDMETHODIMP CCalculation::get_output_shortnames(VARIANT* names)
{
    char** names_array = calc_get_short_output_names(calc); //this works fine
    string_array_to_bstr_safearray_variant(output_length,names_array,names); //this fails before returning
    return S_OK;
}

Edit: Debugger info

Without a debugger, I get an access violation.

Stepping through this code with the debugger it appears to work. output_length is set correctly; out_array is created and filled correctly and so is out_variant as far as I can tell through variable watching. However, the COM client still fails, saying "lisp value has no coercion to VARIANT with this type: #<safearray...>" (which is odd because a previous version of the client interprets the return value just fine). It then crashes complaining that it’s out of memory.

Running the code inside the debugger, but letting it run rather than stepping, it failed inside the constructor of CComVariant, complaining of an invalid argument thrown because the inner call to SafeArrayCopy failed.

Edit: on another recent step-through it failed in the loop, so maybe the problem is with CComSafeArray as @terriblememory suggests?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T00:47:05+00:00Added an answer on June 6, 2026 at 12:47 am

    Well finally I found the answer to this! The code posted in the question is correct as it is. There was earlier code causing undefined behaviour:

    STDMETHODIMP CCalculation::configure(VARIANT radii) // radii contains a safearray of doubles
    {
    CComSafeArray<double> radii_sa;
    radii_sa.Attach(radii.parray);
    ULONG num_radii = radii_sa.GetCount();
    
    //unpack radii array into c-style array
    double *radii_array = new double[num_radii];
    for (long i=0;i<num_radii;i++)
    radii_array[i] = radii_sa.GetAt(i);
    
    //...do something with radii_array...
    
    delete[] radii_array;
    
    return S_OK;
    }
    

    Did you spot the deliberate mistake? COM rules say that radii is owned by the client, not my dll. By attaching to it then letting the wrapper go out of scope, I was deallocating the safearray. Fixed by adding this before the return statement:

    radii_sa.Detach();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used the following PHP code in shared hosting and it worked just fine:
The following code failed when I upgraded to 1.4.1, and worked ok when I
I have the following bit of code that worked as expected before we upgraded
The following code works fine: person = {:a=>:A, :b=>:B, :c=>:C} berson = {:a=>:A1, :b=>:B1,
I just worked with C# but do not have any idea on Silverlight so
I wanted a TextBox with just digits as entry. So i wrote following code
So I have the following code: File txtFileDir = new File(MAIN_DIR + test); FilenameFilter
The following java code prints Hello World on my co-worker's computer: public void testJDBC()
Following code iterates through many data-rows, calcs some score per row and then sorts
following code doesn't work with input: 2 7 add Elly 0888424242 add Elly 0883666666

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.