Prolog code causing Access Violation error in VS2008

This is a discussion on Prolog code causing Access Violation error in VS2008 within the General AI Programming forums, part of the Cprogramming.com and AIHorizon.com's Artificial Intelligence Boards category; Does anyone know why including a term such as: Code: t = PL_new_term_ref(); would cause an Unhandled Exception error message: ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    26

    Prolog code causing Access Violation error in VS2008

    Does anyone know why including a term such as:
    Code:
        t = PL_new_term_ref();
    would cause an Unhandled Exception error message:
    Code:
        0xC0000005: Access violation reading location 0x0000000c.
    (Visual Studio 2008)


    I have a header file:
    Code:
        class UserTaskProlog : public ArAction
         {
          public:
                  UserTaskProlog( const char* name = " sth " );
                  ~UserTaskProlog( );
                  AREXPORT virtual ArActionDesired *fire( ArActionDesired currentDesired );  
    
          private:
                  term_t t;
         };
    and a cpp file:
    Code:
        UserTaskProlog::UserTaskProlog( const char* name ) : ArAction( name, " sth " )
         {
          char** argv;
          argv[ 0 ] = "libpl.dll";
          PL_initialise( 1, argv );
          PlCall( "consult( 'myProg.pl' )" );
         }
    
        UserTaskProlog::~UserTaskProlog( )
         {
         }
    
        ArActionDesired *UserTaskProlog::fire( ArActionDesired currentDesired )
         {
          cout << " something " << endl;
          t = PL_new_term_ref( );
         }
    Without t=PL_new_term_ref() everything works fine, but when I start adding my Prolog code (declarations first, such as t=PL_new_term_ref), I get this Access Violation error message.

    I'd appreciate any help.

    Thanks,

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    48
    You need to some memory allocated here first.
    Code:
    char** argv;
    argv[ 0 ] = "libpl.dll";

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    26
    How do I do that, given that I am using the above as a Prolog engine initializer?

    I'm actually trying to avoid plld and foreign_t complications by loading my Prolog sources and initializing them at the start (in my constructor):
    Code:
        # include files...
    
        UserTaskProlog::UserTaskProlog( const char* name ) : ArAction( name, "Joshua" )
        {
         char** argv;
         argv[0] = "libpl.dll";
         PL_initialise( 1, argv );
         PlCall( "consult( swi( 'plwin.rc' ) )" );
         PlCall( "consult( 'myPL.pl' )" );
        }
    
        UserTaskProlog::~UserTaskProlog()
        {
        }
    
        AREXPORT ArActionDesired *UserTaskProlog::fire( ArActionDesired currentDesired )
        {
         C++ stuff;
         //t = PL_new_term_ref();
         return NULL;
        }
    Is this not possible for linking my pl and c++ sources?

    As soon as I uncomment the PL_new_term_ref line and run the program, I get the error message:
    Code:
        Unhandled exception at 0x0054870f in Joshua.exe:
        0xC0000005: Access violation reading location 0x0000000c.
    with a green pointer pointing at this very line.

    I did try a little something with plld:
    Code:
        plld  -o  Joshua.exe  myCPP.cpp  myPL.pl
    But got a deluge of error messages, mainly to do with:
    Code:
        . . . Joshua.obj:  error LNK2019:  unresolved external symbol  "__declspec(dllimport) public . . . "
    Is this ? indicative of something particular???

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    This doesn't have anything to do with Prolog. When you declare a pointer, like you did with argv, by default it points to New Jersey. You have to make that pointer point to some memory inside your own computer (with malloc, or if you're feeling Windows-like whatever the Windows system call is) before you can then use it.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    26
    Thanks tabstop.

    I made my argv a fixed-size array of characters, but the error message is persistent...


    Original:

    I am interfacing C++ and Prolog and I am trying to avoid the plld utility and foreign_t complications by loading my Prolog source code at the start (in my constructor):

    Here is my header file:
    Code:
        #include...
    
        class UserTaskProlog : public ArAction
        {
            public:
                AREXPORT UserTaskProlog( const char* name = "Prolog" );
                AREXPORT virtual ~UserTaskProlog( );
                AREXPORT virtual ArActionDesired *fire( ArActionDesired currentDesired );
    
            protected:
                ArActionDesired myDesired;
                int aria_cycles;
                term_t t;
        };
    And here is my main code:
    Code:
        #include...
    
        AREXPORT UserTaskProlog::UserTaskProlog( const char* name ) : ArAction( name, "Prolog" )
        {
            char* argv[ 1 ];
            argv[ 0 ] = "libpl.dll";
            PL_initialise( 1, argv );
            if ( !PL_initialise( 1, argv ) )
                PL_halt( 1 );
            PlCall( "consult( 'myPrologFile.pl' )" );
            aria_cycles = 0;
        }
    
        AREXPORT UserTaskProlog::~UserTaskProlog( )
        {
        }
    
        AREXPORT ArActionDesired *UserTaskProlog::fire( ArActionDesired currentDesired )
        {
            aria_cycles++;
            cout << aria_cycles << endl;
            // t = PL_new_term_ref( );
            return NULL;
        }
    
        int main( int argc, char** argv )
        {
            UserTaskProlog talk;
    
            robot.addAction( &talk, 100 );
    
            Aria::exit( 0 );
        }
    The protected Prolog declation in my header file doesn't cause any problems. Also, with PL_new_term_ref commented out, I press F5 in VS2008 and my program builds and compiles successfully, with aria_cycles printing out and incrementing. But as soon as I uncomment PL_new_term_ref, I get the error message:
    Code:
        Unhandled exception at 0x0054870f in Joshua.exe:
        0xC0000005: Access violation reading location 0x0000000c.
    with a green pointer pointing at this very line.

    Can anyone spot what I am doing wrong here?

    Thanks,

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Just as a reference, it's highly unlikely that you want to call PL_initialise twice. Looking at the manual, it doesn't say you can't, but that's a lot of things running behind the scenes. Right below it is PL_is_initialised -- or better yet, just save the return code from the first time into a variable.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    26
    Quote Originally Posted by HJoshy View Post
    Thanks tabstop.

    I made my argv a fixed-size array of characters, but the error message is persistent...

    I am interfacing C++ and Prolog and I am trying to avoid the plld utility and foreign_t complications by loading my Prolog source code at the start (in my constructor):

    Here is my header file:
    Code:
        #include...
    
        class UserTaskProlog : public ArAction
        {
            public:
                AREXPORT UserTaskProlog( const char* name = "Prolog" );
                AREXPORT virtual ~UserTaskProlog( );
                AREXPORT virtual ArActionDesired *fire( ArActionDesired currentDesired );
    
            protected:
                ArActionDesired myDesired;
                int aria_cycles;
                term_t t;
        };
    And here is my main code:
    Code:
        #include...
    
        AREXPORT UserTaskProlog::UserTaskProlog( const char* name ) : ArAction( name, "Prolog" )
        {
            char* argv[ 1 ];
            argv[ 0 ] = "libpl.dll";
            PL_initialise( 1, argv );
            PlCall( "consult( 'myPrologFile.pl' )" );
            aria_cycles = 0;
        }
    
        AREXPORT UserTaskProlog::~UserTaskProlog( )
        {
        }
    
        AREXPORT ArActionDesired *UserTaskProlog::fire( ArActionDesired currentDesired )
        {
            aria_cycles++;
            cout << aria_cycles << endl;
            // t = PL_new_term_ref( );
            return NULL;
        }
    
        int main( int argc, char** argv )
        {
            UserTaskProlog talk;
    
            robot.addAction( &talk, 100 );
    
            Aria::exit( 0 );
        }
    The protected Prolog declation in my header file doesn't cause any problems. Also, with PL_new_term_ref commented out, I press F5 in VS2008 and my program builds and compiles successfully, with aria_cycles printing out and incrementing. But as soon as I uncomment PL_new_term_ref, I get the error message:
    Code:
        Unhandled exception at 0x0054870f in Joshua.exe:
        0xC0000005: Access violation reading location 0x0000000c.
    with a green pointer pointing at this very line.

    Can anyone spot what I am doing wrong here?

    Thanks,

    Good point tabstop, but what do you mean by "ust save the return code from the first time into a variable" ?

  8. #8
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    If you are using this
    Code:
            PL_initialise( 1, argv );
            if ( !PL_initialise( 1, argv ) )
                PL_halt( 1 );
    then you call PL_initialize twice. If tabstop is referring to that then he meant sth like:
    Code:
    bool returnCode = PL_initialize(1,argv);
    if (!returnCode)
     ...
    Of course you should just erase the first call and leave the if part as it is.

    Just a quick suggestion. Try:
    Code:
    t = (term_t)0;
    to see if it lets you change t (which it should I guess)

    or int main (or somewhere more suitable)
    Code:
    term_t tmp = PL_new_term_ref();
    to see if the PL_new_term_ref() is functioning. Maybe you forgot to add something?
    Last edited by C_ntua; 04-17-2010 at 06:12 AM.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    26
    .


    OK, thanks. That was useful. I left:
    Code:
    PL_initialise( 1, argv );
    and got rid of the other 'if' check statement.

    Then I tried your suggestion, but it still wouldn't let me use:
    Code:
    PL_new_term_ref( );
    I can't figure out why...


    .

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The manual (again with the manual) suggests you need to open a frame before you can do any references.

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    26
    .


    I have tried that, believe it or not!

    I have tried all the examples in the SWI manual, including the foreign_t and PL_open_frame to no avail in this Robotics program in VS2008.

    I am in the process of trying a different class , instead of
    Code:
    class UserTaskProlog : public ArAction
    which is an ARIA ArAction class.

    I will post again if anything changes.

    I appreciate your help and attention to my posts.

    Cheers,


    .

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    26

    argv Memory Allocation

    Have attended to my replies and revised my code.

    Now, I was wondering if someone could tell me what I am doing wrong that I get this Unhandled Exception error message:
    Code:
        0xC0000005: Access violation reading location 0x0000000c.
    with a green pointer pointing at this line:
    Code:
        t = PL_new_term_ref( );
    Here is my header file:
    Code:
        #pragma once
        #include files...
    
        class UserTaskProlog
        {
    	public:
    			UserTaskProlog( ArRobot* r );
    			~UserTaskProlog( );
    	protected:
    			int cycles;
    			char* argv[ 1 ];
    			term_t tf;
    			term_t tx;
    			term_t goal_term;
    			functor_t goal_functor;
    			ArRobot* robot;
     			void logTask( );
        };
    And here is my main code:
    Code:
        #include files...
        using namespace std;
    
        UserTaskProlog::UserTaskProlog( ArRobot* r ) : robot( r ), robotTaskFunc( this, &UserTaskProlog::logTask )
        {
    	cycles = 0;
    	argv[ 0 ] = "libpl.dll";
    	PL_initialise( 1, argv );
    	PlCall( "consult( 'myPrologFile.pl' )" );
    	robot->addSensorInterpTask( "UserTaskProlog", 50, &robotTaskFunc );
        }
    
        UserTaskProlog::~UserTaskProlog( )
        {
    	robot->remSensorInterpTask( &robotTaskFunc );
        }
    
        void UserTaskProlog::logTask( )
        {
    		cycles++;
    
    		tf = PL_new_term_ref( );
    		PL_put_integer( tf, 5 );
    		tx = PL_new_term_ref( );
    		goal_term = PL_new_term_ref( );
    		goal_functor = PL_new_functor( PL_new_atom( "factorial" ), 2 );
    		PL_cons_functor( goal_term, goal_functor, tf, tx );
    		int fact;
    		if ( PL_call( goal_term, NULL ) )
    		{
    			PL_get_integer( tx, &fact );
    			cout << fact << endl;
    		}
    
        }
    
    
        int main( int argc, char** argv )
        {
    	ArRobot robot;
    	ArArgumentParser argParser( &argc, argv );
    	UserTaskProlog talk( &robot );
        }
    Please ignore the code on robotics stuff. I am just wondering what the problem is and if it has anything to do with pointer memory allocation. If yes, how should I resolve it to get it working? Perhaps by using malloc or PL_malloc? Or is it to do with PL_open_foreign_frame?

    Thank you,

  13. #13
    Registered User
    Join Date
    Mar 2010
    Posts
    26

    argv Memory Allocation (updated code)

    Could someone please tell me what I am doing wrong that I get this Unhandled Exception error message:
    Code:
        0xC0000005: Access violation reading location 0x0000000c.
    with a green pointer pointing at my first Prolog line (fid_t):

    Here is my header file:
    Code:
        class UserTaskProlog
        {
          public:
                     UserTaskProlog( ArRobot* r );
                     ~UserTaskProlog( );
          protected:
                     int cycles;
                     char* argv[ 1 ];
                     term_t tf;
                     term_t tx;
                     term_t goal_term;
                     functor_t goal_functor;
                     ArRobot* robot;
                     void logTask( );
        };
    And here is my main code:
    Code:
        UserTaskProlog::UserTaskProlog( ArRobot* r ) : robot( r ), robotTaskFunc( this, &UserTaskProlog::logTask )
        {
          cycles = 0;
          argv[ 0 ] = "libpl.dll";
          argv[ 1 ] = NULL;
          PL_initialise( 1, argv );
          PlCall( "consult( 'myPrologFile.pl' )" );
          robot->addSensorInterpTask( "UserTaskProlog", 50, &robotTaskFunc );
        }
    
        UserTaskProlog::~UserTaskProlog( )
        {
          robot->remSensorInterpTask( &robotTaskFunc );
        }
    
        void UserTaskProlog::logTask( )
        {
          cycles++;
    
          fid_t fid = PL_open_foreign_frame( );
    
            tf = PL_new_term_ref( );
            PL_put_integer( tf, 5 );
            tx = PL_new_term_ref( );
            goal_term = PL_new_term_ref( );
            goal_functor = PL_new_functor( PL_new_atom( "factorial" ), 2 );
            PL_cons_functor( goal_term, goal_functor, tf, tx );
            int fact;
            if ( PL_call( goal_term, NULL ) )
            {
              PL_get_integer( tx, &fact );
              cout << fact << endl;
            }
    
          PL_discard_foreign_frame( fid );
    
        }
    
        int main( int argc, char** argv )
        {
          ArRobot robot;
          ArArgumentParser argParser( &argc, argv );
          UserTaskProlog talk( &robot );
        }
    Thank you,

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I think, unless a person who actually has this system installed miraculously appears, that we need to go back to the basics of troubleshooting here. You have a function that isn't quite working right. Don't fiddle with your actual code with the robots and the stuff. Make a completely blank project. Work with that completely blank project, only putting in what is needed to make this Prolog stuff work, until you know how this function is supposed to be used. Then you can take that knowledge and apply it to the actual code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Istream::Release access violation? [C++]
    By A10 in forum Windows Programming
    Replies: 10
    Last Post: 01-13-2009, 09:56 PM
  2. Stack trace for access violation
    By locksleyu in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2008, 02:01 PM
  3. access violation in int array
    By George2 in forum C Programming
    Replies: 2
    Last Post: 08-02-2007, 11:28 PM
  4. Help! CListCtrl access violation
    By bonkey in forum Windows Programming
    Replies: 4
    Last Post: 11-18-2003, 01:40 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21