Memory leak coming from COleSafeArray object
-
I must be missing something in the cleanup, but here is a cut version of the code that causes the leak.
// ... an Excel file has been loaded already into oSheet CExcelRange usedCells = oSheet.get_UsedRange(); // Copy used cells to array - It is this line of code that causes the leak /****************************************************/ COleSafeArray cellArray(usedCells.get_Value()); /****************************************************/ usedCells.Clear(); usedCells.ReleaseDispatch(); VariantClear(cellArray); cellArray.Clear(); cellArray.Detach(); // ...close down excel etc etc
I have deliberately cut down my code for testing so it essentially does nothing but load the file. There is no code between "COleSafeArray cellArray(usedCells.get_Value());" and "usedCells.Clear();" to help narrow down the leak. If I comment out the line "COleSafeArray cellArray(usedCells.get_Value());" (and oviously the associated cleanup so it will compile) I have no leak, but if it calls this line I get an immediate loss of ~4-5 times the file size which equates to about 30 meg per file. If I run this multiple times it continues to drop, so I don't think it is a windows caching problem. Anybody know what I'm missing? When I die I'd like to go peacefully in my sleep like my father, not screaming in terror like his passengers!!! -
I must be missing something in the cleanup, but here is a cut version of the code that causes the leak.
// ... an Excel file has been loaded already into oSheet CExcelRange usedCells = oSheet.get_UsedRange(); // Copy used cells to array - It is this line of code that causes the leak /****************************************************/ COleSafeArray cellArray(usedCells.get_Value()); /****************************************************/ usedCells.Clear(); usedCells.ReleaseDispatch(); VariantClear(cellArray); cellArray.Clear(); cellArray.Detach(); // ...close down excel etc etc
I have deliberately cut down my code for testing so it essentially does nothing but load the file. There is no code between "COleSafeArray cellArray(usedCells.get_Value());" and "usedCells.Clear();" to help narrow down the leak. If I comment out the line "COleSafeArray cellArray(usedCells.get_Value());" (and oviously the associated cleanup so it will compile) I have no leak, but if it calls this line I get an immediate loss of ~4-5 times the file size which equates to about 30 meg per file. If I run this multiple times it continues to drop, so I don't think it is a windows caching problem. Anybody know what I'm missing? When I die I'd like to go peacefully in my sleep like my father, not screaming in terror like his passengers!!!OK I've worked it out. It seems you cannot set the data via the constructor or it will cause a leak, don't ask me why, it just does. Otherwise it works fine. The fix is to replace the faulty line with these two COleSafeArray cellArray; cellArray.Attach(usedCells.get_Value()); I have also noticed with this fussing today that if a value is assigned to any form of variant based objects in C++ it will leak unless you call Clear() after every assignment. One of the many reasons why I despise variants. When I die I'd like to go peacefully in my sleep like my father, not screaming in terror like his passengers!!!