C++ Virtual Function Causing Exception

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am getting an exception when calling pure virtual functions.

Using - Atmel Studio 6 (Version: 6.2.1153 - )

1. Created a new C++ project,
2. Used the Board Wizard (EVK1100) to add ASF
3. Checked Linker/General -nostartfiles so it would compile
4. Removed Optimisation (-O0)

Header: "AtmelTest.h"

class interface
{
	public:
	virtual void virtual_function() = 0;
};

class base : virtual public interface
{
	public:
	unsigned char function();
};

class derived : virtual public interface, public base
{
	public:
	void virtual_function();
};

Source: "AtmelTest.cpp"

extern "C"
{
	#include 
}

#include "AtmelTest.h"

derived object;

int main(void)
{
	board_init();
	
	object.function();
}

void derived::virtual_function()
{

}

unsigned char base::function()
{	
	object.virtual_function(); // No Exception
	virtual_function(); // Exception

	return 0;
}

When calling this->virtual_function I get a Bus Error Data Fetch Exception

Can someone please help, Not sure what I am doing wrong

This topic has a solution.
Last Edited: Tue. May 3, 2016 - 08:28 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Solved it, was a bad idea to check -nostartfiles
Deleted the ASF startup_uc3.S instead, not sure what this will affect but seems to be working so far.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

was a bad idea to check -nostartfiles

Not sure about the AVR32's, but for the 8-bitters using avr-gcc this would mean that no constructors will be run for statically allocated objects. Possibly some similar situation might be present re vtables, and since you're using virtual functions that would spell disaster.

P.S. Don't forget to make any destructors virtual! :wink:

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Actually I think you might be right, I have had an issue that had me questioning constructors.

Can't remember if it was static or global, I assume global variables will be the same.

Yip you are right about the vtable out of interest I checked it in debug, they are filled in now pretty sure they were 0 previously.

That startup file must have debug code in it because now that I have deleted it breakpoints don't break when -nostartfiles is checked, using JTAGICE mkII.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

savman wrote:

I am getting an exception when calling pure virtual functions.

Using - Atmel Studio 6 (Version: 6.2.1153 - )

1. Created a new C++ project
2. Used the Board Wizard (EVK1100) to add ASF
3. Checked Linker/General -nostartfiles so it would compile
4. Removed Optimisation (-O0)

 

Header: "AtmelTest.h" Code:

class interface
{
public:
virtual void virtual_function() = 0;
};
class base : virtual public interface
{
public:
unsigned char function();
};
class derived : virtual public interface, public base
{
public:
void virtual_function();
};
[/code:1]
Source: "AtmelTest.cpp"
[code:1]
extern "C"
{
#include
}
#include "AtmelTest.h"
derived object;
int main(void)
{
board_init();
object.function();
}
void derived::virtual_function()
{
}
unsigned char base::function()
{
object.virtual_function(); // No Exception
virtual_function(); // Exception
return 0;
}

 

When calling this->virtual_function I get a Bus Error Data Fetch Exception

Can someone please help, Not sure what I am doing wrong

 

This is a test.  I am quote a post with code but I have not modified the quoted content.  This test is being conducted after we removed access to the plain text editor and, thusly, the use of BBcodes.

Last Edited: Fri. Sep 11, 2015 - 09:58 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

savman wrote:

I am getting an exception when calling pure virtual functions.

Using - Atmel Studio 6 (Version: 6.2.1153 - )

1. Created a new C++ project,

2. Used the Board Wizard (EVK1100) to add ASF

3. Checked Linker/General -nostartfiles so it would compile

4. Removed Optimisation (-O0)

Header: "AtmelTest.h"

Code:

class interface

{

public:

virtual void virtual_function() = 0;

};

 

class base : virtual public interface

{

public:

unsigned char function();

};

class derived : virtual public interface, public base

{

public:

void virtual_function();

};

[/code:1]

Source: "AtmelTest.cpp"

[code:1]

extern "C"

{

#include

}

#include "AtmelTest.h"

derived object;

int main(void)

{

board_init();

object.function();

}

void derived::virtual_function()

{

}

unsigned char base::function()

{

object.virtual_function(); // No Exception

virtual_function(); // Exception

return 0;

}

When calling this->virtual_function I get a Bus Error Data Fetch Exception

Can someone please help, Not sure what I am doing wrong

 

I am quoting the first post again and will go back and edit the post after submitting it.

 

The quoted content is fine if the new site saves the content in its native format.  Going back and editing the post (which I'm doing now) shows that the quoted content appears fine in the editor.

Please Read: Code of Conduct

Documentation: Atmel Community User Guide (work-in-progress)

Site Updates: Changelog

Last Edited: Fri. Aug 1, 2014 - 09:45 PM