Access violation error while running systemC code

hello!I'm trying to do systemc-ams modeling for developing embedded system.I had written code for different module like mixer,adc,filter,etc.using Microsoft visual studio 2010.While I build the code it gets build successfully but on running I get a pop-up saying "Unhandled exception at 0x004f6fef in adc_module.exe: 0xC0000005: Access violation reading location 0x00000000".So, pls. anybody help me out why I'm getting such an error.
Below is my source code for systemC module.
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<systemc-ams.h>
#include<systemc.h>

const double PI=3.1415;
 
 int sc_main(int argc, char *argv[])
{   
	sca_tdf::sca_in<double>rf, osc;
    sc_core::sc_out<sc_int<8>>if_d;

	sc_trace_file *wf = sc_create_vcd_trace_file("wave");
	sc_trace(wf,rf,"rf");
	sc_trace(wf,osc,"osc");
	sc_trace(wf,if_d,"if_d");
	sc_start(1.0,SC_MS);
	getchar();
	sc_close_vcd_trace_file(wf);
    return 0;
}

SCA_TDF_MODULE(mixer) {
// TDF input and output ports
sca_tdf::sca_in<double> in1, in2;
sca_tdf::sca_out<double> out;
// TDF functions
void set_attributes() {
set_timestep(1.0, SC_US); }
void processing() {
out.write(in1.read() * in2.read()); }
// constructor
SCA_CTOR(mixer) {}
};

SCA_TDF_MODULE(lp_filter_ltf_ctrl) {
sca_tdf::sca_in<double> in; // input
sca_tdf::sca_out<double> out; // output
sca_tdf::sc_in<bool> ctrl; // gain ctrl
sca_tdf::sca_ltf_nd ltf; // LTF object
sca_util::sca_vector<double> num, den;

void initialize() {
num(0) = 1.0;
den(0) = 1.0; den(1) = 1.0/(2.0*PI*1.0e4);}
void processing() {
double tf = ltf(num, den, in.read());
if (ctrl.read()) out.write(2.0*tf);
else out.write(1.0*tf);
}
SCA_CTOR(lp_filter_ltf_ctrl) 
{}
};

SCA_TDF_MODULE(ad_converter) {
sca_tdf::sca_in<double> in;
sca_tdf::sc_out<sc_int<8> > out; // DE output
void processing() {
out.write(sc_int<8>(in.read())); }
SCA_CTOR(ad_converter) {}
};

SC_MODULE(front_end) {

sca_tdf::sca_in<double> in,in1,in2,rf, osc;
sca_tdf::sca_out<double> out;
sc_core::sc_out<sc_int<8>> if_d;
sca_tdf::sca_signal<double> if_mxr, if_lpf;

mixer* mxr;
lp_filter_ltf_ctrl* lpf;
ad_converter* adc;

SC_CTOR(front_end)
{
mxr = new mixer("mxr");
mxr->in1(rf),in2(osc),out(if_mxr);
lpf = new lp_filter_ltf_ctrl("lpf");
lpf->in(if_mxr);
lpf->out(if_lpf);
adc = new ad_converter("adc");
adc->in(if_lpf);
adc->out(if_d);
}
};
Unhandled exception at 0x004f6fef in adc_module.exe: 0xC0000005: Access violation reading location 0x00000000".

Not familiar with that library, however, trying to read location 0x00000000 is generally the result of an uninitialized pointer.

Run your code in debug mode. When you get the unhandled exception, VS should show you a stack trace of the function calls currently on the stack. You may be library code when the exception occurs, so follow the stack trace back to the last function of yours on the stack. By clicking on that entry on the stack trace, VS will display the source line that was active at the time. By looking at the locals used in that statement, you should be able to identify the pointer which is uninitialized.

I'm running program in debug mode only.How to go to the line giving an error of unhandeld exception.I can see a yellow pointer pointing on the particular line of file "ostream.dll".But how will come to know where is the actual null pointer in my source code.
You should have a stack trace window open. That window should have an arrow to the ostream function where the exception occurred. As I said in my previous post, follow the trace stack back until you get to one of your functions. Click on that line in the stack trace. The source window should change to show the current line in that function. Now switch to the autos tab. Any pointers being used in the current source line should appear in the autos tab.
Sir,I'm trying to trace out the line by stack back but, it is showing exception in ostream function only.How, will I get into my function for correcting error.
It does not sound like you're looking at the stack trace window.
Can you post the contents of the stack trace window?
Last edited on
Sure sir, here are my contents of stack trace window upon exception handle error.....

[code]
{ // insert a character
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);

# if (!_Ok)
_State |= ios_base::badbit;
else
{ // state okay, insert character
_TRY_IO_BEGIN
if (_Traits::eq_int_type(_Traits::eof(),
_Myios::rdbuf()->sputc(_Ch)))
_State |= ios_base::badbit;
_CATCH_IO_END
}

_Myios::setstate(_State);
return (*this);
}
[\code]

I'm getting "Access violation error at the line marked with # sign" and null pointer is acknowledged at the previous line.
Last edited on
@AbstractionAnon
If an exception occurs in an external modul such as a the 'ostream.dll' the debugger might not be able to show the trace.

@kaustubh90
To pin down the line where the problem happens you might use debug output. if necessary in front of each line.

By the way: It's not an uninitialized pointer, it's a null pointer. Which likely means that you forgot to provide some required data.
That's not the stack trace window. That's the source window.

You should see a tabbed window at the bottom of your screen. One tab should be labeled "Call Stack". When I just forced an unhandled exception, the "Call Stack" tab appeared automatically at the bottom of the screen.

I'm getting "Access violation error at the line marked with # sign"

sentry is a class used to check that the good bit is set on the ostream.
Since you're getting an exception here, I would suspect that you're using a pointer to an ostream, and that pointer is NULL. i.e. ostream's this pointer is NULL.
Without changing scope, try displaying the this pointer.

@coder777 - As long as the stack is not corrupt, the debugger should be able to show all the functions on the stack. Any functions that are in system libraries for which debugging information is not available will be grayed out (at least in VS).

And yes, you're right. I meant to say a NULL pointer. Thanks for the correction.
Last edited on
sorry! I put the source code instead of stack trace window.But here are my contents of stack window

>> adc_module.exe!std::basic_ostream<char,std::char_traits<char> >::put(char _Ch) Line 529 + 0xf bytes C++

adc_module.exe!std::endl(std::basic_ostream<char,std::char_traits<char> > & _Ostr) Line 1006 C++

adc_module.exe!std::basic_ostream<char,std::char_traits<char> >::operator<<(std::basic_ostream<char,std::char_traits<char> > & (std::basic_ostream<char,std::char_traits<char> > &)* _Pfn) Line 189 + 0x9 bytes C++

adc_module.exe!sc_core::pln() Line 106 C++
adc_module.exe!sc_elab_and_sim(int argc, char * * argv) Line 80 C++
adc_module.exe!main(int argc, char * * argv) Line 34 + 0xd bytes C++
adc_module.exe!__tmainCRTStartup() Line 278 + 0x19 bytes C
adc_module.exe!mainCRTStartup() Line 189 C
kernel32.dll!@BaseThreadInitThunk@12() + 0x12 bytes
ntdll.dll!___RtlUserThreadStart@8() + 0x27 bytes
ntdll.dll!__RtlUserThreadStart@8() + 0x1b bytes

Above are contents of stack window and error occured at first line marked with "<<"
While in ostream::put, check the this pointer as I suggested above.

adc_module.exe!sc_core::pln() Line 106 C++

This is your last function on the stack. Double click on that line. The source window should change to the sc_core::pln() function. You should see something similar to this (very simplified):
103
104
105
106
  ostream * os = NULL;  // Somewhere.  Maybe passed in.  Maybe in class.
  char c = ' ';
...  
  os->put (c);  // Line 106: Your problem should be here.  os is NULL 

Last edited on
Sir, upon double clicking above mentioned function pointer jumps to another file named "sc_ver.cpp" and there is no such NULL assigned. The code in sc_ver.cpp is as follows :

[code]
{
static const char indent[] = " ";
std::string line;
std::stringstream copyright;

// temporary stream to print copyright line-wise with indentation
copyright << sc_copyright();

cerr << endl;
# cerr << indent << sc_version() << endl;
while( getline( copyright, line ) )
cerr << indent << line << endl;

// regressions check point

if( getenv( "SYSTEMC_REGRESSION" ) != 0 ) {
cerr << "SystemC Simulation" << endl;
}

lnp = true;
}
[\code]

and the pointer is at line "cerr << indent << sc_version() << endl;"
That's very strange. cerr should always be defined and available under normal circumstances.

The fact that sentry is failing in ostream::put() implies the cerr file is not open, is bad, or is corrupt. There are ways to turn off standard C files at build time, but I assume you're not doing that.

The only thing I can think of is that you've overwriting the run-time's pointer to cerr, but I think that is unlikely.

I would suggest putting cerr statements at various points up until this function is called, including first thing in main. That should narrow down where cerr is getting clobbered.
If a cerr statement that is first thing in main fails, there is something wrong with your runtime environment.

I've asked you twice to display this inside ostream::put(). Still waiting for that information.

BTW, your closing code tag should have a forward slash (/), not a backslash.

Sir, but this all are system files how can I make changes to it.I can make changes in my own systemc code for "adc_module.exe".But how will I come to know which line is throwing an error.
The pointer is pointing errror inside ostream file and not adc_module.Just want to know how can I reach to my code for identifying error.
Since you can display the source for sc_ver.cpp, you must have that source on your system.
Presumably you can change that and recompile it.

Before you do that though, change line 7 of the code you posted above to do a cerr.
 
  cerr << "entering sc_main" << endl;


If you get that message on the console, you know cerr is okay at that point. If that statement traps, then you know your runtime environment is not correct at the start of the program.

Topic archived. No new replies allowed.