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. Does this code leaks memory?

Does this code leaks memory?

Scheduled Pinned Locked Moved C / C++ / MFC
announcementc++htmlperformancehelp
6 Posts 4 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.
  • J Offline
    J Offline
    Joan M
    wrote on last edited by
    #1

    Hello, I'm debugging my last program... and today I've acquired Rational Purify (trial version) in order to receive help on this task... after running my program under Rational Purify, I've found some memory leaks... but now I'm not sure of which is my name (almost)... let's see, it says that this code is leaking memory: ------------------------------------------------------------- ------------------------------------------------------------- // Variables que permeten la construcció del codi HTML. CString csCodiHTML = "HTML STRING..."; BSTR bstr; int iWidthAreaClientHTMLCtrl = 0; // Tractament del SafeArray. HRESULT hrTractarAmbSafeArray = S_OK; SAFEARRAY *pSafeArray = NULL; VARIANT *pElement = NULL; extern CTCCApp TCCApp; //------------------------------------------------ // Obtenir accés al control HTML via ATL. //------------------------------------------------ CComQIPtr spHTMLDocument = this->m_HTMLCtrl.GetHtmlDocument(); if(spHTMLDocument) { CComQIPtr spHTML; spHTMLDocument->get_body(&spHTML); if (spHTML) { bstr = csCodiHTML.AllocSysString(); spHTML->put_innerHTML(bstr); spHTML.Release(); SysFreeString(bstr); csCodiHTML.ReleaseBuffer(); csCodiHTML.FreeExtra(); } else { pSafeArray = ::SafeArrayCreateVector(VT_VARIANT, 0, 1); if (pSafeArray) { hrTractarAmbSafeArray = ::SafeArrayAccessData(pSafeArray,(LPVOID*) &pElement); pElement->vt = VT_BSTR; // HERE! pElement->bstrVal = csCodiHTML.AllocSysString(); hrTractarAmbSafeArray = ::SafeArrayUnaccessData(pSafeArray); hrTractarAmbSafeArray = spHTMLDocument->write(pSafeArray); ::SafeArrayDestroy(pSafeArray); csCodiHTML.ReleaseBuffer(); // HERE TOO! csCodiHTML.FreeExtra(); } } } if (spHTMLDocument) spHTMLDocument.Release(); ------------------------------------------------------------- ------------------------------------------------------------- Where you can see HERE! and HERE TOO! is where that program Rational purify is detecting some memory leaks... do you think that the code is leaking memory? and if so... why? I thought that if I have not called new I should'nt call delete and that whenever the function goes out of scope the local variables are destructed automatically... isn't it true? Thank you in advance...

    https://www.robotecnik.com freelance robots, PLC and CNC programmer.

    T R 2 Replies Last reply
    0
    • J Joan M

      Hello, I'm debugging my last program... and today I've acquired Rational Purify (trial version) in order to receive help on this task... after running my program under Rational Purify, I've found some memory leaks... but now I'm not sure of which is my name (almost)... let's see, it says that this code is leaking memory: ------------------------------------------------------------- ------------------------------------------------------------- // Variables que permeten la construcció del codi HTML. CString csCodiHTML = "HTML STRING..."; BSTR bstr; int iWidthAreaClientHTMLCtrl = 0; // Tractament del SafeArray. HRESULT hrTractarAmbSafeArray = S_OK; SAFEARRAY *pSafeArray = NULL; VARIANT *pElement = NULL; extern CTCCApp TCCApp; //------------------------------------------------ // Obtenir accés al control HTML via ATL. //------------------------------------------------ CComQIPtr spHTMLDocument = this->m_HTMLCtrl.GetHtmlDocument(); if(spHTMLDocument) { CComQIPtr spHTML; spHTMLDocument->get_body(&spHTML); if (spHTML) { bstr = csCodiHTML.AllocSysString(); spHTML->put_innerHTML(bstr); spHTML.Release(); SysFreeString(bstr); csCodiHTML.ReleaseBuffer(); csCodiHTML.FreeExtra(); } else { pSafeArray = ::SafeArrayCreateVector(VT_VARIANT, 0, 1); if (pSafeArray) { hrTractarAmbSafeArray = ::SafeArrayAccessData(pSafeArray,(LPVOID*) &pElement); pElement->vt = VT_BSTR; // HERE! pElement->bstrVal = csCodiHTML.AllocSysString(); hrTractarAmbSafeArray = ::SafeArrayUnaccessData(pSafeArray); hrTractarAmbSafeArray = spHTMLDocument->write(pSafeArray); ::SafeArrayDestroy(pSafeArray); csCodiHTML.ReleaseBuffer(); // HERE TOO! csCodiHTML.FreeExtra(); } } } if (spHTMLDocument) spHTMLDocument.Release(); ------------------------------------------------------------- ------------------------------------------------------------- Where you can see HERE! and HERE TOO! is where that program Rational purify is detecting some memory leaks... do you think that the code is leaking memory? and if so... why? I thought that if I have not called new I should'nt call delete and that whenever the function goes out of scope the local variables are destructed automatically... isn't it true? Thank you in advance...

      T Offline
      T Offline
      Tim Smith
      wrote on last edited by
      #2

      You are missing a call to SafeArrayUnaccessData. Tim Smith I'm going to patent thought. I have yet to see any prior art.

      D T 2 Replies Last reply
      0
      • T Tim Smith

        You are missing a call to SafeArrayUnaccessData. Tim Smith I'm going to patent thought. I have yet to see any prior art.

        D Offline
        D Offline
        Daniel Turini
        wrote on last edited by
        #3

        "Resource acquisition is initialization" I love it. ;)


        It's not the fall that kills you: it's the sudden stop - Down by Law, Jim Jamursch (1986)

        J 1 Reply Last reply
        0
        • T Tim Smith

          You are missing a call to SafeArrayUnaccessData. Tim Smith I'm going to patent thought. I have yet to see any prior art.

          T Offline
          T Offline
          Tim Smith
          wrote on last edited by
          #4

          Actually, it is in the code. I must have missed it first time around. *shrug* Tim Smith I'm going to patent thought. I have yet to see any prior art.

          1 Reply Last reply
          0
          • D Daniel Turini

            "Resource acquisition is initialization" I love it. ;)


            It's not the fall that kills you: it's the sudden stop - Down by Law, Jim Jamursch (1986)

            J Offline
            J Offline
            Joan M
            wrote on last edited by
            #5

            What do you mean?

            https://www.robotecnik.com freelance robots, PLC and CNC programmer.

            1 Reply Last reply
            0
            • J Joan M

              Hello, I'm debugging my last program... and today I've acquired Rational Purify (trial version) in order to receive help on this task... after running my program under Rational Purify, I've found some memory leaks... but now I'm not sure of which is my name (almost)... let's see, it says that this code is leaking memory: ------------------------------------------------------------- ------------------------------------------------------------- // Variables que permeten la construcció del codi HTML. CString csCodiHTML = "HTML STRING..."; BSTR bstr; int iWidthAreaClientHTMLCtrl = 0; // Tractament del SafeArray. HRESULT hrTractarAmbSafeArray = S_OK; SAFEARRAY *pSafeArray = NULL; VARIANT *pElement = NULL; extern CTCCApp TCCApp; //------------------------------------------------ // Obtenir accés al control HTML via ATL. //------------------------------------------------ CComQIPtr spHTMLDocument = this->m_HTMLCtrl.GetHtmlDocument(); if(spHTMLDocument) { CComQIPtr spHTML; spHTMLDocument->get_body(&spHTML); if (spHTML) { bstr = csCodiHTML.AllocSysString(); spHTML->put_innerHTML(bstr); spHTML.Release(); SysFreeString(bstr); csCodiHTML.ReleaseBuffer(); csCodiHTML.FreeExtra(); } else { pSafeArray = ::SafeArrayCreateVector(VT_VARIANT, 0, 1); if (pSafeArray) { hrTractarAmbSafeArray = ::SafeArrayAccessData(pSafeArray,(LPVOID*) &pElement); pElement->vt = VT_BSTR; // HERE! pElement->bstrVal = csCodiHTML.AllocSysString(); hrTractarAmbSafeArray = ::SafeArrayUnaccessData(pSafeArray); hrTractarAmbSafeArray = spHTMLDocument->write(pSafeArray); ::SafeArrayDestroy(pSafeArray); csCodiHTML.ReleaseBuffer(); // HERE TOO! csCodiHTML.FreeExtra(); } } } if (spHTMLDocument) spHTMLDocument.Release(); ------------------------------------------------------------- ------------------------------------------------------------- Where you can see HERE! and HERE TOO! is where that program Rational purify is detecting some memory leaks... do you think that the code is leaking memory? and if so... why? I thought that if I have not called new I should'nt call delete and that whenever the function goes out of scope the local variables are destructed automatically... isn't it true? Thank you in advance...

              R Offline
              R Offline
              Rob Groves
              wrote on last edited by
              #6

              I don't think there are leaks in your code. It could be that the complaint is about the line below //HERE, as your tool has no way of knowing that SafeArrayDestroy() will delete the BSTRs in the array. Also, in my opinion, your calls to ReleaseBuffer() and FreeExtra() are not necessary. Cheers, Rob.

              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