Menu

C# error "Attempted to read or write protected memory." while setting...

Yolan
2013-03-21
2013-04-26
  • Yolan

    Yolan - 2013-03-21

    Hi guys,

    I get an exception while trying to set the PortId of a SerialDriver multiple times.
    My function searches for a modem and so far it works. But when I use it 5/6 times in a row, I get this error :

    System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
    at Com.Coronis.Sdk.Serialdriver.Rs232driver.Wrapper.RS232Wrapper.SetPortId(Int32 driver, String Id)
    at Com.Coronis.Sdk.Serialdriver.Rs232driver.RS232Driver.SetSerialPortId(String serialPortId)
    at environnementTest.Program.Main(String[] args) in Test\Program.cs:line 45

    It seems to work fine in C++ so I'm assuming it comes from the wrapper.
    Here is the simplified function :

    for( int i=1 ; i<20 ; i++ )
    {
        try
        {
            String portId = "COM" + i.ToString()    ;
            serialDriver.SetSerialPortId( portId )  ;
            protocol.SetSerialDriverInstance( serialDriver ) ;
            protocol.Init() ;
            protocol.Open() ;
    
            LocalNodeServiceCommand localService = new LocalNodeServiceCommand( NodeServiceId.STACK_SERVICE, new Payload("5005") ) ;
            LocalNodeServiceResponse response = protocol.RequestLocalNodeService( localService );
            if( response.Status == ResponseStatus.OK_PROTOCOL_SUCCESS )
            {
                Console.Out.WriteLine( portId + " modem trouvé : " + response.Command.ToString() ) ;
            }
            else
            {
                Console.Out.WriteLine( portId + " modem PAS trouvé " ) ;
            }
            protocol.Close()    ;
        }
        catch( Exception ex )
        {
            if( ex is ProtocolException )
            {
            }
            else
            {
                protocol.Close()    ;
                serialDriver.Close()    ;
                serialDriver = new RS232Driver()    ;
                protocol = new Waveport()           ;
            }
        }
    }
    

    Thanks for reading !

    Yolan

     
  • Thierry CHOMAUD

    Thierry CHOMAUD - 2013-03-21

    Hye,
    Because in our C implementation, we use a loading system for instance, we have to reserved memory for each future possible instance. To limit default memory reservation, we have limited the number of instance to 5.

    So if you try to create more than 5 instances, you will have this type of error.
    I see in your extracted code that in case of Exception (expect for ProtocolException) you recreate an new instance. It could be the cause of your pb (because of the wrapping, if we don't explicitely destroy the instance, the C associated instance will not be free).
    Could you please add following console output in exception part and return to us the console output:

            if (ex is ProtocolException)
                    {
                        Console.Out.WriteLine("ProtocolException:" + ex.ToString());
                    }
                    else
                    {
                        Console.Out.WriteLine("Other Exception: "+ex.ToString());
                        wp.Close();
                        dr.Close();
                        dr = new RS232Driver();
                        wp = new Waveport();
                    }
    

    Thierry

     

    Last edit: Thierry CHOMAUD 2013-03-21
  • Yolan

    Yolan - 2013-03-21

    I could use one instance only but when there is an "other exception", I think I have to create a new Waveport and a new RS232Driver.

    Here is the end of the result :

     config port COM19...
    protocol exception: Unknown protocol error occured with code (12) : KO_PROTOCOL_
    NODE_SERVICE_ID_REQUIRED
    
     ----------------- tentative n° 4 ---------------------
    
     config COM1...
    other exception System.ArgumentException: out of bounds capacity value (2560 byt
    es max)
       at Com.Coronis.Sdk.Domain.Payload..ctor(UInt32 length, Byte[] frame)
       at Com.Coronis.Sdk.Domain.Helper.ProtocolHelper.NativeToPayload(sPayload c_pa
    yload)
       at Com.Coronis.Sdk.Domain.Helper.ProtocolHelper.NativeToLocalNodeServiceComma
    nd(sLocalNodeServiceCommand sLocalNodeServiceCommand)
       at Com.Coronis.Sdk.Domain.Helper.ProtocolHelper.NativeToLocalNodeServiceRespo
    nse(sLocalNodeServiceResponse c_response)
       at Com.Coronis.Sdk.Protocol.Waveport.Waveport.RequestLocalNodeService(LocalNo
    deServiceCommand command)
       at Test.Program.Main(String[] args) in Program.cs:line 56
    
     config COM2...
    protocol exception: Unknown protocol error occured with code (12) : KO_PROTOCOL_
    NODE_SERVICE_ID_REQUIRED
    
     config port COM3...
    COM3 modem trouvé : 510018634B30452F;STACK_SERVICE
    
     config port COM4...
    
     

    Last edit: Yolan 2013-03-22
  • Thierry CHOMAUD

    Thierry CHOMAUD - 2013-03-21

    When you say "I think I have to create a new Waveport...", you means now you have removed these new creations & the problem is resolved, or if you not let these creations, you have another problem?
    What is connected on your COM1?

     
  • Yolan

    Yolan - 2013-03-22

    No, I think I need, I must create a new instance. After the "other exception", I can't use anymore my objects Protocol and SerialDriver. Maybe there is another way ?

    No devices are connected to my computer, besides the waveport (and my keyboard/mouse).

     
  • Thierry CHOMAUD

    Thierry CHOMAUD - 2013-04-26

    For getting available port Ids, we suggest to you to use the new added method GetSerialPortIds in C# Domain (see closed ticket #18 in version 2.1.0)

     

Log in to post a comment.

MongoDB Logo MongoDB