Access Violation Exception thrown

This Forum is for the use of Software Developers requiring help and assistance for Tracker Software's PDF-Tools SDK of Library DLL functions(only) - Please use the PDF-XChange Drivers API SDK Forum for assistance with all PDF Print Driver related topics or PDF-XChange Viewer SDK if appropriate.

Moderators: TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Tracker Supp-Stefan

Post Reply
isaware
User
Posts: 39
Joined: Fri Sep 25, 2009 6:21 pm

Access Violation Exception thrown

Post by isaware »

Hi,

I'm getting the following internal exception intermittently.

Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
.XCPro40_Defs.PXCp_OptimizeFonts(Int32, Int32)

Do you know why this is happening and how to prevent it?

Thank you,
Walter-Tracker Supp
User
Posts: 381
Joined: Mon Jun 13, 2011 5:10 pm

Re: Access Violation Exception thrown

Post by Walter-Tracker Supp »

What language are you using? Can you provide a short code sample to show the context?
isaware
User
Posts: 39
Joined: Fri Sep 25, 2009 6:21 pm

Re: Access Violation Exception thrown

Post by isaware »

I'm using VB (Framework 4.0 on VS 2010).

Try
Dim iRes As Integer

sCurrentProcess = "OptimizeFontsAfterMerge"
iRes = XCPro40_Defs.PXCp_OptimizeFonts(m_iPDF, 0)
mCommon.ResultChecks(iRes, sCurrentProcess)
sCurrentProcess = "OptimizeNamedDests"
iRes = XCPro40_Defs.PXCp_OptimizeRemoveNamedDests(m_iPDF)
mCommon.ResultChecks(iRes, sCurrentProcess)
sCurrentProcess = "MergedFileCompression"
iRes = XCPro40_Defs.PXCp_OptimizeStreamCompression(m_iPDF)
mCommon.ResultChecks(iRes, sCurrentProcess)

sCurrentProcess = "WriteMergeFile"
iRes = XCPro40_Defs.PXCp_WriteDocumentW(m_iPDF, m_sRootFileName, XCPro40_Defs.PXCp_CreationDisposition.PXCp_CreationDisposition_Overwrite, 0)
mCommon.ResultChecks(iRes, sCurrentProcess)

bNewFile = False
Return True
Catch ex As Exception
'Some handle
Return False
End Try

I even have the code within the try-catch block and yet it throws the exception and terminates the program..
Walter-Tracker Supp
User
Posts: 381
Joined: Mon Jun 13, 2011 5:10 pm

Re: Access Violation Exception thrown

Post by Walter-Tracker Supp »

isaware wrote:I'm using VB (Framework 4.0 on VS 2010).

Try
Dim iRes As Integer

sCurrentProcess = "OptimizeFontsAfterMerge"
iRes = XCPro40_Defs.PXCp_OptimizeFonts(m_iPDF, 0)
mCommon.ResultChecks(iRes, sCurrentProcess)
sCurrentProcess = "OptimizeNamedDests"
iRes = XCPro40_Defs.PXCp_OptimizeRemoveNamedDests(m_iPDF)
mCommon.ResultChecks(iRes, sCurrentProcess)
sCurrentProcess = "MergedFileCompression"
iRes = XCPro40_Defs.PXCp_OptimizeStreamCompression(m_iPDF)
mCommon.ResultChecks(iRes, sCurrentProcess)

sCurrentProcess = "WriteMergeFile"
iRes = XCPro40_Defs.PXCp_WriteDocumentW(m_iPDF, m_sRootFileName, XCPro40_Defs.PXCp_CreationDisposition.PXCp_CreationDisposition_Overwrite, 0)
mCommon.ResultChecks(iRes, sCurrentProcess)

bNewFile = False
Return True
Catch ex As Exception
'Some handle
Return False
End Try

I even have the code within the try-catch block and yet it throws the exception and terminates the program..
But this is completely intermittent? ie, if you re-run it on the same inputs it will occasionally terminate but other times complete successfully? Or does it depend on the input you are using?
isaware
User
Posts: 39
Joined: Fri Sep 25, 2009 6:21 pm

Re: Access Violation Exception thrown

Post by isaware »

It happens intermittent. With same files it happens sometimes it doesn't sometimes.
I'll explain what happens exactly.
Let say we have 15 PDF files that we want to combine 5 PDFs each so we could end up with 3 PDF files. (Running same functions 3 times in a loop)
It processes the first 5 PDFs and merge them correct. The next merge fails on the third file - it throws the error and terminates the program. Then the program restarts automatically and it runs the failed 5 PDFs again. This time it processes correct but for the next set of 5 PDFs, it fails again on the third file.
It is very weird that it always fails on the third file.
Also we have the whole function wrapped in the try-catch block and yet the program throws an unhandled exception..
User avatar
Ivan - Tracker Software
Site Admin
Posts: 3550
Joined: Thu Jul 08, 2004 10:36 pm
Location: Vancouver Island - Canada
Contact:

Re: Access Violation Exception thrown

Post by Ivan - Tracker Software »

Can you send us complete simplest VB project using which we can try to reproduce the issue?
Tracker Software (Project Director)

When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.
isaware
User
Posts: 39
Joined: Fri Sep 25, 2009 6:21 pm

Re: Access Violation Exception thrown

Post by isaware »

There is no simple sample code since it occurs on a complex windows service that ties to a lot of different functionalities. We've been using this service for more than 2 years now and the only change that happened recently is the OS that the service resides. Again, this issue occurs intermittently.
User avatar
Paul - Tracker Supp
Site Admin
Posts: 6901
Joined: Wed Mar 25, 2009 10:37 pm
Location: Chemainus, Canada
Contact:

Re: Access Violation Exception thrown

Post by Paul - Tracker Supp »

HI isaware,

I'm sure you can appreciate that if this is difficult to reproduce then it will be exceedingly difficult to correct. Before we go any further can you confirm that this is all being done with the current build? (Build 200 is current as I write this.) There is not much point moving forward investigating older builds if this is not an issue in 200.


Also you mentioned that this became apparent after a move to a new OS.
We've been using this service for more than 2 years now and the only change that happened recently is the OS that the service resides. Again, this issue occurs intermittently.
Is this behaviour then only on a single physical machine or does this happen on more than one machine? We are wondering if there is something hardware related that may be causing this and would like to be able to eliminate this as a possible cause.

regards
Best regards

Paul O'Rorke
Tracker Support North America
http://www.tracker-software.com
isaware
User
Posts: 39
Joined: Fri Sep 25, 2009 6:21 pm

Re: Access Violation Exception thrown

Post by isaware »

I'm attaching some sample code for this issue..
Access violation error occurs usually on saving the second merge or optimize font for the second merge.
Even when the exception doesn't get thrown, the resulted file doesn't display correct..

Public Sub New(ByVal sRootFileName As String, ByVal sFirstMergeFile As String)
Dim sCurrentProcess As String = ""
Try
Dim iRes As Integer

sCurrentProcess = "InitMergeFile"
iRes = XCPro40_Defs.PXCp_Init(m_iPDF, XCPro40_Defs.g_RegKey, XCPro40_Defs.g_DevCode)
mCommon.ResultChecks(iRes, sCurrentProcess)
sCurrentProcess = "ReadMergeFile"
iRes = XCPro40_Defs.PXCp_ReadDocumentW(m_iPDF, sFirstMergeFile, 0)
mCommon.ResultChecks(iRes, sCurrentProcess)

bHasRootFile = True
bNewFile = True
m_sRootFileName = sRootFileName
Catch ex As Exception
mCommon.LogWrite("Initialize isaPDFMerge for " & sCurrentProcess & " : " & sRootFileName & vbCrLf & ex.Message, ErrorLogType.eltPDFErrors)
bHasRootFile = False
End Try
End Sub

Public Function MergeDocument(ByVal sFileName As String) As Boolean
Dim sCurrentProcess As String = ""
Try
Dim iRes As Integer
Dim iPDFToMerge As Integer

sCurrentProcess = "InitOrgFile"
iRes = XCPro40_Defs.PXCp_Init(iPDFToMerge, XCPro40_Defs.g_RegKey, XCPro40_Defs.g_DevCode)
mCommon.ResultChecks(iRes, sCurrentProcess)
sCurrentProcess = "ReadOrgFile"
iRes = XCPro40_Defs.PXCp_ReadDocumentW(iPDFToMerge, sFileName, 0)
mCommon.ResultChecks(iRes, sCurrentProcess)

sCurrentProcess = "GetPagesCount"
Dim iPagesRange() As XCPro40_Defs.PXCp_CopyPageRange
ReDim iPagesRange(1)
Dim iPagesCount As Integer
iRes = XCPro40_Defs.PXCp_GetPagesCount(iPDFToMerge, iPagesCount)
mCommon.ResultChecks(iRes, sCurrentProcess)

sCurrentProcess = "InsertMergeFile"
iPagesRange(0).StartPage = 0
iPagesRange(0).EndPage = iPagesCount - 1
iPagesRange(0).InsertBefore = -1
XCPro40_Defs.PXCp_InsertPagesTo(iPDFToMerge, m_iPDF, iPagesRange, 1, 0)
mCommon.ResultChecks(iRes, sCurrentProcess)

sCurrentProcess = "WriteMergeFile"
iRes = XCPro40_Defs.PXCp_WriteDocumentW(m_iPDF, m_sRootFileName, XCPro40_Defs.PXCp_CreationDisposition.PXCp_CreationDisposition_Overwrite, XCPro40_Defs.PXCp_WriteDocFlag.PXCp_Write_NoRelease)
mCommon.ResultChecks(iRes, sCurrentProcess)

sCurrentProcess = "ReleaseMergeFile"
iRes = XCPro40_Defs.PXCp_Delete(iPDFToMerge)
mCommon.ResultChecks(iRes, sCurrentProcess)

Return True

Catch ex As Exception
mCommon.LogWrite("MergeDocument for " & sCurrentProcess & " : " & sFileName & vbCrLf & ex.Message, ErrorLogType.eltPDFErrors)
Return False
End Try
End Function

Public Function WriteDocument() As Boolean
Dim sCurrentProcess As String = ""
Try
Dim iRes As Integer

sCurrentProcess = "OptimizeFontsAfterMerge"
iRes = XCPro40_Defs.PXCp_OptimizeFonts(m_iPDF, 0)
mCommon.ResultChecks(iRes, sCurrentProcess)
sCurrentProcess = "OptimizeNamedDests"
iRes = XCPro40_Defs.PXCp_OptimizeRemoveNamedDests(m_iPDF)
mCommon.ResultChecks(iRes, sCurrentProcess)
sCurrentProcess = "MergedFileCompression"
iRes = XCPro40_Defs.PXCp_OptimizeStreamCompression(m_iPDF)
mCommon.ResultChecks(iRes, sCurrentProcess)

sCurrentProcess = "WriteMergeFile"
iRes = XCPro40_Defs.PXCp_WriteDocumentW(m_iPDF, m_sRootFileName, XCPro40_Defs.PXCp_CreationDisposition.PXCp_CreationDisposition_Overwrite, 0)
mCommon.ResultChecks(iRes, sCurrentProcess)

sCurrentProcess = "ReleaseMergeFile"
iRes = XCPro40_Defs.PXCp_Delete(m_iPDF)
mCommon.ResultChecks(iRes, sCurrentProcess)

bNewFile = False
Return True
Catch ex As Exception
mCommon.LogWrite("WriteDocument for " & sCurrentProcess & vbCrLf & ex.Message, ErrorLogType.eltPDFErrors)
Return False
End Try

End Function

Public Function ReleaseFirstTime() As Boolean
Dim sCurrentProcess As String = ""
Try
Dim iRes As Integer
sCurrentProcess = "WriteMergeFile"
iRes = XCPro40_Defs.PXCp_WriteDocumentW(m_iPDF, m_sRootFileName, XCPro40_Defs.PXCp_CreationDisposition.PXCp_CreationDisposition_Overwrite, XCPro40_Defs.PXCp_WriteDocFlag.PXCp_Write_NoRelease)
mCommon.ResultChecks(iRes, sCurrentProcess)

Return True
Catch ex As Exception
mCommon.LogWrite("ReleaseFirstTime for " & sCurrentProcess & vbCrLf & ex.Message, ErrorLogType.eltPDFErrors)
Return False
End Try
End Function



Functions to create merged files.

Dim oMergePDF As isaPDFMerge = New isaPDFMerge(sMergedFileName1, sFirstFile)
oMergePDF.ReleaseFirstTime()
oMergePDF.MergeDocument(sSecondFile)
oMergePDF.MergeDocument(sThirdFile)
oMergePDF.MergeDocument(sFourthFile)
oMergePDF.MergeDocument(sFifthFile)
oMergePDF.WriteDocument()

oMergePDF = New isaPDFMerge(sMergedFileName2, sFirstFile)
oMergePDF.ReleaseFirstTime()
oMergePDF.MergeDocument(sSecondFile)
oMergePDF.MergeDocument(sThirdFile)
oMergePDF.MergeDocument(sFourthFile)
oMergePDF.MergeDocument(sFifthFile)
oMergePDF.WriteDocument()
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17948
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Access Violation Exception thrown

Post by Tracker Supp-Stefan »

Thanks for the sample code isaware,

I've asked one of my colleagues to take a look and we will post back here as soon as we have any further comments.

Best,
Stefan
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17948
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Access Violation Exception thrown

Post by Tracker Supp-Stefan »

Hello again isaware,

We discussed this case in the team, and we will need a complete sample (sources, executable and test files), as at first look the code you have posted looks correct but my colleague working on your case says that he is doing something differently than you (e.g. using older/newer wrappers) - and as such it would be best and fastest if you send us the full sample.

Best,
Stefan
isaware
User
Posts: 39
Joined: Fri Sep 25, 2009 6:21 pm

Re: Access Violation Exception thrown

Post by isaware »

I'm attaching the full sample code. I took out the dev/reg codes.
I also had to took out some files (including dlls) since the forum only allows maximum of 5MB.
Attachments
AccessViolationException.zip
(37.43 KiB) Downloaded 232 times
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17948
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Access Violation Exception thrown

Post by Tracker Supp-Stefan »

Thanks isaware,

We will work with that and post here as soon as we have any news.

Best,
Stefan
User avatar
Lzcat - Tracker Supp
Site Admin
Posts: 677
Joined: Thu Jun 28, 2007 8:42 am

Re: Access Violation Exception thrown

Post by Lzcat - Tracker Supp »

Can you provide your test files, please?
And with detailed instruction how to use your project, please - it runs, but no error messages, no crash, nothing.
Victor
Tracker Software
Project manager

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
isaware
User
Posts: 39
Joined: Fri Sep 25, 2009 6:21 pm

Re: Access Violation Exception thrown

Post by isaware »

Hi, I've attached 5 PDF files.
Attachments
test5.pdf
(16.25 KiB) Downloaded 340 times
test4.pdf
(16.26 KiB) Downloaded 280 times
test3.pdf
(16.25 KiB) Downloaded 306 times
test2.pdf
(16.26 KiB) Downloaded 323 times
test1.pdf
(16.25 KiB) Downloaded 241 times
isaware
User
Posts: 39
Joined: Fri Sep 25, 2009 6:21 pm

Re: Access Violation Exception thrown

Post by isaware »

I'm also attaching the outcome files.
Attachments
testresult2_test.pdf
(20.47 KiB) Downloaded 280 times
testresult_test.pdf
(39.8 KiB) Downloaded 377 times
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17948
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Access Violation Exception thrown

Post by Tracker Supp-Stefan »

hello isaware,

Seems like Lzcat (Victor) has finished for today - but I have passed him your samples and he will follow up here as soon as possible.

Best,
Stefan
User avatar
Lzcat - Tracker Supp
Site Admin
Posts: 677
Joined: Thu Jun 28, 2007 8:42 am

Re: Access Violation Exception thrown

Post by Lzcat - Tracker Supp »

Thanks for files, but where I should put them?
Or where I should put breakpoint to start debugging and see where files should be located?
Victor
Tracker Software
Project manager

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
isaware
User
Posts: 39
Joined: Fri Sep 25, 2009 6:21 pm

Re: Access Violation Exception thrown

Post by isaware »

If you look at the code, I hardcoded the file on the form load event.
You can just change the file path - also the file path for the new merged PDFs - to wherever you saved the file.
User avatar
Lzcat - Tracker Supp
Site Admin
Posts: 677
Joined: Thu Jun 28, 2007 8:42 am

Re: Access Violation Exception thrown

Post by Lzcat - Tracker Supp »

Hi isaware.
As in most cases when using xcpro a crash is because of an incorrect call sequence:
Public Function WriteDocument() As Boolean
'some code skipped
sCurrentProcess = "WriteMergeFile"
iRes = XCPro40_Defs.PXCp_WriteDocumentW(m_iPDF, m_sRootFileName, XCPro40_Defs.PXCp_CreationDisposition.PXCp_CreationDisposition_Overwrite, 0)
mCommon.ResultChecks(iRes, sCurrentProcess)

sCurrentProcess = "ReleaseMergeFile"
iRes = XCPro40_Defs.PXCp_Delete(m_iPDF)
mCommon.ResultChecks(iRes, sCurrentProcess)
Call to PXCp_WriteDocumentW with the last parameter 0 (zero) will release the document, and subsequent calls to the PXCp_Delete function with the same document handle will result memory corruption. This may be not a problem for a while, but the program will eventually crash (in your case - at the second write operation). The solution is to replace 0 with XCPro40_Defs.PXCp_WriteDocFlag.PXCp_Write_NoRelease (or do not call the PXCp_Delete function if the write operation was successful.
Victor
Tracker Software
Project manager

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
Post Reply