constructor_throw.cpp revision 12855:588919e0e4aa
1/*****************************************************************************
2
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements.  See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License.  You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied.  See the License for the specific language governing
16  permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21
22  test01.cpp --
23
24  Original Author: Andy Goodrich, Forte Design Systems, 14 March 2006
25
26 *****************************************************************************/
27
28/*****************************************************************************
29
30  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
31  changes you are making here.
32
33  $Log: constructor_throw.cpp,v $
34  Revision 1.1.1.1  2006/12/15 20:25:56  acg
35  systemc_tests-2.3
36
37  Revision 1.1  2006/03/15 00:12:08  acg
38   Andy Goodrich: Forte Design Systems
39   First check in.
40
41 *****************************************************************************/
42
43//  This tests a bug when an exception is thrown in
44// sc_module::sc_module() for a dynamically allocated sc_module
45//  object. We are calling sc_module::end_module() on a module that has
46// already been deleted. The scenario runs like this:
47//
48// a) the sc_module constructor is entered
49// b) the exception is thrown
50// c) the exception processor deletes the storage for the sc_module
51// d) the stack is unrolled causing the sc_module_name instance to be deleted
52// e) ~sc_module_name() calls end_module() with its pointer to the sc_module
53// f) because the sc_module has been deleted its storage is corrupted,
54// either by linking it to a free space chain, or by reuse of some sort
55// g) the m_simc field is garbage
56// h) the m_object_manager field is also garbage
57// i) an exception occurs
58//
59// This does not happen for automatic sc_module instances since the
60// storage for the module is not reclaimed its just part of the stack.
61//
62// I am fixing this by having the destructor for sc_module clear the
63// module pointer in its sc_module_name instance. That cuts things at
64// step (e) above, since the pointer will be null if the module has
65// already been deleted. To make sure the module stack is okay, I call
66// end-module() in ~sc_module in the case where there is an
67// sc_module_name pointer lying around.
68
69#include "systemc.h"
70
71SC_MODULE(X)
72{
73    SC_CTOR(X)
74    {
75        SC_REPORT_ERROR(SC_ID_SET_TIME_RESOLUTION_,"");
76    }
77};
78
79int sc_main(int argc, char* argv[])
80{
81    sc_module* x_p = new X("x");
82
83    cout << "Program completed" << endl;
84    return 0;
85}
86
87