Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 47
Default variants and freeing bstr memory (C++)

I am having a problem with Excel 2003 running out of "memory
handles" on my Windows 7 x64 box after creating several hundred
excel spreadsheets. I am explicitly releasing all of my Dispatch
pointers. Do I need to explicitly release my variants after I
store a bstr in them ? For instance, the following code converts
a cell range string into a bstr and gets excel to return a Dispatch
pointer for putting stuff into that range.

VARIANT range;
VariantInit ( & range);
range.vt = VT_BSTR;
_bstr_t address = _bstr_t (cellAddress.c_str ());
range.bstrVal = address;
std::string errorMsg = "Select a range in the active spreadsheet, " + cellAddress +
" (PutStringInServer)";
OLEMethod (DISPATCH_PROPERTYGET, & result1, pExcelSheet, L"Range", errorMsg, 1, range);
if (result1.vt == VT_DISPATCH)
{
....

So, do I need to release the range variant variable ?

Thanks,
Lynn
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 47
Default variants and freeing bstr memory (C++)

On 11/30/2010 2:40 PM, Lynn McGuire wrote:
I am having a problem with Excel 2003 running out of "memory
handles" on my Windows 7 x64 box after creating several hundred
excel spreadsheets. I am explicitly releasing all of my Dispatch
pointers. Do I need to explicitly release my variants after I
store a bstr in them ? For instance, the following code converts
a cell range string into a bstr and gets excel to return a Dispatch
pointer for putting stuff into that range.

VARIANT range;
VariantInit ( & range);
range.vt = VT_BSTR;
_bstr_t address = _bstr_t (cellAddress.c_str ());
range.bstrVal = address;
std::string errorMsg = "Select a range in the active spreadsheet, " + cellAddress +
" (PutStringInServer)";
OLEMethod (DISPATCH_PROPERTYGET, & result1, pExcelSheet, L"Range", errorMsg, 1, range);
if (result1.vt == VT_DISPATCH)
{
...

So, do I need to release the range variant variable ?

Thanks,
Lynn


I've been looking at VariantClear
http://msdn.microsoft.com/en-us/library/ms221165.aspx
and it appears that VariantClear should always be called for
any variant containing a dispatch pointer or a bstr pointer.

Lynn
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 47
Default variants and freeing bstr memory (C++)

On 11/30/2010 5:42 PM, Lynn McGuire wrote:
On 11/30/2010 2:40 PM, Lynn McGuire wrote:
I am having a problem with Excel 2003 running out of "memory
handles" on my Windows 7 x64 box after creating several hundred
excel spreadsheets. I am explicitly releasing all of my Dispatch
pointers. Do I need to explicitly release my variants after I
store a bstr in them ? For instance, the following code converts
a cell range string into a bstr and gets excel to return a Dispatch
pointer for putting stuff into that range.

VARIANT range;
VariantInit ( & range);
range.vt = VT_BSTR;
_bstr_t address = _bstr_t (cellAddress.c_str ());
range.bstrVal = address;
std::string errorMsg = "Select a range in the active spreadsheet, " + cellAddress +
" (PutStringInServer)";
OLEMethod (DISPATCH_PROPERTYGET, & result1, pExcelSheet, L"Range", errorMsg, 1, range);
if (result1.vt == VT_DISPATCH)
{
...

So, do I need to release the range variant variable ?

Thanks,
Lynn


I've been looking at VariantClear
http://msdn.microsoft.com/en-us/library/ms221165.aspx
and it appears that VariantClear should always be called for
any variant containing a dispatch pointer or a bstr pointer.


OK, I now know that you cannot VariantClear any variant that
you have added to a SafeArray using SafeArrayPutElement.
I thought that SafeArrayPutElement does a deep copy on the
variant containing a BStr being added to SafeArray but I was
wrong. That is a sure way to lose your string headed to Excel.

Lynn

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
if I create a BSTR in C++, do I need to release it ? Lynn McGuire[_2_] Excel Programming 4 July 29th 10 04:51 PM
BSTR to char* Help! Ai Excel Programming 5 July 7th 10 02:41 PM
I don't like variants but... brzak Excel Programming 6 October 10th 08 06:37 PM
pressing the "reset" button and freeing memory Elie W Excel Programming 1 February 12th 04 07:44 PM
Freeing memory problem filippo Excel Programming 0 October 9th 03 10:06 AM


All times are GMT +1. The time now is 06:42 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"