troubleshooting Question

COleSafeArray and cleanup thereof

Avatar of AndyAinscow
AndyAinscowFlag for Switzerland asked on
System Programming
3 Comments1 Solution594 ViewsLast Modified:
I am using a COleSafeArray to transfer large quantities of data to an Excel sheet.  I have coded a class to isolate the handling of Excel from the class I use it in (it will be used in more than one place ultimately)
Some code snippets.

in header
      COleSafeArray m_sa;

in .cpp
//initialise safe array
      DWORD dwElements[2];
      dwElements[0]= iRows;    //Number of rows
      dwElements[1]= iCols;          //Number of columns
      m_sa.Create(VT_VARIANT, 2, dwElements);


//add a long to the safe array
      long index[2];
      index[0] = iRow;
      index[1] = iCol;

      COleVariant v((long)lItem);
      m_sa.PutElement(index, v);

//OR a string      LPCTSTR pszItem
      long index[2];
      index[0] = iRow;
      index[1] = iCol;

      COleVariant v(pszItem);
      m_sa.PutElement(index, v);


//put the safe array to the sheet
      m_pSheet->Range["A1:Z1"]->Value = m_sa;
//refill safe array with values for next row
      m_pSheet->Range["A2:Z2"]->Value = m_sa;
and so on (there could be tens of thousands of rows in reality).  The safe array is being reused, by that I mean I will call the PutElement with the same index many times.  

So the question is: Do I need to cleanup the variants I put into the safearray before I put the next one into the array and if so how should the above code be modified?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros