I'm in the final process of developing a program and I came across an error, I spent some time researching and could not solve the problem.
About the program:
In short, the program receives and writes values in another program (Aspen HYSYS) and communicates with OPC servers by writing and receiving values.
Some details to clarify further:
To update the values in the main form, I created 3 threads, one receives the values from the server and writes in the program, one takes the values of the program and writes to the server and the other has the beginning of the first, as a secondary thread you can not change anything in the forms interface, each thread makes an invoke that calls a delegate to update the values in the program.
To communicate with OPC servers, I use two Softing DLLs.
My problem:
When I give the command to update the values, the following error appears: "Attempt to read or write protected memory. This is often an idication that other memory is corrupt." From what I've researched, it's as if I've been trying to write in a memory space that has already been deallocated. Explicitly, I did not move anything, but some Softing method may have deallocated or deleted the reference without my knowing.
The error does not occur on a specific line, it varies between read and write methods on the server.
Code snippet where the error occurs:
if (opcform.plcopc.URL == null)
{
opcform.plcopc.URL = opcform.plcopc.getURL(opcform.GridExport.Rows[i].Cells[6].Value.ToString(),opcform.GridExport.Rows[i]Cells[7].Value.ToString());
//o método acima apenas pega a URL do servidor
opcform.plcopc.m_daSession = new DaSession(opcform.plcopc.URL);<<<<<<<<<<<<
//instancia uma sessão no servidor com a URL do mesmo
opcform.plcopc.m_daSubscription = new DaSubscription(500, opcform.plcopc.m_daSession); <<<<<<<<<
//instancia um objeto necessário para fazer as leituras e escritas de forma síncrona e adiciona ele a sessão
}
opcform.plcopc.m_daItem = new DaItem(opcform.GridExport.Rows[i].Cells[8].Value.ToString(), opcform.plcopc.m_daSubscription); <<<<<<<<<<<
opcform.plcopc.exOptions.ExecutionType = EnumExecutionType.SYNCHRONOUS;
//seta que a comunicação vai ser síncrona
if (opcform.plcopc.m_daSession.CurrentState != EnumObjectState.CONNECTED)
{
opcform.plcopc.m_daSession.Connect(true, true, opcform.plcopc.exOptions);
//conecta com o servidor
}
int retorno;
double vl = Convert.ToDouble(opcform.GridExport.Rows[i].Cells[4].Value);
//pega o valor que vai ser escrito no servidor
ValueQT vt = new ValueQT(vl, EnumQuality.GOOD, DateTime.Now);
//o objeto que vai ser escrito no servidor
opcform.plcopc.m_daItem.Write(vt, out retorno, opcform.plcopc.exOptions);<<<<<<
//escrevendo o objeto no servidor
Attempts:
Well, as the problem is many lines, I could not think of much, I tried to lock some objects, but other errors appeared.
My order
Well, can someone tell me if I have "lock" the reference so that only I can delete it? or how can I monitor to see where it is deleted?
Please respond in theory, you do not have to type thousands of lines of code and end up wasting your time. Materials for me to look for solution will be very welcome.