Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8084979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:57:40+00:00 2026-06-05T17:57:40+00:00

I am getting an unexpected Access Violation error in the following code: program Project65;

  • 0

I am getting an unexpected Access Violation error in the following code:

program Project65;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  SysUtils;

type
  ITest = interface
  end;

  TTest = class(TInterfacedObject, ITest)
  end;

var
  p: ^ITest;

begin
  GetMem(p, SizeOf(ITest)); 
  p^ := TTest.Create; // AV here
  try
  finally
    p^ := nil;
    FreeMem(p);
  end;
end.

I know that interfaces should be used differently. However I am working on a legacy codebase which uses this approach. And I was very surprised to see that it is not sufficient to reserve SizeOf(ITest) memory to put an ITest there.

Now interestingly if I change the first line to

GetMem(p, 21);

than the AV is gone. (20 bytes or less fails). What is the explanation to this?

(I am using Delphi XE2 Update 4 + HotFix)

Please don’t comment on how horrible the code is or suggest how this could be coded properly. Instead please answer why is it necessary to reserve 21 bytes instead of SizeOf(ITest) = 4?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:57:42+00:00Added an answer on June 5, 2026 at 5:57 pm

    What you have effectively written is doing the following logic behind the scenes:

    var
      p: ^ITest;
    begin
      GetMem(p, SizeOf(ITest));
      if p^ <> nil then p^._Release; // <-- AV here
      PInteger(p)^ := ITest(TTest.Create);
      p^._AddRef;
      ...
      if p^ <> nil then p^._Release;
      PInteger(p)^ := 0;
      FreeMem(p);
    end;
    

    GetMem() is not guaranteed to zero out what it allocates. When you assign the new object instance to the interface varaiable, if the bytes are not zeros, the RTL will think there is already an existing interface reference and will try to call its _Release() method, causing the AV since it is not backed by a real object instance. You need to zero out the allocated bytes beforehand, then the RTL will see a nil interface reference and not try to call its _Release() method anymore:

    program Project65;
    
    {$APPTYPE CONSOLE}
    
    {$R *.res}
    
    uses
      SysUtils;
    
    type
      ITest = interface
      end;
    
      TTest = class(TInterfacedObject, ITest)
      end;
    
    var              
      p: ^ITest;              
    
    begin              
      GetMem(p, SizeOf(ITest));               
      try
        FillChar(p^, SizeOf(ITest), #0); // <-- add this!
        p^ := TTest.Create; // <-- no more AV
        try
          ...
        finally
          p^ := nil;
        end;
      finally
        FreeMem(p);
      end;
    end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting the following error in a Joomla model file: unexpected T_VARIABLE, expecting T_FUNCTION
getting below error on IE8 canvas.getContext(2d) Unexpected call to method or property access.Error i
I'm getting unexpected output when running following code, DateFormat df = new SimpleDateFormat(YYYY-MM-dd); Date
I'm getting an unexpected T_CONCAT_EQUAL error on a line of the following form: $arg1
I am getting unexpected T_TRY, expecting T_FUNCTION error message and am not sure as
I am running this code and getting unexpected results. I expect that the loop
I can't quite figure out why I am getting unexpected results from the following
I am getting an Unexpected error. I tried a few times, and I still
I am getting unexpected console output after array assignment in webkit browsers (Chrome 16.0.912.77
I am getting Ruby Error: syntax error, unexpected tGVAR, expecting $end. I am using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.