Jump to content
  • Advertisement
Sign in to follow this  
Enigma

Access Violation under one compiler only - compiler bug?

This topic is 4745 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In a couple of recent projects I've found I've been getting access violations when I throw an exception under certain circumstances, but only under my Borland compiler. When I compile with gcc or MSVC++ the access violation does not occur and the exception propogates as expected. Both times I've carefully examined the source and have been unable to find any reason for the access violation. I've finally narrowed it down to this test case:
#include <iostream>
#include <map>
#include <string>

class Thing
{

	public:

		std::string string() const
		{
			return "Hello\n";
		}

};

class Test
{

	public:

		static Thing const & func(std::string name, unsigned int size);

	private:

		static Thing createThing(std::string name, unsigned int size);

		static std::map< std::pair< std::string, unsigned int >, Thing > things_;

};

Thing const & Test::func(std::string name, unsigned int size)
{
	std::map< std::pair< std::string, unsigned int >, Thing >::const_iterator thing = things_.find(std::make_pair(name, size));
	if (thing == things_.end())
	{
		// this line seems to cause the AV
		thing = things_.insert(std::make_pair(std::make_pair(name, size), createThing(name, size))).first;
	}
	return thing->second;
}

Thing Test::createThing(std::string name, unsigned int size)
{
	throw std::runtime_error("error");
}

std::map< std::pair< std::string, unsigned int >, Thing > Test::things_;

int main()
{
	try
	{
		std::cout << Test::func("Test", 7).string() << '\n';
	}
	catch (std::exception & exception)
	{
		std::cerr << exception.what() << '\n';
	}
	catch (...)
	{
		std::cerr << "unknown exception\n";
	}
}
If I replace the indicated line with:
Thing thing(name, size);
thing = things_.insert(std::make_pair(std::make_pair(name, size), thing)).first;
Then the access violation no longer occurs and the exception propogates to main and is caught as expected. This leads me to suspect that this is an issue with the Borland compiler and not a problem in the code itself, but it could just be that there's a subtle problem that I'm not seeing. Can anybody see any reason (baring compiler error) why the posted code would cause an access violation? Thanks, Enigma

Share this post


Link to post
Share on other sites
Advertisement
What you're probably running into is something to do with how the compiler implements argument evaluation to functions, which isn't really specified by the standard (I believe).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!