Click to See Complete Forum and Search --> : Attempted to read or write protected memory. This is often an indication that other m
abhinav0112
August 20th, 2008, 04:45 PM
Hi .... I am getting the error "Attempted to read or write protected memory. This is often an indication that other memory is corrupt". This is basically happening when i use threads in my application. When i remove the Thread invocation and use the function without the threading, it works fine.
Some background on the Project: It is basically a CAD simulation project using API of a software. The error basically happens at the API's function call. But works fine if i dont use any threads. But then if the part that is executing the API runs in a seperate thread, i get this error.
I tried to search a lot on this issue, and people have given some solutions which have worked for them but isn't working in my case.
Please let me know if you have any ideas on how to go about it.
Thanks. :)
BigEd781
August 20th, 2008, 04:50 PM
You are most likely misusing the API in some way. This can easily occur and is not always easy to track down. You would do better to ask a more specific question about how to use the API. You could also simply be modifying a value in one thread before you attempt to use it in another.
abhinav0112
August 20th, 2008, 05:06 PM
@BigEd781
Thanks for the quick reply .... While i also thought that it probably has got to do something with misuing the API, but then it works fine without using threads, and that is the reason i am lil skeptical on that.
Also the problem of modifying value before using it in another is probably not something that is happening because, There is a lot of processing that goes on in a praticular function and the app in the mean time becomes unresponsive ... so the only reason for the thread was so as to make the application not show "Not Responding" while it is doin its stuff. But there is nuthing else happening simultaneously ...
what i am not sure of though is that, We need to get the application session for using the application's API. Which happens at the form load in the Base program thread, And then when i use the API in a new thread, the error pops up but if it is w/o the new thread it works fine. Could this somehow cause any problems. I mean i know its difficult to tell without knowing the API details ... but in a general scenario would this be something to be worried about .. ?
BigEd781
August 20th, 2008, 05:14 PM
It's hard to answer without knowing exactly what is going on, but I once tried something very similar and got the same result. The cause of the problem in that case was that the interface I was using only runs on the main GUI thread.
I'm sorry that this is not more helpful. Perhaps someone more experienced can give us some more insight. OUt of curiosity, what is the name/vendor of the API that you are using?
abhinav0112
August 20th, 2008, 05:26 PM
hmm ... probably it could be something similar here ...
Thanks for the help anyways ... Really appreciate it. :)
btw the software is NX5.0 by UGS ...
If anyone else has any insight into similar situations, the help would be greatly appreciated ...
Arjay
August 20th, 2008, 07:51 PM
If anyone else has any insight into similar situations, the help would be greatly appreciated ...The issue is that any resources that are shared between threads must be synchronized. By resources, I'm talking about strings, structures, files, classes, etc. Is the NX api thread safe? If not, and you are using it across different threads, then you have the possibility of memory corruption. Read the api docs regarding thread safety. If it isn't thread safe, then you'll need provide synchronization yourself with a critical section or mutex.
jusstujoo
August 21st, 2008, 04:03 AM
Can you share some code for us to look into? This may help us track down the correct source of problem.
I am assuming that you're using .NET/C#.
abhinav0112
August 21st, 2008, 06:16 PM
@Arjay
I am not too sure about the thread safety of the NX api ... will check it out. Its not mentioned clearly anywhere though. But like i said when the thread is in progress nuthin else is accessing the variables. The thread is basically being used for making the application more responsive.
@jusstujoo
I am really sorry but i will not be able to share the code out. But its really nuthin much. The outline structure is
1. There are 2 classes. One the Base form class and another class.
2. On form load, the app pulls out the session details using the API and those variables are later used to perform any actions with the API.
3. I run a loop to read data from file and reposition the CAD assembly parts.
3a. This loop iterates a number of times to simulate the data. it takes about 2- 3 minutes to complete the simulation.
3b. Things are fine so far.
3c. The moment i pull out the loop and its contents and try to run them in a thread, it breaks at the instance where the the API is being called, right from the very first iteration of the loop.
Arjay
August 21st, 2008, 07:30 PM
If you declare an instance of the api in one thread (like the form thread) and access in another thread, that may be enough to break the app if the api isn't thread safe.
jusstujoo
August 24th, 2008, 11:36 PM
Well, I'm sorry didn't get all of it, but can you try in your Form's constructor,
CheckForIllegalCrossThreadCalls = false;
Can you also, provide us with the message in exception?
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.