Results 1 to 7 of 7

Thread: Initializing function references, access violation exceptions

  1. #1
    Join Date
    Oct 2013
    Location
    East Coast USA
    Posts
    770
    Mentioned
    61 Post(s)
    Quoted
    364 Post(s)

    Default Initializing function references, access violation exceptions

    Why do I get access violation exceptions if I try to initialize function references with

    var xxx = @reference

    Exception in Script: Runtime error: "Access violation" at line 31, column 23

    If I manually initialize it it works fine.

    Code:
    program new;
    
    function realfunction() : boolean;
    begin
      result := true
    end;
    
    type
      trec = record
        callthis : function:boolean;
      end;
    
    var
      noinit : function:boolean;
      norec : function:boolean = @realfunction;
      onerec : trec = [@realfunction];
      funcarr : array[0..0] of trec = [[@realfunction]];
      b : boolean;
    
    begin
      noinit := @realfunction;
      noinit();
      writeln(b);
    
      try
        b := norec();
        writeln(b);
      except
        writeln('got access violation');
      end;
    
      try
        b := onerec.callthis();
        writeln(b);
      except
        writeln('got access violation');
      end;
    
      try
        with funcarr[0] do
          b := callthis();
        writeln(b);
      except
        writeln('got access violation');
      end;
    end.
    Code:
    False
    got access violation
    got access violation
    got access violation
    Successfully executed.

  2. #2
    Join Date
    Sep 2012
    Location
    Here.
    Posts
    2,007
    Mentioned
    88 Post(s)
    Quoted
    1014 Post(s)

    Default

    I don't remember the entire scenario, but I recall in pascalscript I was having troubles using function pointers, despite having no such issues with procedure pointers. I wonder if a similar issue exists in Lape due to their similar natures. @Dgby714 As you're the name I'm most familiar with in all these updates... Ideas?

  3. #3
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Try initialising it with := instead of = and see what happens?
    I am Ggzz..
    Hackintosher

  4. #4
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Quote Originally Posted by Brandon View Post
    Try initialising it with := instead of = and see what happens?
    ^^^

    Lape parses variable initialization before assigning methods a stack position. (Meaning using "=" makes them nil)

    Using ":=" basicly tells Lape to add delayed code, and the variable gets assigned right before the code starts being ran.

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  5. #5
    Join Date
    Oct 2013
    Location
    East Coast USA
    Posts
    770
    Mentioned
    61 Post(s)
    Quoted
    364 Post(s)

    Default

    That fixes the simple case but still no joy with the record/array initialization.

    I figured I was doing something wrong. I'll just init the array with assignments in a function to bypass.

    Code:
    program new;
    
    function realfunction() : boolean;
    begin
      result := true
    end;
    
    type
      trec = record
        callthis : function:boolean;
      end;
    
    var
      noinit : function:boolean;
      norec : function:boolean := @realfunction;
      onerec : trec := [@realfunction];
      funcarr : array[0..0] of trec := [[@realfunction]];
      b : boolean;
    
    begin
      noinit := @realfunction;
      b := noinit();
      writeln(b);
    
      try
        b := norec();
        writeln(b);
      except
        writeln('got access violation');
      end;
    
      try
        b := onerec.callthis();
        writeln(b);
      except
        writeln('got access violation');
      end;
    
      try
        with funcarr[0] do
          b := callthis();
        writeln(b);
      except
        writeln('got access violation');
      end;
    end.
    Code:
    True
    True
    got access violation
    got access violation
    Successfully executed.

  6. #6
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    This is because Lape's delay initialisation is flawed and useless and only initialises right before the variable or function is used.. You can't really depend on it to do anything right.. Just trick it then.. Or initialise within the body of the main.

    The way to get around it is to do:

    Simba Code:
    type T = record
      func: function: Boolean;
    end;

    function callme: boolean;
    begin
      writeln('called');
      result := true;
    end;

    var
      Ptr: Pointer := [@Callme]; //Delay initialisation here..
      R: T := [Ptr]; //It is initialised here because this is where the variable is being used..
    begin
      R.func();
    end.

    Doing
    R: T := [AddressOf(Callme)];

    does nothing because it initialises it to 0xFFFFFFFF.


    EDIT: Ahh the old villavu mention tag bug.. Can't use the @ sign because villavu's keeps parsing it as a mention tag.. How stupid.
    Last edited by Brandon; 10-14-2013 at 08:24 PM.
    I am Ggzz..
    Hackintosher

  7. #7
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Quote Originally Posted by Brandon View Post
    This is because Lape's delay initialisation is flawed and useless and only initialises right before the variable or function is used.. You can't really depend on it to do anything right.. Just trick it then.. Or initialise within the body of the main.

    The way to get around it is to do:

    Simba Code:
    type T = record
      func: function: Boolean;
    end;

    function callme: boolean;
    begin
      writeln('called');
      result := true;
    end;

    var
      Ptr: Pointer := [@Callme]; //Delay initialisation here..
      R: T := [Ptr]; //It is initialised here because this is where the variable is being used..
    begin
      R.func();
    end.

    Doing
    R: T := [AddressOf(Callme)];

    does nothing because it initialises it to 0xFFFFFFFF.


    EDIT: Ahh the old villavu mention tag bug.. Can't use the @ sign because villavu's keeps parsing it as a mention tag.. How stupid.
    I think it's creating the array on the stack, then calling the assign in the delayed code.

    The delayed initialization isn't that smart iirc, it just does the initialization before the first Call.
    The first call being the first call on this scope, the main begin end. for global and the method contents on local.
    Last edited by Dgby714; 10-15-2013 at 10:08 AM.

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •