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

  • SEARCH
  • Home
  • 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 8138875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:38:34+00:00 2026-06-06T11:38:34+00:00

I find it hard to find clear examples that would explain how to read

  • 0

I find it hard to find clear examples that would explain how to read a COleSafeArray…

So I have an object that returns through a member function a _variant_t that is actually a COleSafeArray . I want to read it’s element and make sure I’m not leaking memory…

Here is a kind of sample code. I am just trying to read the ifrst element of the array which I assume to be a long. There’s more data in the array.

class ExampleObject
{
  _variant_t GetArray();
};

//...
long Read(ExmapleObject* ptr)
{
  COleSafeArray the_array = ptr->GetArray();


  VARIANT value_temp;
  VariantInit(&value_temp);

  long index = 0;
  the_array.GetElement(&index, &value_temp);
  long my_result = value_temp.lVal;

  return my_result;
}

Is there anything wrong in this code that may generate memory leaks ?

  • 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-06T11:38:36+00:00Added an answer on June 6, 2026 at 11:38 am

    Efficient read of COleSafeArray contents is about reliability and performance.

    To read reliably, avoid junk and leaking, you should check types of the array itself and the elements. The .vt field tells you the type of array, which might be array of specific [fixed] type, or array of VARIANT, which in turn might embed sub-arrays.

    You have options to get individual elements using GetElement, in which case if the data element is a string, object, or variant, the function copies the element in the correct way, and hence you are responsible for clearing the copy. To have it done for you by a wrapper class, get your elements into CComVariant class variable, as opposed to VARIANT struct (~CComVariant will clean things up for you).

    Or, otherwise having type checked you can Lock/Unlock array and obtain direct access to elements managed by the array. You might prefer this method for performance because you lock once and copy if needed, as opposed to having locks and copies per element in previous access option.

    ~COleSafeArray destructor clears the elements of the array, so you don’t have to destroy/release them explicitly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code like this, and I find it a bit hard to read:
I find it hard to explain, so please request more info if you need
I find it hard to understand how UTC works. I have to do the
I would have thought that there is a lot of information out there on
It's hard to explain our situaction. We have a 3-tier application. The engine is
Does anybody have her vim setup in a way that uses hard tabs as
I have an Oracle view that uses a table that I cannot find anywhere.
While working with Java, I find it hard to position my main window in
As C# lacks support for freestanding functions, I find it hard to find a
I usually play with elisp code on my scratch buffer. I find it hard

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.