FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Antonio: "BUG" in TPrinter
Posts: 1048
Joined: Mon Oct 24, 2005 09:54 AM
Antonio: "BUG" in TPrinter
Posted: Tue Jan 21, 2014 09:55 AM

Antonio, i am using a printerobject with the direct pdf-output. Now i see, that the emf-files in the temp-directory are still present and should be deleted! If i will manually or programatically delete this files, i become a "Access Violation".
...and the temp-directory grows and grows...
A second question: After ENDPRINT the printobject shows with valtype(oPrint) still "O"???

Regards,
Günther
---------------------------------
office@byte-one.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Antonio: "BUG" in TPrinter
Posted: Tue Jan 21, 2014 02:06 PM

Gunther,

  1. the emf-files in the temp-directory are still present

Where is located your temp-directory ? What files are there ? Please show a test and the names of the files that you get. If you cant delete them it means that they are open (in use)

  1. END... does not mean that the object is turned into nil. When you go out of the scope where it is defined, the garbage collector will do its work and will finally free it.
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1048
Joined: Mon Oct 24, 2005 09:54 AM
Re: Antonio: "BUG" in TPrinter
Posted: Tue Jan 21, 2014 05:04 PM
  1. Antonio, when using normal Tprinter-object the EMFs are deleted! Only when i use the direct pdf-creation with clausula ..FILE "xyz.pdf" the files in Format z.b. "234571.emf" are not erased! The temp-directory are in normal windows location.
  2. END.. Ok, i understand!!
Regards,
Günther
---------------------------------
office@byte-one.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Antonio: "BUG" in TPrinter
Posted: Tue Jan 21, 2014 10:47 PM

Gunter,

Are you calling ENDPRINT after sending the print to the PDF file ?

Class TPrinter Method End() is responsible for deleting the metafiles.

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1048
Joined: Mon Oct 24, 2005 09:54 AM
Re: Antonio: "BUG" in TPrinter
Posted: Wed Jan 22, 2014 08:10 AM
Code (fw): Select all Collapse
function PrintEnd()
   if oPrinter:lMeta
      if Empty( oPrinter:cFile )
         oPrinter:Preview()
      elseif "pdf" $ Lower( oPrinter:cFile )
         FWSavePreviewToPDF( oPrinter, If( Lower( cFileExt( oPrinter:cFile ) ) == "pdf", ;
               oPrinter:cFile, nil ), oPrinter:lPreview )
      endif
   else
      oPrinter:End()
   endif
   oPrinter := nil
return nil


Antonio, you see, that end() only calling on normal prints! On preview, the preview itself deletes the EMFs, on FWSavePreviewToPDF(..) nobody deletes this files!
Should end() called in all cases?
Regards,
Günther
---------------------------------
office@byte-one.com
Posts: 1048
Joined: Mon Oct 24, 2005 09:54 AM
Re: Antonio: "BUG" in TPrinter
Posted: Wed Jan 22, 2014 08:49 AM
Antonio, I found a solution:

1. In Emf2Jpeg() instead of CloseEnhMetafile() using DeleteEnhMetafile()! In this function should also inserted a sysrefresh(). Then i can produce hundreds of pages without Problems!
2. In FWSavePreviwToPDF() before returning oDevice:end() BUT only, when not called from TPreview as the preview deletes the EMFs!!
Regards,
Günther
---------------------------------
office@byte-one.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Antonio: "BUG" in TPrinter
Posted: Wed Jan 22, 2014 09:02 AM

Gunther,

I guess you can also do:

ENDPRINT
oPrinter:End()

anyhow, please post here your exact code changes so we can review them and include them, many thanks

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1048
Joined: Mon Oct 24, 2005 09:54 AM
Re: Antonio: "BUG" in TPrinter
Posted: Thu Jan 23, 2014 09:13 AM
This is my code, I use. In poor Computers with only 1Gb memory it can bee, the program terminates without any message! Possibly from the FI functions with Memory?

Code (fw): Select all Collapse
//----------------------------------------------------------------------------//

function FWSavePreviewToPDF( oDevice, cPDF, lOpen, nQual )   // oDevice can be oPrinter or oPreview

   local cOrient, oPDF
   local hWnd , lPreview

   DEFAULT nQual := 0 , lPreview := .F.

   if oDevice:IsKindOf( "TPREVIEW" )
      hWnd     := oDevice:oWnd:hWnd
      oDevice  := oDevice:oDevice
      lPreview := .T.
   endif

   if LoadFreeImage() <= 32
      MsgAlert( "freeimage.dll not found" )
      return nil
   endif

   DEFAULT cPDF   := cGetFile( "PDF File (*.pdf)|*.pdf|", "Select PDF File to Save", ;
                               CurDir(), .t. )

   if ! Empty( cPDF )
      cPDF = cFileSetExt( cPDF, "pdf" )
      CursorWait()
      cOrient = If( oDevice:nHorzSize() > oDevice:nVertSize(), 'L', 'P' )
      oPdf = fwPdf():New( cPdf, cOrient, nQual )
      AEval( oDevice:aMeta, { | cMeta |( oPdf:AddMeta( cMeta )) } )
      oPdf:Close()
      CursorArrow()


      DEFAULT lOpen := MsgYesNo( "View " + cPDF + " (Y/N)?" )

      if lOpen
         ShellExecute( IfNil( hWnd, GetWndDefault() ), "open", cPDF )
      endif
   else
      cPDF  := nil
   endif

  if(!lPreview,oDevice:End(),)      //BYTE-ONE 2014

return cPDF

//----------------------------------------------------------------------------//

static function Emf2Jpeg( cEMF , nQual )

   local cJpeg    := cFileSetExt( cEMF, "jpg" )
   local hDC1, hDC, hDib, hDib2, hMem, hBmp, hOldBmp, hEMF
   local cBuf, oRect, lRet := .f.
   local nFormat

   cBuf         := MemoRead( cEMF )
   oRect        := TRect():New( 0, 0, Bin2L( SubStr( cBuf, 21, 4 ) ) / 2, ;
                                  Bin2L( SubStr( cBuf, 17, 4 ) ) / 2  )
   cBuf        := nil  // to release memory
   HB_GCAll(.T.)
   hEMF        := GetEnhMetaFile( cEMF )
   hDC1        := GetDC( GetDesktopWindow() )
   hDC         := CreateCompatibleDC( hDC1 )
   hBmp        := CreateCompatibleBitmap( hDC1, oRect:nWidth, oRect:nHeight )
   hOldBmp     := SelectObject( hDC, hBmp )
   FillRect( hDC, oRect:aRect, GetStockObject( 0 ) )
   PlayEMF( hDC, hEMF, oRect:cRect )
   DeleteEnhMetafile( hEMF )        //BYTE-ONE 2014
   hDib        := DibFromBitmap( hBmp )
   cBuf        := DibToStr( hDib )
   GloBalFree( hDib )
   SelectObject( hDC, hOldBmp )
   DeleteDC( hDC )
   DeleteDC( hDC1 )
   DeleteObject( hBmp )
   hMem        := FI_OpenMemory( cBuf, Len( cBuf ) )
   nFormat     := FI_GetFileTypeFromMemory( hMem, 0 )
   hDib        := FI_LoadFromMemory( nFormat, hMem, 0 )
   FI_CloseMemory( hMem )
   cBuf        := nil  // to release memory
   HB_GCAll(.T.)
   hDib2       := FICnv24( hDib )
   FIUnload( hDib )
   lRet        := FISave( 2, hDib2, cJpeg, nQual )
   FIUnload( hDib2 )
   cBuf        := If( lRet, MemoRead( cJpeg ), "" )
   FErase( cJpeg )
   sysrefresh()     //BYTE-ONE 2014

return cBuf

//----------------------------------------------------------------------------//
Regards,
Günther
---------------------------------
office@byte-one.com
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Antonio: &quot;BUG&quot; in TPrinter
Posted: Tue Jan 28, 2014 05:20 PM

Have you checked for memory leaks?

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 1048
Joined: Mon Oct 24, 2005 09:54 AM
Re: Antonio: &quot;BUG&quot; in TPrinter
Posted: Tue Jan 28, 2014 07:20 PM

Yes James, no leaks. Maybe the memory-functions from "freeimage" are responsible for this fault!?
We should change to file-functions, but i am not so versed with freeimage.

Regards,
Günther
---------------------------------
office@byte-one.com
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Antonio: &quot;BUG&quot; in TPrinter
Posted: Tue Jan 28, 2014 07:56 PM

Günther,

Perhaps you need to use a general purpose memory leak detector program instead of the Fivewin functions. This might find memory leaks in freeimage.

James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Antonio: &quot;BUG&quot; in TPrinter
Posted: Tue Jan 28, 2014 08:37 PM
Maybe this old message will help.

James

From: Patrick Mast <patrick@winfakt.com>
Subject: Re: FW Win 2000
Date: Friday, January 05, 2001 2:08 AM

Uwe,

> Big Prob my Fw App crashes with a Gpf under Win 2000
> Any Tip for me ??
My app works great in Win2000.

Have you tried this:

Code (fw): Select all Collapse
// In the begin of your app:
InterruptRegister( { || ControlGPF() } )  // Setting our own GPF controller

//-----------
function ControlGPF() //SOCS
   CLOSE DATA
   MsgAlert( "A Windows GPF occured...")
   InterruptUnRegister()   // We release our GPF controller
   Eval( ErrorBlock(), _FWGenError( 5, "Undetermined GPF" ) )
Return nil
//---------

This way maybe you can see what line causes the GPF.

--
Regards,

Patrick Mast
http://www.Patrick.Be.kz
http://www.FiveWin.Be.kz
http://www.Harbour-Project.org
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Antonio: &quot;BUG&quot; in TPrinter
Posted: Mon Feb 03, 2014 02:54 PM
Mr Gunther and Mr Antonio

I suggest modification of printend() function and no other changes anywhere else.

Proposed New function
Code (fw): Select all Collapse
function PrintEnd()

   if oPrinter:lMeta
      if Empty( oPrinter:cFile )
         oPrinter:Preview()
      elseif "pdf" $ Lower( oPrinter:cFile )
         FWSavePreviewToPDF( oPrinter, oPrinter:cFile, oPrinter:lPreview )
         oPrinter:End()
      endif
   else
      oPrinter:End()
   endif

   oPrinter := nil

return nil

Do you think this solves the issue?
Regards



G. N. Rao.

Hyderabad, India
Posts: 1048
Joined: Mon Oct 24, 2005 09:54 AM
Re: Antonio: &quot;BUG&quot; in TPrinter
Posted: Mon Feb 03, 2014 09:41 PM

OK, but usíng CloseEnhMetaFile() frees not the emf-files to let them erasing!
We must use DeleteEnhMetafile( hEMF ) in the function Emf2Jpeg(...)!
Another negative issue from this function is the dependency from the resolution of the Printer-Driver. It should in all cases
the same!
I suggest further the option to use automatically (if available) a PDF-Printer-Driver (with a special name?).

Regards,
Günther
---------------------------------
office@byte-one.com
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Antonio: &quot;BUG&quot; in TPrinter
Posted: Sat Feb 15, 2014 06:33 AM

Implemented.

Regards



G. N. Rao.

Hyderabad, India

Continue the discussion