Delphi IPXC_Document double-delete

A forum for questions or concerns related to the PDF-XChange Core API SDK

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

Forum rules
DO NOT post your license/serial key, or your activation code - these forums, and all posts within, are public and we will be forced to immediately deactivate your license.

When experiencing some errors, use the IAUX_Inst::FormatHRESULT method to see their description and include it in your post along with the error code.
Post Reply
GeoffW
User
Posts: 19
Joined: Mon Jul 23, 2018 10:01 am

Delphi IPXC_Document double-delete

Post by GeoffW »

Core API SDK v7.0 build 326. Delphi v10.2, 32bit project, under a Windows 10 x64 VM.

I have an odd problem and I thought I'd ask here to see if anyone else has seen it. The following code should compile with minimal change:

Code: Select all

procedure TestPdfDoc(const AFilename: string);
var
  idoc: IPXC_Document;
  page: IPXC_Page;
  i, count: DWORD;
begin
  // PdfXInst is a PXC_Inst global in this test unit
  idoc := PdfXInst.OpenDocumentFromFile(PChar(AFilename), nil, nil, 0, 0);
  if Assigned(idoc) then
  begin
    idoc.Pages.Get_Count(count);
    for i := 0 to count - 1 do
    begin
      idoc.Pages.Get_Item(i, page);
    end;
    idoc.Close(0);
    page := nil;
    idoc := nil;  // <<<<< If this line exists
  end;
end;              // <<<<< Access violation here
Obviously "idoc := nil;" is redundant in this case, but it should be harmless. It's not, it causes an access violation in the Delphi @IntfClear function as the function exits. Take out the redundant assignment and there is no AV. Delphi is obviously trying to delete idoc a second time. You can have two or three "idoc := nil;" and you still only get the error as the procedure exits and the automatic Delphi clean up takes effect. Does that make this a Delphi bug?

But it only happens with IPXC_Document variables. The IPXC_Page variable in the example is quite happy with its redundant clean up. Does that make it a CORE API SDK bug? Or maybe someone here will try this with the same version of Delphi and won't see the problem, making it all my problem. :(

In the above example, you can leave "idoc := nil;" in the code if you take out the Pages.Get_Count and Pages.Get_Item loop. So there would seem to be something that happens in the SDK that alters Delphi's behaviour at clean up. Any guesses?

P.S. This is not a urgent problem for me. The assignment to nil truly is redundant, even in my original code. (I was trying it out to see if idoc was holding a file open (it wasn't) when I found this error.) So not urgent but I would like to resolve it, or at least work how where it's coming from.

Edited to Add: My PdfXInst is a PXC_Inst global is created via loadlibrary and calling PXC_GetInstance (not via a registered COM server). Just in case that makes a difference.
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Delphi IPXC_Document double-delete

Post by Sasha - Tracker Dev Team »

Hello GeoffW,

Our Delphi expert is away for vacation for 3 weeks - please bump this thread again when he will be available.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
GeoffW
User
Posts: 19
Joined: Mon Jul 23, 2018 10:01 am

Re: Delphi IPXC_Document double-delete

Post by GeoffW »

I've included this problem in the demo attached to my other thread (about merging original and text-only pdfs) - so at least you might be able to run that and see whether you get the same problem (or hold onto it for your expert). I'll try to remember to bump this thread later anyway, if I haven't heard back before then.
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Delphi IPXC_Document double-delete

Post by Sasha - Tracker Dev Team »

Hello GeoffW,

Got it. Will address this to my teammate when he gets back from the vacation.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
GeoffW
User
Posts: 19
Joined: Mon Jul 23, 2018 10:01 am

Re: Delphi IPXC_Document double-delete

Post by GeoffW »

Sasha - Tracker Dev Team wrote:Hello GeoffW,

Our Delphi expert is away for vacation for 3 weeks - please bump this thread again when he will be available.

Cheers,
Alex
Bump
Dima - Tracker Dev Team
User
Posts: 3
Joined: Mon Sep 21, 2015 6:16 am

Re: Delphi IPXC_Document double-delete

Post by Dima - Tracker Dev Team »

Hello GeoffW,

That when calling the function "idoc.Pages.Get_Count" that returns the interface, creates an "invisible" variable - an interface link, for which at the end of the procedure, in connection with its output from the zone of visibility, called _IntfClear. And the object is already destroyed by this time.

Code: Select all

procedure TestPdfDoc(const AFilename: string);
var
  idoc: IPXC_Document;
  page: IPXC_Page;
  pages: IPXC_Pages; // Add object
  i, count: DWORD;
begin
  // PdfXInst is a PXC_Inst global in this test unit
  idoc := PXC_Inst1.OpenDocumentFromFile(PChar(AFilename), nil, nil, 0, 0);
  if Assigned(idoc) then
  begin
    pages := idoc.Pages; //Add "invisible" object
    pages.Get_Count(count);
    for i := 0 to count - 1 do
    begin
      pages.Get_Item(i, page);
    end;
    page := nil;
    pages := nil; // Corect release
    idoc.Close(0);
    idoc := nil;
  end;
end;
GeoffW
User
Posts: 19
Joined: Mon Jul 23, 2018 10:01 am

Re: Delphi IPXC_Document double-delete

Post by GeoffW »

Dima - Tracker Dev Team wrote:Hello GeoffW,

That when calling the function "idoc.Pages.Get_Count" that returns the interface, creates an "invisible" variable - an interface link, for which at the end of the procedure, in connection with its output from the zone of visibility, called _IntfClear. And the object is already destroyed by this time.[...]
Thank you very much for the explanation. I can confirm that it behaves as you say. I have not come across that before. It seems very strange that Delphi would retain the temporary variable for so long ... and then I suppose the dependencies between document and pages come into play (the pages would need a document reference to stop it being prematurely deleted).

Anyway, thanks again. Now I know what to look for.
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17824
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Delphi IPXC_Document double-delete

Post by Tracker Supp-Stefan »

:)
Post Reply