1 Replies - 796 Views - Last Post: 14 February 2012 - 10:39 AM Rate Topic: -----

#1 boris90  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 15-November 09

Access violation error, VS2010 (C++)

Posted 14 February 2012 - 10:05 AM

Hi,

I have a problem here. I used the code from this Wikipedia article:Virtual function to implement it inside my code. I've put the code parts everywhere it should belong and it worked just fine the first time I ran (compiled) the code. The output was as expected, outputting this:

I do some stuff
I do homework!
I mark the homework!
I question the student!



I then went to the line:

virtual void action()


and removed the
virtual
word. I ran the code again and it, again, worked as expected outputting this:

I eat like a generic animal.
I eat like a generic animal.
I eat like a generic animal.
I eat like a generic animal.
I eat like a generic animal.


After this, I added
virtual
back in front of the
void action()
part, and ran the code again. It showed me the error:

Unhandled exception at 0x774215de in CS323 PZ 780.exe: 0xC0000005: Access violation


Now, whenever I restart Visual Studio 2010 and rerun the code, it results in the same error.

What seems to be the problem, and what should I do to prevent it? Where should I write what. :helpsmilie:

Here's my existing code:

Main.cpp
#include <iostream>
#include <conio.h>
#include <memory>
#include <vector>
#include "Person.h"
#include "Student.h"
#include "Professor.h"
#include "Assistant.h"
using namespace std;

void main()
{

	std::vector<std::unique_ptr<Person>> people;

	people.push_back(std::unique_ptr<Person>(new Person));
	people.push_back(std::unique_ptr<Person>(new Student));
	people.push_back(std::unique_ptr<Person>(new Assistant));
	people.push_back(std::unique_ptr<Person>(new Professor));

	for (auto it = people.begin(); it != people.end(); ++it) {
		(*it)->action();
	}

	people.clear();

	getch();
}


Person.h
#pragma once
#include <iostream>
using namespace std;

class Person
{
public:
	// Constructor
	Person(void);

	// Destructor
	virtual ~Person(void);

	// A person characteristic function
	virtual void action()
	{
		cout << "I do some stuff";
		cout << endl;
	}

	long long personId;
	char* name;
	char* title;
};


Student.h
#pragma once
#include <iostream>
#include "person.h"
using namespace std;

class Student :
	public Person
{
public:
	// Constructor
	Student(void);

	// Destructor
	~Student(void);

	// A student characteristic function
	void action()
	{
		cout << "I do homework!";
		cout << endl;
	}
};


Assistant.h
#pragma once
#include "person.h"

class Assistant :
	public Person
{
public:
	Assistant(void);
	~Assistant(void);

	// A student characteristic function
	void action()
	{
		cout << "I mark the homework!";
		cout << endl;
	}
};


Professor.h
#pragma once
#include "person.h"

class Professor :
	public Person
{
public:
	Professor(void);
	~Professor(void);

	// A student characteristic function
	void action()
	{
		cout << "I question the student!";
		cout << endl;
	}
};


Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Access violation error, VS2010 (C++)

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Access violation error, VS2010 (C++)

Posted 14 February 2012 - 10:39 AM

std::unique_ptr<Person>(new Person)


std::unique_ptr deletes it's contents as soon as it's destructor is called. in this case as soon as you push a value onto the vector a copy is made then the original is deleted so your pushing a deallocated value onto the vector.

maybe you want auto_ptr? auto_ptr transfers ownership on copy.

smart_ptr is your other option. it handles almost everything. the exception being when a smart_ptr goes out of scope without it's destructor being called; this can happen if you self referential classes;

we have a nice tutorial on C++11 memory management here

This post has been edited by ishkabible: 14 February 2012 - 10:45 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1