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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Does this ATL Code Leak memory?

Does this ATL Code Leak memory?

Scheduled Pinned Locked Moved ATL / WTL / STL
c++performancequestion
2 Posts 2 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.
  • K Offline
    K Offline
    Killer3
    wrote on last edited by
    #1

    STDMETHODIMP CTryoutATL::ConcatenateString(long count, BSTR *ReturnVal) { // TODO: Add your implementation code here BSTR outputTempString; BSTR outputString = new OLECHAR[30]; BSTR tempString = new OLECHAR[30]; swprintf(tempString,L"The KILLER "); BOOL bFirstTime = TRUE; for(long i =1; i < count ; i++) { BSTR tempString = new OLECHAR[30]; swprintf(tempString,L"This Ilanthendral kills %ld\n",i); outputTempString = new OLECHAR[30*i]; if(!bFirstTime) { wcscpy(outputTempString,outputString); wcscat(outputTempString,tempString); } else wcscpy(outputTempString,tempString); bFirstTime = FALSE; delete [] outputString; delete [] tempString; outputString = outputTempString; } *ReturnVal = outputTempString; return S_OK; }

    for thendral

    S 1 Reply Last reply
    0
    • K Killer3

      STDMETHODIMP CTryoutATL::ConcatenateString(long count, BSTR *ReturnVal) { // TODO: Add your implementation code here BSTR outputTempString; BSTR outputString = new OLECHAR[30]; BSTR tempString = new OLECHAR[30]; swprintf(tempString,L"The KILLER "); BOOL bFirstTime = TRUE; for(long i =1; i < count ; i++) { BSTR tempString = new OLECHAR[30]; swprintf(tempString,L"This Ilanthendral kills %ld\n",i); outputTempString = new OLECHAR[30*i]; if(!bFirstTime) { wcscpy(outputTempString,outputString); wcscat(outputTempString,tempString); } else wcscpy(outputTempString,tempString); bFirstTime = FALSE; delete [] outputString; delete [] tempString; outputString = outputTempString; } *ReturnVal = outputTempString; return S_OK; }

      for thendral

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2
      1. The outer tempString will always leak one allocation.
      2. If count == 0, outputString will not be deallocated
      3. If count == 0, outputTempString will never be assigned to, so you'll return an indeterminate value

      In addition - these aren't valid BSTRs. BSTRs contain more information than just raw characters (for example, string length), so should always be allocated using SysAllocString, SysAllocStringByteLen or SysAllocStringLen and deallocated using SysFreeString. I'd suggest you read the documentation for CComBSTR or _bstr_t and then use one of them rather than BSTR. These BSTR wrappers make BSTR management much easier. BSTR management much easier.

      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