Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7054209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:35:56+00:00 2026-05-28T03:35:56+00:00

This snippet of code generate huge memory leaks. Could you help me to find

  • 0

This snippet of code generate huge memory leaks. Could you help me to find out where it happens?

This code does the following thing:

1) It gets IHTMLDocuments2 interface
2) Requests for all tags collection
3) Iterates over whole collection
4) and adds some of the tags’ data to list

IDispatch* pDisp;
pDisp = this->GetHtmlDocument();

if (pDisp != NULL ) 
{
    IHTMLDocument2* pHTMLDocument2;
    HRESULT hr;
    hr = pDisp->QueryInterface( IID_IHTMLDocument2,(void**)&pHTMLDocument2 );
    if (hr == S_OK)
    {
                    // I know that I could use IHTMLDocument3 interface to get collection by ID
                    // but it didn't worked and returned NULL on each call.
        IHTMLElementCollection* pColl = NULL;
                    // get all tags
        hr = pHTMLDocument2->get_all( &pColl );
        if (hr == S_OK && pColl != NULL)
        {
            LONG celem;
            hr = pColl->get_length( &celem );
            if ( hr == S_OK )
            {
                                    //iterate through all tags
                                    // if I iterate this block of code in cycle, it 
                                    // uses memory available upto 2GBs and then
                                    // app crashes
                for ( int i=0; i< celem; i++ )
                {                       
                    VARIANT varIndex;
                    varIndex.vt = VT_UINT;
                    varIndex.lVal = i;
                    VARIANT var2;
                    VariantInit( &var2 );
                    IDispatch* pElemDisp = NULL;
                    hr = pColl->item( varIndex, var2, &pElemDisp );
                    if ( hr == S_OK && pElemDisp != NULL)
                    {
                        IHTMLElement* pElem;
                        hr = pElemDisp->QueryInterface(IID_IHTMLElement,(void **)&pElem);
                        if ( hr == S_OK)
                        {                   
                                                            // check INPUT tags only
                            BSTR tagNameStr = L"";
                            pElem->get_tagName(&tagNameStr);
                            CString tagname(tagNameStr);
                            SysFreeString(tagNameStr);
                            tagname.MakeLower();
                            if (tagname != "input")
                            {
                                continue;
                            }
                                                            //get ID attribute
                            BSTR bstr = L"";
                            pElem->get_id(&bstr);
                            CString idStr(bstr);
                            SysFreeString(bstr);

                            if (RequiredTag(pElem)) 
                            {       
                                AddTagToList(pElem);
                            }
                                                            //release all objects
                            pElem->Release();
                        }
                        pElemDisp->Release();
                    }
                }
            }
                            // I looked over this code snippet many times and couldn't find what I'm missing here...
            pColl->Release();
        }
        pHTMLDocument2->Release();
    }
    pDisp->Release();       
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:35:56+00:00Added an answer on May 28, 2026 at 3:35 am

    Inside your loop, for each retreived element that does not have a tagname of "input" (which will be most elements), you are not calling pElem->Release() when calling continue, so you are leaking them:

    if (tagname != "input") 
    { 
        pElem->Release(); // <-- add this
        continue; 
    } 
    

    With that said, you should re-write your code to use ATL’s smart pointer classes (CComPtr, CComQIPtr, CComBSTR, etc) to manage the memory for you so you do not have to manually release everything yourself anymore, eg:

    CComPtr<IDispatch> pDisp;
    pDisp.Attach(this->GetHtmlDocument());
    if (pDisp.p != NULL)    
    {   
        CComQIPtr<IHTMLDocument2> pHTMLDocument2(pDisp);   
        if (pHTMLDocument2.p != NULL)   
        {   
            CComPtr<IHTMLElementCollection> pColl;   
            pHTMLDocument2->get_all(&pColl);
            if (pColl.p != NULL)
            {   
                LONG celem;   
                if (SUCCEEDED(pColl->get_length(&celem)))   
                {   
                    for (LONG i = 0; i < celem; ++i)   
                    {                          
                        VARIANT varIndex;   
                        varIndex.vt = VT_UINT;   
                        varIndex.lVal = i;   
                        VARIANT var2;   
                        VariantInit( &var2 );   
                        CComPtr<IDispatch> pElemDisp;   
                        pColl->item( varIndex, var2, &pElemDisp );   
                        if (pElemDisp.p != NULL)   
                        {   
                            CComQIPtr<IHTMLElement> pElem(pElemDisp);   
                            if (pElem.p != NULL)   
                            {                      
                                CComBSTR tagNameStr;   
                                pElem->get_tagName(&tagNameStr);   
    
                                if (lstrcmpiW(tagNameStr.m_str, L"input") != 0)
                                    continue;   
    
                                CComBSTR idStr;   
                                pElem->get_id(&idStr);   
    
                                if (RequiredTag(pElem))    
                                    AddTagToList(pElem);   
                            }   
                        }   
                    }   
                }   
            }   
        }   
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This following code snippet works fine using jQuery1.2.3, but it doesn’t work with latest
I'm trying to generate excel using c# following is the code snippet for it
this is the snippet of my code in which i am trying to generate
This snippet of code always parses the date into the current timezone, and not
Consider this snippet of code: public static class MatchCollectionExtensions { public static IEnumerable<T> AsEnumerable<T>(this
I came across this snippet of code on MSDN: entityBuilder.Metadata = @res://*/AdventureWorksModel.csdl| res://*/AdventureWorksModel.ssdl| res://*/AdventureWorksModel.msl;
I wrote this snippet of code and I assume len is tail-recursive, but a
Edit: I put this snippet of code in jsbin: http://jsbin.com/eneru I am trying to
I've found this snippet of code on the net. It sets up an NSMutableArray
I have this snippet of code private Templates retrieveFromCache(String name) { TemplatesWrapper t =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.