Menu

Bind Using OCI_BAM_INTERNAL

mch
2015-08-23
2015-08-23
  • mch

    mch - 2015-08-23

    HI,
    I am trying to bind some variable to the statement and this is my sample
    coede below:

    void binda( OCI_Statement st)
    {
    {
    int n = 0,n2 =1;
    OCI_BindInt(st,"res",&n);
    OCI_BindInt(st,"p1",&n2);
    }
    }
    int main(void)
    {
    OCI_Connection
    cn;
    OCI_Statement st;
    OCI_Resultset
    rs;

    if (!OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT | OCI_ENV_THREADED
    

    |OCI_ENV_CONTEXT))
    return EXIT_FAILURE;

    cn = OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT);
    
    st = OCI_StatementCreate(cn);
    
    OCI_SetBindAllocation(st,OCI_BAM_INTERNAL);
    
    OCI_Prepare(st, "begin  :res := func( :p1); end;");
    
    binda(st);
    OCI_Execute(st); /******** If I use OCI_BAM_EXTERNAL  it crashes after
    

    calling this function*/

    OCI_StatementFree(st);  /******** If I use OCI_BAM_INTERNAL it crashes
    

    after calling this function*/

    OCI_Cleanup();
    
    return EXIT_SUCCESS;
    

    }

    please help me how to bind variable not on the stack.

    Thanks

     
    • Vincent Rogier

      Vincent Rogier - 2015-08-23

      Hi,

      Variables that are used for binding must exist and be accessible when
      OCI_Execute() is called.
      In your example, n and n2 are local variable of method binda(). Thus, when
      binda() return they do not exists and the program crashes as it attempts to
      access non reachable memory.
      When using internal allocation mode, you must pass null pointers to bind
      functions.

      Thus:
      - when using external allocation mode (default), provide valid addresses
      for the life time of a prepared statement (until it is freed or until it is
      re-prepared)
      - when using internal allocation mode, pass null pointers.

      btw, OCILIB has moved to Github. Please use the Github tracker instead of
      SF resources.

      thanks :)

      Regards,

      On Sun, Aug 23, 2015 at 3:46 PM, mch m-ch@users.sf.net wrote:

      HI,
      I am trying to bind some variable to the statement and this is my sample
      coede below:

      void binda( OCI_Statement

      st) { { int n = 0,n2 =1; OCI_BindInt(st,"res",&n);
      OCI_BindInt(st,"p1",&n2); } } int main(void) { OCI_Connection
      cn;
      OCI_Statement

      st; OCI_Resultset rs;

      if (!OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT | OCI_ENV_THREADED

      |OCI_ENV_CONTEXT))
      return EXIT_FAILURE;

      cn = OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT);

      st = OCI_StatementCreate(cn);

      OCI_SetBindAllocation(st,OCI_BAM_INTERNAL);

      OCI_Prepare(st, "begin :res := func( :p1); end;");

      binda(st);
      OCI_Execute(st); /** If I use OCI_BAM_EXTERNAL it crashes after

      calling this function*/

      OCI_StatementFree(st); /** If I use OCI_BAM_INTERNAL it crashes

      after calling this function*/

      OCI_Cleanup();

      return EXIT_SUCCESS;

      }

      please help me how to bind variable not on the stack.

      Thanks

      Bind Using OCI_BAM_INTERNAL
      https://sourceforge.net/p/orclib/discussion/470800/thread/ded30412/?limit=25#74cd


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/orclib/discussion/470800/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

      --
      Vincent Rogier

       
  • Vincent Rogier

    Vincent Rogier - 2015-08-23

    Hi,

    Variables that are used for binding must exist and be accessible when OCI_Execute() is called.
    In your example, n and n2 are local variable of method binda(). Thus, when binda() return they do not exists and the program crashes as it attempts to access non reachable memory.
    When using internal allocation mode, you must pass null pointers to bind functions.

    Thus:
    - when using external allocation mode (default), provide valid addresses for the life time of a prepared statement (until it is freed or until it is re-prepared)
    - when using internal allocation mode, pass null pointers.

    btw, OCILIB has moved to Github. Please use the Github tracker instead of SF resources.

    thanks :)

    Regards,

    vincent