How to free memory using a COleSafeArray

Posted on 2004-11-22
Medium Priority
Last Modified: 2012-08-14
I am creating and loading a COleSafeArray form Excel

COleSafeArray saRet;
CRange oRange = oSheet.get_Range(COleVariant(csStart),COleVariant(csEnd));
saRet = oRange.get_Value(vOpt);

Everytime  I run my code the windows memory usage increases; + 30meg;
I found Q_20742007.html  but the answer does not work for me;
SetProcessWorkingSetSize(GetCurrentProcess(), 0xFFFFFFFF, 0xFFFFFFFF);
maybe because my code is a DLL addin to another application.

My Code is almost a copy of MSDN example http://support.microsoft.com/kb/308407/EN-US/#2

I have tried       saRet->DestroyData();
Nothing Helps
What to try next?

Question by:MCofer
  • 5
  • 3

Expert Comment

ID: 12649001
Have you checked your Lock count on this particular item?  Perhaps the item has been locked and not unlocked the same number of times.  In the COM world... An array cannot be deleted while it is locked.  Or if the lock count has been incremented without sufficient Unlock(...) calls.

check here:

On another note the memory usage does seem a bit large, BUT I must say I have seen this issue whenever I incorporate the Office COM libraries which is what you are doing in this particular instance.  

In the particular scenario you showed from the MS site, you would not need to reclaim the SafeArray, but you would want to make sure that you did not leave a positive lock count on the item.  Just be careful about releasing the Excel COM object when your program exits.  Otherwise you'll leave 'em in memory which is pretty ugly.


Author Comment

ID: 12649886
I'm using
long nIndex[2];
COleVariant vData;
for (int nRow = 0; nRow < nRows; nRow++)
    for (int nCol = 0; nCol < nCols; nCol++)
       nIndex[0] = nRow+1;
       nIndex[1] = nCol+1;
to acces the data according to :
Lock and Unlock are called automaticly;

trying to use either
returns an exception error.

Expert Comment

ID: 12650280
In the scenario listed above, you would not need to call an Unaccessdata().  You would only need to do that if you were calling AccessData().  Basically, leave the COLEsafearray vaiable as is.  The lock count is reset for sure if you are only using GetElement(..)
You would only need to manually aid the variable if you were doing anything that could increment the locks and unlocks.

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 12650575
If I simply run
 COleSafeArray saRet;
CRange oRange = oSheet.get_Range(COleVariant(csStart),COleVariant(csEnd));
saRet = oRange.get_Value(vOpt);
and exit (never try to access the data) the memory is not released.
the problem occurs when the COleSafeArray is loaded and is not released when the array goes out of scope
I tried using new/delete and got the same results
I can use
COleVariant vData = oRange.get_Value(vOpt);
instead of using the COleSafeArray  but looping through the entire excel sheet is very slow compared to loading the array
this is a critical problem for me so I am increassing the points to 500
Please continue helping.


Accepted Solution

danielsonchris earned 1500 total points
ID: 12651656
ahh I think I have it... do the following when done with the COleSafeArray...



after this call perhaps call the VariantClear(...) method.  (but this may not be necessary).



Author Comment

ID: 12736685
I've been a week or so geting back.
I had to get my application working without using COleSafeArray
The  detach/VariantClear looks like it should work but from the examples I do not understand how to use it.
 COleSafeArray saRet;
saRet = oRange.get_Value(vOpt);  Gets the objects in an excel range inn my case 200 columns
                                                   by 1000 rows (uses about 10 meg of memory.
Now to detach a variant array
VARIANT vData = saRet.Detach(); only returns one variant value. how doi i get and clear the
                                                  entire array??


Author Comment

ID: 12909727
All of the answers were informative but nothing solved the memory problem
Does anyone have any other Ideas or should I close the question?

Author Comment

ID: 13007899
None of the suggestions really worked but the Info was helpful
Thanks for the effort

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question