• Welcome to PlanetSquires Forums.
 

José, if you're bored...

Started by Paul Squires, August 02, 2018, 09:19:19 PM

Previous topic - Next topic

José Roca

Seems that the problem happens when compiling with 64 bit. I will check it. It works with 32 bit.

José Roca

#61
It fails with 64 bit if you use the -O switch. Without it, it works fine.

Paul Squires

Quote from: José Roca on August 09, 2018, 07:14:25 PM
It fails with 64 bit if you use the -O switch. Without it, it works fine.

That is interesting. It was failing for me in 32-bit because I am using additional compiler switches " -gen gcc -Wc -O2 "
Strange that the -O switch causes it to fail in both 32 and 64 bit.
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

José Roca

#63
These gcc optimizations seem dangerous. Next time, don't forget to post which switches are you using or we will end with you saying "It GPF's" and I saying "It works". If at least there was a way to disable optimizations in parts of the code...



Paul Squires

I have removed the optimization switch from all of my compiles. I fear though that others may fall into this trap if they use CFileStream with -O as I've seen the -O switch being talked about a lot and somewhat recommended on the forums (well, at least the lower number -O optimizations).
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

José Roca

As I never have used the O switch to test my code, I don't know if other parts will fail. It has been a happy coincidence that I had the -O 2 switch added in the 64 bit build configurations; otherwise, I will be still scratching my head. I remember that I did it to try the difference on size.

Johan Klassen

#66
Quote from: José Roca on August 09, 2018, 08:17:47 PM
... If at least there was a way to disable optimizations in parts of the code...
I agree, gcc already has pragmas to turn optimization on/off, so it seems feasible to add to fbc, actually I made a feature request https://www.freebasic.net/forum/viewtopic.php?p=225167
but sadly it got no attention.

Johan Klassen

#67
hello José Roca
please see this post https://www.freebasic.net/forum/viewtopic.php?p=249654#p249654
Quote from: deltarho[1859]In the opening post replace
dim as ulongint i,j
with
dim as ulongint i
dim shared as ulongint j

and the loop is no longer optimized out.

Compiled with -gen gcc -Wc -O3
is it possible to perhaps use key shared variables to trick gcc in not over optimizing?

José Roca

I'm not using loops. Just calls to the methods of the IStream interface. I have no idea why the O switch is messing it.

Johan Klassen

I can't get your example http://www.planetsquires.com/protect/forum/index.php?topic=4191.msg32153#msg32153 to crash, am using WinFBE 1.7.3, build options -s gui -gen gcc -Wc -O2, tried both 32 and 64-bit

José Roca

Hi Paul,

If I rem the line DyLibFree(pLib) in this function, located at the top of CStream.inc, it works with 64 bit and the -O switch. Why? I have no idea.


' ========================================================================================
' Opens or creates a file and retrieves a stream to read or write to that file.
' ========================================================================================
PRIVATE FUNCTION AfxSHCreateStreamOnFileEx (BYVAL pwszFile AS WSTRING PTR, BYVAL grfMode AS DWORD, _
BYVAL dwAttributes AS DWORD, BYVAL fCreate AS WINBOOL, BYVAL ppStream AS IStream PTR PTR) AS HRESULT
   DIM AS ANY PTR pLib = DyLibLoad("shlwapi.dll")
   IF pLib = NULL THEN EXIT FUNCTION
   DIM pShCreateStreamOnFileEx AS FUNCTION (BYVAL pwszFile AS WSTRING PTR, BYVAL grfMode AS DWORD, BYVAL dwAttributes AS DWORD, _
       BYVAL fCreate AS WINBOOL, BYVAL pstmTemplate AS IStream PTR, BYVAL ppStream AS IStream PTR PTR) AS HRESULT
   pShCreateStreamOnFileEx = DyLibSymbol(pLib, "SHCreateStreamOnFileEx")
   IF pShCreateStreamOnFileEx = NULL THEN EXIT FUNCTION
   DIM hr AS HRESULT = pShCreateStreamOnFileEx(pwszFile, grfMode, dwAttributes, fCreate, NULL, ppStream)
'   DyLibFree(pLib)
   RETURN hr
END FUNCTION
' ========================================================================================


José Roca

#71
I have slightly modified the function changing two EXIT FUNCTION to RETURN E_POINTER. If I use EXIT FUNCTION it will return 0, that can be interpreted as success.


' ========================================================================================
' Opens or creates a file and retrieves a stream to read or write to that file.
' ========================================================================================
PRIVATE FUNCTION AfxSHCreateStreamOnFileEx (BYVAL pwszFile AS WSTRING PTR, BYVAL grfMode AS DWORD, _
BYVAL dwAttributes AS DWORD, BYVAL fCreate AS WINBOOL, BYVAL ppStream AS IStream PTR PTR) AS HRESULT
   DIM AS ANY PTR pLib = DyLibLoad("shlwapi.dll")
   IF pLib = NULL THEN RETURN E_POINTER
   DIM pShCreateStreamOnFileEx AS FUNCTION (BYVAL pwszFile AS WSTRING PTR, BYVAL grfMode AS DWORD, BYVAL dwAttributes AS DWORD, _
       BYVAL fCreate AS WINBOOL, BYVAL pstmTemplate AS IStream PTR, BYVAL ppStream AS IStream PTR PTR) AS HRESULT
   pShCreateStreamOnFileEx = DyLibSymbol(pLib, "SHCreateStreamOnFileEx")
   IF pShCreateStreamOnFileEx = NULL THEN RETURN E_POINTER
   DIM hr AS HRESULT = pShCreateStreamOnFileEx(pwszFile, grfMode, dwAttributes, fCreate, NULL, ppStream)
   ' For some reason, if we free the library, calling the methods that use the returned
   ' IStream pointer will GPF if compiled with the -O switch.
'   DyLibFree(pLib)
   RETURN hr
END FUNCTION
' ========================================================================================


José Roca

#72
Modified again to check if the library is already loaded to avoid multiple loadings.


' ========================================================================================
' Opens or creates a file and retrieves a stream to read or write to that file.
' ========================================================================================
PRIVATE FUNCTION AfxSHCreateStreamOnFileEx (BYVAL pwszFile AS WSTRING PTR, BYVAL grfMode AS DWORD, _
BYVAL dwAttributes AS DWORD, BYVAL fCreate AS WINBOOL, BYVAL ppStream AS IStream PTR PTR) AS HRESULT
   ' // See if the library is already loaded in the address space
   DIM AS ANY PTR pLib = GetModuleHandleW("shlwapi.dll")
   ' // If it is not loaded, load it
   IF pLib = NULL THEN pLib = DyLibLoad("shlwapi.dll")
   IF pLib = NULL THEN RETURN E_POINTER
   DIM pShCreateStreamOnFileEx AS FUNCTION (BYVAL pwszFile AS WSTRING PTR, BYVAL grfMode AS DWORD, BYVAL dwAttributes AS DWORD, _
       BYVAL fCreate AS WINBOOL, BYVAL pstmTemplate AS IStream PTR, BYVAL ppStream AS IStream PTR PTR) AS HRESULT
   pShCreateStreamOnFileEx = DyLibSymbol(pLib, "SHCreateStreamOnFileEx")
   IF pShCreateStreamOnFileEx = NULL THEN RETURN E_POINTER
   DIM hr AS HRESULT = pShCreateStreamOnFileEx(pwszFile, grfMode, dwAttributes, fCreate, NULL, ppStream)
   ' For some reason, if we free the library, calling the methods that use the returned
   ' IStream pointer will GPF if compiled with the -O switch.
'   DyLibFree(pLib)
   RETURN hr
END FUNCTION
' ========================================================================================


Chris Maher

Hi José,

CPrint.PrintBitmapToFile is working fine for the 'Microsoft Print to PDF' printer, but it needs the lpszDocName member available too to be able to view, control and debug the print queue.

Thanks.

José Roca

#74
Added a parameter for the document name.