Exception thrown: Read access violation

Hi, I am working on an assignment and there seems to no error in my code but when debugging, I get the error

Exception thrown: read access violation.
_Other was 0x20.

Any hint on how to find the mistake will be appreciated. I cantupload the all the 9 files

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#pragma once
#include <memory>
#include "ItemSpec.h"
#include <string>

class Item {
public:
	Item() = default;

	Item(std::string name, std::shared_ptr<const ItemSpec> spec) : _name{ name } {
		set_specification(spec);
	}

	std::string const get_name() const { return _name; }

	std::shared_ptr<const ItemSpec> get_specification() const { return _spec; }

	virtual void send_to(std::ostream & os) const { /*BLABLABLA*/ }

protected:
	void set_specification(std::shared_ptr<const ItemSpec> spec) { _spec = spec; }
	void set_name(std::string name) { _name = name; }
private:
	std::string _name;
	std::shared_ptr<const ItemSpec> _spec;
};

std::ostream & operator<<(std::ostream & os, const Item & item)
{
	item.send_to(os);
	return os;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma once
#include <memory>
#include <string>

class ItemSpec {
public:
	enum class continent { ANY, Africa, Europe, Asia, Australia, Eurasia, North_America, Latin_America };
	ItemSpec() : _country(""), _rating(0), _continent(ItemSpec::continent::ANY) { }
	ItemSpec(std::string const& country, ItemSpec::continent continent, int rating);
	ItemSpec(ItemSpec::continent continent) : _country(""), _rating(0), _continent(continent) { }
	virtual bool matches(const ItemSpec & item_spec) const { return true; }
	std::string get_country() const { return _country; }
	continent get_Continent() const { return _continent; }
	int get_rating() const { return _rating; }
	virtual bool get_visited() const { return false; }
	virtual float get_height() const { return 0; }
	virtual void send_to(std::ostream& os) const { /* Does nothing */ }
	std::string get_continent() const
	{
		const char *mode[] = { "ANY", "Africa", "Europe", "Asia", "Australia", "Eurasia", "North_America", "South_America" };
		return mode[static_cast<size_t>(_continent)];
	}

protected:
	std::string _country;
	continent _continent;
	int _rating;
};

//std::ostream& operator<<(std::ostream& os, ItemSpec::continent t);
std::ostream & operator<<(std::ostream & os, ItemSpec::continent t)
{
	switch (t)
	{
	case ItemSpec::continent::Africa: os << "Africa"; break;
	case ItemSpec::continent::Asia: os << "Asia"; break;
	case ItemSpec::continent::Australia: os << "Australia"; break;
	case ItemSpec::continent::Eurasia: os << "Eurasia"; break;
	case ItemSpec::continent::Europe: os << "Europe"; break;
	case ItemSpec::continent::Latin_America: os << "South America"; break;
	case ItemSpec::continent::North_America: os << "North America"; break;
	default: os << "Unknown"; break;
	}
	return os;
}
//std::ostream & operator<<(std::ostream & os, const TownSpec & spec);

std::ostream & operator<<(std::ostream & os, const ItemSpec & spec)
{
	spec.send_to(os);
	return os;
}
ItemSpec::ItemSpec(std::string const& country, ItemSpec::continent continent, int rating)
{
	_country = country;
	_continent = continent;
	_rating = rating;
}
Last edited on
run through a debugger, when it crashes it should point it to the live of code that caused the exception.
Perform a backtrace to know how you reach it.

may also use valgrind to catch some bad memory access.


> I cantupload the all the 9 files
¿what's your excuse?
https://github.com/
De donde eres? I cant upload all the 9 files because of the character limit on this forum. Unless I upload them as comments. I'll download valgrind and see if I can find the memory access problem.
Any other idea??
That's why I suggested github. Or zip the sources and upload that somewhere.
I don't want to have to reproduce your file structure.

Ideally, you should be able to make a minimal testcase, removing all that is not needed to reproduce your issue. The backtrace may help you with that.


> De donde eres?
From a land where you need to start questions with a ¿
The soil have turned red from the blood spilled because of discussions on that matter. ;)
Exception thrown: read access violation.
_Other was 0x20.


Any time you see a crash with a really small address, it's likely that you're passing a null pointer to something. In this case, maybe you're passing a null pointer to an object, and then trying to access a member that's 0x20 bytes into it.

Hope this helps.
Topic archived. No new replies allowed.