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

    Resolved Attempted to read or write protected memory.

    Hi,

    Sure hope someone here can help me resolve this problem...

    I'm working on a test application integrated with Caliber RM using the Caiber SDK.

    I started developing this app on a 64 bit win 7 with visual studio 2008 sp1. Everything was working fine, then my HDD died and since my workplace won't support my 64bit operating system, I had to accept they installed a 32 bit win7 instead. Still running visual studio 2008 sp1.

    After this breakup I started to get this error which in my world makes absolutely no sense at all!
    Error: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

    Here is the method where the error arises.

    Code:
          public string[] GetProjectNames()
            {
                projectNames = new string[ProjectCount];
                projectID = new ProjectID[ProjectCount];
    
                int i = 0;
                foreach (Project p in session.Projects)
                {
                    projectNames[i] = p.Name; //This runs fine
                    try
                    {
                        projectID[i] = p.ProjectID; //This fails with the error
                    }
                    catch (Exception e)
                    { string error = e.Message; }
                    Project pII = p; //This runs fine(Of course this is only tranferring a stack pointer and not actually reading anything - Had to try though (I'm deperate).
                    try
                    {
                        projectID[i] = pII.ProjectID; //This fails with the error
                    }
                    catch (Exception e)
                    { string error = e.Message; }
    
                    Progress++;
                    i++;
                }
    
                return projectNames;
            }
    I really don't understand what on earth goes wrong here. Tried google - Didn't make much sense...

    I tried letting thecode run thru with try/catch in place and I get all project names but nothing from project ID. Seriously! This is the same freaking class.. Why oh why?
    Last edited by albenza; September 26th, 2011 at 06:07 AM.

  2. #2
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Attempted to read or write protected memory.

    To me it sounds like a problem with the project settings. Try creating a new project and then running the code. If it runs, you'll know that something in the project settings is causing the error (most likely somewhere in the Properties pages)
    It's not a bug, it's a feature!

  3. #3
    Join Date
    May 2007
    Posts
    1,546

    Re: Attempted to read or write protected memory.

    Are you P/Invoking native libraries anywhere in your application?
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  4. #4
    Join Date
    Sep 2011
    Posts
    4

    Re: Attempted to read or write protected memory.

    Quote Originally Posted by foamy View Post
    To me it sounds like a problem with the project settings. Try creating a new project and then running the code. If it runs, you'll know that something in the project settings is causing the error (most likely somewhere in the Properties pages)
    I tried doing that and had the same error, maybe I copy pasted to much from the old project. I'll try again...

    Not using p/invoke on any libraries

    We just tested the program on a colleagues win7 64 bit machine and it worked fine. So it does have something to do with the 32/64 bit conversion. (The build is targeted at "Any CPU" platform. Tried setting it to x86 but it made no difference)

    /Edit:
    I now tried creating the solution again on my 32 bit win 7. Had to reference the same DLL's of course and copy the code with the classes I've written, but besides that this solution now should be 100% created on a 32 bit machine. Still run into the same problem... So I must be missing something in my code somewhere.
    Last edited by albenza; September 26th, 2011 at 05:19 AM.

  5. #5
    Join Date
    Sep 2011
    Posts
    4

    Re: Attempted to read or write protected memory.

    ? Problem solved apparently.

    I deleted the reference to the caliber sdk dll and added it again, this time not from the old project but from the fresh CaliberRM Installation I got on my machine. For some reason this solved the problem.

    I guess the dll was "incoded as 64 bit" from the old machine still. Was this the way you where going Mutant_Fruit? (what a whacky name).

    This thread pointed the direction.

  6. #6
    Join Date
    May 2007
    Posts
    1,546

    Re: Attempted to read or write protected memory.

    Yeah. This kind of error is generally because there's a 32bit/64bit mismatch when using native libraries. My guess is the caliber SDK reference you had was trying to execute a 64bit native library so when you copied/pasted to a 32bit os, it just started breaking. Glad ya got it solved anyway.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  7. #7
    Join Date
    Sep 2011
    Posts
    4

    Resolved Re: Attempted to read or write protected memory.

    Nice to know for future errors of this kind..

    Thanks for your help guys, both of you.

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