Forums

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF Merger for COM/ActiveX (v7.x)  /  Memory leak

Forum Thread - Memory leak

 Mar 25 2015 2:45 PM
Dear support. The following fragment of code is adding images to pdf.
Unfortunely it gives the memory leak.
With large files (over 20 Mb for each DPDFLogo.png, DPDFLogo2.png etc) and generating multiple PDFs this leak could be clearly visible from Task Manager memory usage.

        

        VARIANT var1 = AsciiTOVARIANT(_T("DPDFLogo.png"));

        {
                //Add an image with a scale of 100%        
                objPage->AddImage(var1, 0, 200, 100, &objImage);
                objImage->Release();
        }

        VARIANT var2 = AsciiTOVARIANT(_T("DPDFLogo2.png"));

        {
                //Add an image with a scale of 100%        
                objPage->AddImage(var2, 0, 201, 101, &objImage2);
                objImage2->Release();
        }

        VARIANT var3 = AsciiTOVARIANT(_T("DPDFLogo3.png"));

        {
                //Add an image with a scale of 100%        
                objPage->AddImage(var3, 0, 201, 101, &objImage3);
                objImage3->Release();
        }

        VARIANT var4 = AsciiTOVARIANT(_T("DPDFLogo4.png"));

        {
                //Add an image with a scale of 100%        
                objPage->AddImage(var4, 0, 201, 101, &objImage4);
                objImage4->Release();
        }
 Mar 25 2015 4:58 PM
Posted by a ceTe Software moderator
Hello,

Please make sure you are using the latest build v7.0.4 (Build 27594) available and it can be downloaded from the customer area.

If you still see this issue with the latest build, then please email us support@cete.com a sample project to reproduce this behavior so we can look into it further.

Thanks,
ceTe Software Support Team.

 Mar 26 2015 6:10 AM
Tested on 7.0.4
It has the same problem.
Sent the sample project

Regards,
Vadim Gorenko
 Jun 02 2015 1:10 PM
Posted by a ceTe Software moderator
Hello,

Below is the code that shows how to create and release DynamicPDF objects (including image objects) correctly to avoid memory leaks.

        ::CoInitialize(NULL);

        for(int i = 0; i<25; i++)
        {
                Sleep(3000);

                CComPtr<DynamicPDF::IDocument> objDoc;
                CComPtr<DynamicPDF::IPage> objPage;
                CComPtr<DynamicPDF::IImage> objImage1;
                CComPtr<DynamicPDF::IImage> objImage2;
                CComPtr<DynamicPDF::IImage> objImage3;
                
                VARIANT varImage;

                HRESULT hr=::CoCreateInstance(CLSID_Document,NULL,CLSCTX_INPROC_SERVER,IID_IDocument,(void**)&objDoc);
                if(hr != S_OK)
                        return 0;
                
                objDoc->AddPage(&objPage);
                
                if(objPage== NULL)
                        goto Cleanup;

                WCHAR lpCurDir[512];
                WCHAR lpFilePath[512];
                ::GetCurrentDirectoryW(512,lpCurDir);
                wsprintf(lpFilePath,L"GeneratedOutput_%d.pdf",i);                

                VariantInit(&varImage);
                varImage.vt = VT_BSTR;
                varImage.bstrVal = SysAllocString(TEXT("DPDFlogo.png"));
                objPage->AddImage(varImage, 0, 200, 100, &objImage1);
                VariantClear(&varImage);

                VariantInit(&varImage);
                varImage.vt = VT_BSTR;
                varImage.bstrVal = SysAllocString(TEXT("DPDFlogo2.png"));
                objPage->AddImage(varImage, 0, 200, 100, &objImage2);
                VariantClear(&varImage);

                VariantInit(&varImage);
                varImage.vt = VT_BSTR;
                varImage.bstrVal = SysAllocString(TEXT("DPDFlogo3.png"));
                objPage->AddImage(varImage, 0, 200, 100, &objImage3);
                VariantClear(&varImage);


                BSTR FilePath= SysAllocString(lpFilePath);
                hr = objDoc->DrawToFile(FilePath);
                if(hr != S_OK)
                        goto Cleanup;
                
        Cleanup:
                if(objImage1 != NULL)
                {
                        objImage1.Release();
                        objImage1 = NULL;
                }
                if(objImage2 != NULL)
                {
                        objImage2.Release();
                        objImage2 = NULL;
                }
                if(objImage3 != NULL)
                {
                        objImage3.Release();
                        objImage3 = NULL;
                }                

                if(FilePath!= NULL)
                {
                        SysFreeString(FilePath);
                        FilePath = NULL;
                }
                
                if(objPage != NULL)
                {
                        objPage.Release();
                        objPage = NULL;
                }
                if(objDoc!= NULL)
                {
                        objDoc.Release();
                        objDoc = NULL;
                }
        }
        ::CoUninitialize();
        
Thanks,
ceTe Software Support Team.

All times are US Eastern Standard time. The time now is 6:52 PM.