65 RCPNodeInfo() : nodePtr(0) {}
67 : info(info_in), nodePtr(nodePtr_in)
74 typedef std::pair<const void*, RCPNodeInfo> VoidPtrNodeRCPInfoPair_t;
77 typedef std::multimap<const void*, RCPNodeInfo> rcp_node_list_t;
80 class RCPNodeInfoListPred {
82 bool operator()(
const rcp_node_list_t::value_type &v1,
83 const rcp_node_list_t::value_type &v2
87 return v1.second.nodePtr->insertion_number() < v2.second.nodePtr->insertion_number();
89 return v1.first < v2.first;
112 rcp_node_list_t*& rcp_node_list()
114 static rcp_node_list_t *s_rcp_node_list = 0;
122 return s_rcp_node_list;
126 bool& loc_isTracingActiveRCPNodes()
128 static bool s_loc_isTracingActiveRCPNodes =
129 #if defined(TEUCHOS_DEBUG) && defined(HAVE_TEUCHOS_DEBUG_RCP_NODE_TRACING)
135 return s_loc_isTracingActiveRCPNodes;
142 return s_loc_rcpNodeStatistics;
146 bool& loc_printRCPNodeStatisticsOnExit()
148 static bool s_loc_printRCPNodeStatisticsOnExit =
false;
149 return s_loc_printRCPNodeStatisticsOnExit;
168 const void* base_obj_map_key_void_ptr = rcp_node->get_base_obj_map_key_void_ptr();
169 if (base_obj_map_key_void_ptr)
170 return base_obj_map_key_void_ptr;
178 std::ostringstream oss;
180 <<
"RCPNode {address="
183 <<
", base_obj_map_key_void_ptr=" << rcp_node->get_base_obj_map_key_void_ptr()
186 <<
", map_key_void_ptr=" << get_map_key_void_ptr(rcp_node)
189 <<
", insertionNumber="<< rcp_node->insertion_number()
208 const any &extra_data,
const std::string& name
216 const std::string type_and_name( extra_data.
typeName() + std::string(
":") + name );
221 ,
"Error, the type:name pair \'" << type_and_name
222 <<
"\' already exists and force_unique==true!" );
230 (*extra_data_map_)[type_and_name] =
241 ,
"Error, no extra data has been set yet!" );
246 const std::string type_and_name( type_name + std::string(
":") + name );
248 extra_data == NULL, std::invalid_argument
249 ,
"Error, the type:name pair \'" << type_and_name <<
"\' is not found!" );
257 const std::string& name )
260 const std::string type_and_name( type_name + std::string(
":") + name );
263 return &(*itr).second.extra_data;
276 extra_data_map_t::value_type &entry = *itr;
278 entry.second.extra_data =
any();
293 return loc_isTracingActiveRCPNodes();
297 #if defined(TEUCHOS_DEBUG) && !defined(HAVE_TEUCHOS_DEBUG_RCP_NODE_TRACING)
298 void RCPNodeTracer::setTracingActiveRCPNodes(
bool tracingActiveNodes)
300 loc_isTracingActiveRCPNodes() = tracingActiveNodes;
309 return rcp_node_list()->size();
317 return loc_rcpNodeStatistics();
325 <<
"\n*** RCPNode Tracing statistics:"
335 bool printRCPNodeStatisticsOnExit)
337 loc_printRCPNodeStatisticsOnExit() = printRCPNodeStatisticsOnExit;
343 return loc_printRCPNodeStatisticsOnExit();
349 #ifdef TEUCHOS_SHOW_ACTIVE_REFCOUNTPTR_NODE_TRACE
351 <<
"\nCalled printActiveRCPNodes() :"
352 <<
" rcp_node_list.size() = " << rcp_node_list().size() <<
"\n";
353 #endif // TEUCHOS_SHOW_ACTIVE_REFCOUNTPTR_NODE_TRACE
354 if (loc_isTracingActiveRCPNodes()) {
356 if (rcp_node_list()->size() > 0) {
362 typedef std::vector<VoidPtrNodeRCPInfoPair_t> rcp_node_vec_t;
363 rcp_node_vec_t rcp_node_vec(rcp_node_list()->begin(), rcp_node_list()->end());
364 std::sort(rcp_node_vec.begin(), rcp_node_vec.end(), RCPNodeInfoListPred());
366 typedef rcp_node_vec_t::const_iterator itr_t;
368 for ( itr_t itr = rcp_node_vec.begin(); itr != rcp_node_vec.end(); ++itr ) {
369 const rcp_node_list_t::value_type &entry = *itr;
373 << std::setw(3) << std::right << i << std::left
374 <<
": RCPNode (map_key_void_ptr=" << entry.first <<
")\n"
375 <<
" Information = " << entry.second.info <<
"\n"
376 <<
" RCPNode address = " << entry.second.nodePtr <<
"\n"
378 <<
" insertionNumber = " << entry.second.nodePtr->insertion_number()
397 static int insertionNumber = 0;
402 rcp_node->set_insertion_number(insertionNumber);
405 if (loc_isTracingActiveRCPNodes()) {
409 #ifdef RCP_NODE_DEBUG_TRACE_PRINT
411 <<
"RCPNodeTracer::addNewRCPNode(...): Adding "
412 << convertRCPNodeToString(rcp_node) <<
" ...\n";
417 const void *
const map_key_void_ptr = get_map_key_void_ptr(rcp_node);
420 typedef rcp_node_list_t::iterator itr_t;
421 typedef std::pair<itr_t, itr_t> itr_itr_t;
422 const itr_itr_t itr_itr = rcp_node_list()->equal_range(map_key_void_ptr);
423 const bool rcp_node_already_exists = itr_itr.first != itr_itr.second;
424 RCPNode *previous_rcp_node = 0;
425 bool previous_rcp_node_has_ownership =
false;
426 for (itr_t itr = itr_itr.first; itr != itr_itr.second; ++itr) {
427 previous_rcp_node = itr->second.nodePtr;
429 previous_rcp_node_has_ownership =
true;
434 rcp_node_already_exists && rcp_node->
has_ownership() && previous_rcp_node_has_ownership,
436 "RCPNodeTracer::addNewRCPNode(rcp_node): Error, the client is trying to create a new\n"
437 "RCPNode object to an existing managed object in another RCPNode:\n"
439 " New " << convertRCPNodeToString(rcp_node) <<
"\n"
441 " Existing " << convertRCPNodeToString(previous_rcp_node) <<
"\n"
443 " Number current nodes = " << rcp_node_list()->size() <<
"\n"
445 "This may indicate that the user might be trying to create a weak RCP to an existing\n"
446 "object but forgot make it non-ownning. Perhaps they meant to use rcpFromRef(...)\n"
447 "or an equivalent function?\n"
458 (*rcp_node_list()).insert(
460 std::make_pair(map_key_void_ptr, RCPNodeInfo(info, rcp_node))
469 ++loc_rcpNodeStatistics().totalNumRCPNodeAllocations;
470 loc_rcpNodeStatistics().maxNumRCPNodes =
476 #define TEUCHOS_RCPNODE_REMOVE_RCPNODE(CONDITION, RCPNODE) \
477 TEUCHOS_TEST_FOR_EXCEPTION((CONDITION), \
479 "RCPNodeTracer::removeRCPNode(node_ptr): Error, the " \
480 << convertRCPNodeToString(RCPNODE) << " is not found in the list of" \
481 " active RCP nodes being traced even though all nodes should be traced." \
482 " This should not be possible and can only be an internal programming error!")
498 typedef rcp_node_list_t::iterator itr_t;
499 typedef std::pair<itr_t, itr_t> itr_itr_t;
501 const itr_itr_t itr_itr =
502 rcp_node_list()->equal_range(get_map_key_void_ptr(rcp_node));
503 const bool rcp_node_exists = itr_itr.first != itr_itr.second;
505 #ifdef HAVE_TEUCHOS_DEBUG_RCP_NODE_TRACING
518 if (rcp_node_exists) {
519 #ifdef RCP_NODE_DEBUG_TRACE_PRINT
521 <<
"RCPNodeTracer::removeRCPNode(...): Removing "
522 << convertRCPNodeToString(rcp_node) <<
" ...\n";
524 bool foundRCPNode =
false;
525 for(itr_t itr = itr_itr.first; itr != itr_itr.second; ++itr) {
526 if (itr->second.nodePtr == rcp_node) {
527 rcp_node_list()->erase(itr);
528 ++loc_rcpNodeStatistics().totalNumRCPNodeDeletions;
542 typedef rcp_node_list_t::iterator itr_t;
543 typedef std::pair<itr_t, itr_t> itr_itr_t;
546 const itr_itr_t itr_itr = rcp_node_list()->equal_range(p);
547 for (itr_t itr = itr_itr.first; itr != itr_itr.second; ++itr) {
548 RCPNode* rcpNode = itr->second.nodePtr;
563 "\n*** Warning! The following Teuchos::RCPNode objects were created but have"
564 "\n*** not been destroyed yet. A memory checking tool may complain that these"
565 "\n*** objects are not destroyed correctly."
567 "\n*** There can be many possible reasons that this might occur including:"
569 "\n*** a) The program called abort() or exit() before main() was finished."
570 "\n*** All of the objects that would have been freed through destructors"
571 "\n*** are not freed but some compilers (e.g. GCC) will still call the"
572 "\n*** destructors on static objects (which is what causes this message"
573 "\n*** to be printed)."
575 "\n*** b) The program is using raw new/delete to manage some objects and"
576 "\n*** delete was not called correctly and the objects not deleted hold"
577 "\n*** other objects through reference-counted pointers."
579 "\n*** c) This may be an indication that these objects may be involved in"
580 "\n*** a circular dependency of reference-counted managed objects."
589 "NOTE: To debug issues, open a debugger, and set a break point in the function where\n"
590 "the RCPNode object is first created to determine the context where the object first\n"
591 "gets created. Each RCPNode object is given a unique insertionNumber to allow setting\n"
592 "breakpoints in the code. For example, in GDB one can perform:\n"
594 "1) Open the debugger (GDB) and run the program again to get updated object addresses\n"
596 "2) Set a breakpoint in the RCPNode insertion routine when the desired RCPNode is first\n"
597 "inserted. In GDB, to break when the RCPNode with insertionNumber==3 is added, do:\n"
599 " (gdb) b 'Teuchos::RCPNodeTracer::addNewRCPNode( [TAB] ' [ENTER]\n"
600 " (gdb) cond 1 insertionNumber==3 [ENTER]\n"
602 "3) Run the program in the debugger. In GDB, do:\n"
604 " (gdb) run [ENTER]\n"
606 "4) Examine the call stack when the program breaks in the function addNewRCPNode(...)\n"
618 #ifdef TEUCHOS_SHOW_ACTIVE_REFCOUNTPTR_NODE_TRACE
619 std::cerr <<
"\nCalled ActiveRCPNodesSetup::ActiveRCPNodesSetup() : count = " <<
count_ <<
"\n";
620 #endif // TEUCHOS_SHOW_ACTIVE_REFCOUNTPTR_NODE_TRACE
621 if (!rcp_node_list())
622 rcp_node_list() =
new rcp_node_list_t;
629 #ifdef TEUCHOS_SHOW_ACTIVE_REFCOUNTPTR_NODE_TRACE
630 std::cerr <<
"\nCalled ActiveRCPNodesSetup::~ActiveRCPNodesSetup() : count = " <<
count_ <<
"\n";
631 #endif // TEUCHOS_SHOW_ACTIVE_REFCOUNTPTR_NODE_TRACE
632 if( --count_ == 0 ) {
633 #ifdef TEUCHOS_SHOW_ACTIVE_REFCOUNTPTR_NODE_TRACE
634 std::cerr <<
"\nPrint active nodes!\n";
635 #endif // TEUCHOS_SHOW_ACTIVE_REFCOUNTPTR_NODE_TRACE
636 std::cout << std::flush;
646 delete rcp_node_list();
685 local_activeRCPNodesSetup.foo();
715 void Teuchos::throw_null_ptr_error(
const std::string &type_name )
718 true, NullReferenceError,
719 type_name <<
" : You can not call operator->() or operator*()"
720 <<
" if getRawPtr()==0!" );
static void printRCPNodeStatistics(const RCPNodeStatistics &rcpNodeStatistics, std::ostream &out)
Print the RCPNode allocation statistics.
void impl_pre_delete_extra_data()
static bool isTracingActiveRCPNodes()
Return if we are tracing active nodes or not.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
int deincr_count(const ERCPStrength strength)
static void setPrintRCPNodeStatisticsOnExit(bool printRCPNodeStatisticsOnExit)
Set if RCPNode usage statistics will be printed when the program ends or not.
void set_extra_data(const any &extra_data, const std::string &name, EPrePostDestruction destroy_when, bool force_unique)
static RCPNodeStatistics getRCPNodeStatistics()
Return the statistics on RCPNode allocations.
Modified boost::any class, which is a container for a templated value.
long int totalNumRCPNodeAllocations
any & get_extra_data(const std::string &type_name, const std::string &name)
void has_ownership(bool has_ownership_in)
static std::string getCommonDebugNotesString()
Common error message string on how to debug RCPNode problems.
static bool getPrintRCPNodeStatisticsOnExit()
Reteurn if RCPNode usage statistics will be printed when the program ends or not. ...
Node class to keep track of address and the reference count for a reference-counted utility class and...
static RCPNode * getExistingRCPNodeGivenLookupKey(const void *lookupKey)
Return a raw pointer to an existing owning RCPNode given its lookup key.
ERCPStrength strength() const
The strength of this handle.
#define TEUCHOS_MAX(x, y)
virtual const std::string get_base_obj_type_name() const =0
#define TEUCHOS_RCPNODE_REMOVE_RCPNODE(CONDITION, RCPNODE)
EPrePostDestruction
Used to specify a pre or post destruction of extra data.
virtual void delete_obj()=0
any * get_optional_extra_data(const std::string &type_name, const std::string &name)
static void removeRCPNode(RCPNode *rcp_node)
Remove an RCPNode from global list.
extra_data_map_t * extra_data_map_
std::string typeName() const
Return the name of the type.
#define TEUCHOS_ASSERT(assertion_test)
This macro is throws when an assert fails.
Teuchos::map< std::string, extra_data_entry_t > extra_data_map_t
long int totalNumRCPNodeDeletions
Reference-counted pointer node classes.
Thrown if a duplicate owning RCP is creatd the the same object.
static std::string getActiveRCPNodeHeaderString()
Header string used in printActiveRCPNodes().
static int numActiveRCPNodes()
Print the number of active RCPNode objects currently being tracked.
static void addNewRCPNode(RCPNode *rcp_node, const std::string &info)
Add new RCPNode to the global list.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
This macro is designed to be a short version of TEUCHOS_TEST_FOR_EXCEPTION() that is easier to call...
static void printActiveRCPNodes(std::ostream &out)
Print the list of currently active RCP nodes.