Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Memory leak coming from COleSafeArray object

Memory leak coming from COleSafeArray object

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structurestestingbeta-testingperformance
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lastgen
    wrote on last edited by
    #1

    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!!!

    L 1 Reply Last reply
    0
    • L lastgen

      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!!!

      L Offline
      L Offline
      lastgen
      wrote on last edited by
      #2

      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!!!

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups