C++ Read access violation when accessing private data member
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: C++ Read access violation when accessing private data member

  1. #1
    Join Date
    Jun 2017
    Posts
    2

    C++ Read access violation when accessing private data member

    Hello, so I am trying to make a member function that returns the int processTime of an event. The class for the Event class is:
    Code:
    class EVENT {
    	private:
    		string device;
    		int  type;
    		int  processTime;
    	public:
    		EVENT(string n, int t, int pt);
    		void display();
    		int  getprocessTime();
    };
    and the member function (called getProcessTime() plus the exception being thrown (I also put in the constructor for events) are:
    Code:
    EVENT::EVENT(string n, int v, int pt)
    {
    	device = n;
    	type = v;
    	processTime = pt;
    }
    
    int EVENT::getprocessTime() {
    	return processTime;
    }
    The error:
    Exception thrown: read access violation.
    this was 0xCDCDCDCD.


    Why is a read access violation being thrown? processTime is made in the event class, so shouldn't it be accessible outside? What should I do differently to make it be accessible by getProcessTime(); i.e add the -> operator.
    Last edited by 2kaud; June 12th, 2017 at 12:57 PM. Reason: Added code tags

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,940

    Re: C++ Read access violation when accessing private data member

    You might want to post the code causing the access violation...
    Victor Nijegorodov

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,940

    Re: C++ Read access violation when accessing private data member

    Quote Originally Posted by robin0 View Post
    ...
    The error:
    Exception thrown: read access violation.
    this wasv.


    Why is a read access violation being thrown?
    The value 0xCDCDCDCD means that memory was allocated via malloc or new but never written by the application. See http://www.codeguru.com/cpp/w-p/win3...Management.htm
    Victor Nijegorodov

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,571

    Re: C++ Read access violation when accessing private data member

    Code:
    // 140.cpp
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    class EVENT {
        private:
            string device;
            int  type;
            int  processTime;
        public:
            EVENT(string n, int t, int pt);
            void display();
            int  getprocessTime();
    };
    
    EVENT::EVENT(string n, int v, int pt)
    {
        device = n;
        type = v;
        processTime = pt;
    }
    
    int EVENT::getprocessTime() {
        return processTime;
    }
    
    int main()
    {
        EVENT ev("some string", 1, 2);
    
        cout << ev.getprocessTime() << endl;
    
        return 0;
    }
    Code:
    J:\Temp\140>cl 140.cpp /EHsc
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    140.cpp
    Microsoft (R) Incremental Linker Version 10.00.40219.01
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    /out:140.exe
    140.obj
    
    J:\Temp\140>140.exe
    
    2
    Everything works just fine.
    Last edited by Igor Vartanov; June 20th, 2017 at 03:00 AM.
    Best regards,
    Igor

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)