CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2008
    Posts
    4

    Attempted to read or write protected memory. This is often an indication that other m

    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.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Attempted to read or write protected memory. This is often an indication that other m

    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.

  3. #3
    Join Date
    Aug 2008
    Posts
    4

    Re: Attempted to read or write protected memory. This is often an indication that other m

    @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 .. ?

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Attempted to read or write protected memory. This is often an indication that other m

    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?

  5. #5
    Join Date
    Aug 2008
    Posts
    4

    Re: Attempted to read or write protected memory. This is often an indication that other m

    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 ...

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Attempted to read or write protected memory. This is often an indication that other m

    Quote Originally Posted by abhinav0112
    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.

  7. #7
    Join Date
    Apr 2001
    Location
    Karachi, Pakistan.
    Posts
    466

    Re: Attempted to read or write protected memory. This is often an indication that other m

    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#.
    KMan
    use code-tags
    Rating a post is'nt injurious(0:


    http://izlooite.blogspot.com

  8. #8
    Join Date
    Aug 2008
    Posts
    4

    Re: Attempted to read or write protected memory. This is often an indication that other m

    @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.

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Attempted to read or write protected memory. This is often an indication that other m

    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.

  10. #10
    Join Date
    Apr 2001
    Location
    Karachi, Pakistan.
    Posts
    466

    Re: Attempted to read or write protected memory. This is often an indication that other m

    Well, I'm sorry didn't get all of it, but can you try in your Form's constructor,

    Code:
    CheckForIllegalCrossThreadCalls = false;
    Can you also, provide us with the message in exception?
    Last edited by jusstujoo; August 24th, 2008 at 11:41 PM.
    KMan
    use code-tags
    Rating a post is'nt injurious(0:


    http://izlooite.blogspot.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured