43 #include "Teuchos_Assert.hpp" 44 #include "Teuchos_Exceptions.hpp" 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;
153 bool& loc_printActiveRcpNodesOnExit()
155 static bool s_loc_printActiveRcpNodesOnExit =
true;
156 return s_loc_printActiveRcpNodesOnExit;
175 const void* base_obj_map_key_void_ptr = rcp_node->get_base_obj_map_key_void_ptr();
176 if (base_obj_map_key_void_ptr)
177 return base_obj_map_key_void_ptr;
185 std::ostringstream oss;
187 <<
"RCPNode {address=" 190 <<
", base_obj_map_key_void_ptr=" << rcp_node->get_base_obj_map_key_void_ptr()
193 <<
", map_key_void_ptr=" << get_map_key_void_ptr(rcp_node)
196 <<
", insertionNumber="<< rcp_node->insertion_number()
214 void RCPNode::set_extra_data(
215 const any &extra_data,
const std::string& name
220 if(extra_data_map_==NULL) {
221 extra_data_map_ =
new extra_data_map_t;
223 const std::string type_and_name( extra_data.
typeName() + std::string(
":") + name );
224 extra_data_map_t::iterator itr = extra_data_map_->find(type_and_name);
227 (itr != extra_data_map_->end() && force_unique), std::invalid_argument
228 ,
"Error, the type:name pair \'" << type_and_name
229 <<
"\' already exists and force_unique==true!" );
231 if (itr != extra_data_map_->end()) {
233 itr->second = extra_data_entry_t(extra_data,destroy_when);
237 (*extra_data_map_)[type_and_name] =
238 extra_data_entry_t(extra_data,destroy_when);
243 any& RCPNode::get_extra_data(
const std::string& type_name,
const std::string& name )
247 extra_data_map_==NULL, std::invalid_argument
248 ,
"Error, no extra data has been set yet!" );
250 any *extra_data = get_optional_extra_data(type_name,name);
253 const std::string type_and_name( type_name + std::string(
":") + name );
255 extra_data == NULL, std::invalid_argument
256 ,
"Error, the type:name pair \'" << type_and_name <<
"\' is not found!" );
263 any* RCPNode::get_optional_extra_data(
const std::string& type_name,
264 const std::string& name )
266 if( extra_data_map_ == NULL )
return NULL;
267 const std::string type_and_name( type_name + std::string(
":") + name );
268 extra_data_map_t::iterator itr = extra_data_map_->find(type_and_name);
269 if(itr != extra_data_map_->end())
270 return &(*itr).second.extra_data;
275 void RCPNode::impl_pre_delete_extra_data()
278 extra_data_map_t::iterator itr = extra_data_map_->begin();
279 itr != extra_data_map_->end();
283 extra_data_map_t::value_type &entry = *itr;
284 if(entry.second.destroy_when == PRE_DESTROY)
285 entry.second.extra_data =
any();
298 bool RCPNodeTracer::isTracingActiveRCPNodes()
300 return loc_isTracingActiveRCPNodes();
304 #if defined(TEUCHOS_DEBUG) && !defined(HAVE_TEUCHOS_DEBUG_RCP_NODE_TRACING) 305 void RCPNodeTracer::setTracingActiveRCPNodes(
bool tracingActiveNodes)
307 loc_isTracingActiveRCPNodes() = tracingActiveNodes;
312 int RCPNodeTracer::numActiveRCPNodes()
316 return rcp_node_list()->size();
321 RCPNodeTracer::getRCPNodeStatistics()
323 return loc_rcpNodeStatistics();
326 void RCPNodeTracer::printRCPNodeStatistics(
331 <<
"\n*** RCPNode Tracing statistics:" 333 <<
"\n maxNumRCPNodes = "<<rcpNodeStatistics.maxNumRCPNodes
334 <<
"\n totalNumRCPNodeAllocations = "<<rcpNodeStatistics.totalNumRCPNodeAllocations
335 <<
"\n totalNumRCPNodeDeletions = "<<rcpNodeStatistics.totalNumRCPNodeDeletions
340 void RCPNodeTracer::setPrintRCPNodeStatisticsOnExit(
341 bool printRCPNodeStatisticsOnExit)
343 loc_printRCPNodeStatisticsOnExit() = printRCPNodeStatisticsOnExit;
347 bool RCPNodeTracer::getPrintRCPNodeStatisticsOnExit()
349 return loc_printRCPNodeStatisticsOnExit();
353 void RCPNodeTracer::setPrintActiveRcpNodesOnExit(
bool printActiveRcpNodesOnExit)
355 loc_printActiveRcpNodesOnExit() = printActiveRcpNodesOnExit;
359 bool RCPNodeTracer::getPrintActiveRcpNodesOnExit()
361 return loc_printActiveRcpNodesOnExit();
365 void RCPNodeTracer::printActiveRCPNodes(std::ostream &out)
367 #ifdef TEUCHOS_SHOW_ACTIVE_REFCOUNTPTR_NODE_TRACE 369 <<
"\nCalled printActiveRCPNodes() :" 370 <<
" rcp_node_list.size() = " << rcp_node_list().size() <<
"\n";
371 #endif // TEUCHOS_SHOW_ACTIVE_REFCOUNTPTR_NODE_TRACE 372 if (loc_isTracingActiveRCPNodes()) {
374 if (rcp_node_list()->size() > 0) {
375 out << getActiveRCPNodeHeaderString();
380 typedef std::vector<VoidPtrNodeRCPInfoPair_t> rcp_node_vec_t;
381 rcp_node_vec_t rcp_node_vec(rcp_node_list()->begin(), rcp_node_list()->end());
382 std::sort(rcp_node_vec.begin(), rcp_node_vec.end(), RCPNodeInfoListPred());
384 typedef rcp_node_vec_t::const_iterator itr_t;
386 for ( itr_t itr = rcp_node_vec.begin(); itr != rcp_node_vec.end(); ++itr ) {
387 const rcp_node_list_t::value_type &entry = *itr;
391 << std::setw(3) << std::right << i << std::left
392 <<
": RCPNode (map_key_void_ptr=" << entry.first <<
")\n" 393 <<
" Information = " << entry.second.info <<
"\n" 394 <<
" RCPNode address = " << entry.second.nodePtr <<
"\n" 396 <<
" insertionNumber = " << entry.second.nodePtr->insertion_number()
402 << getCommonDebugNotesString();
411 void RCPNodeTracer::addNewRCPNode(
RCPNode* rcp_node,
const std::string &info )
415 static int insertionNumber = 0;
420 rcp_node->set_insertion_number(insertionNumber);
423 if (loc_isTracingActiveRCPNodes()) {
427 #ifdef RCP_NODE_DEBUG_TRACE_PRINT 429 <<
"RCPNodeTracer::addNewRCPNode(...): Adding " 430 << convertRCPNodeToString(rcp_node) <<
" ...\n";
435 const void *
const map_key_void_ptr = get_map_key_void_ptr(rcp_node);
438 typedef rcp_node_list_t::iterator itr_t;
439 typedef std::pair<itr_t, itr_t> itr_itr_t;
440 const itr_itr_t itr_itr = rcp_node_list()->equal_range(map_key_void_ptr);
441 const bool rcp_node_already_exists = itr_itr.first != itr_itr.second;
442 RCPNode *previous_rcp_node = 0;
443 bool previous_rcp_node_has_ownership =
false;
444 for (itr_t itr = itr_itr.first; itr != itr_itr.second; ++itr) {
445 previous_rcp_node = itr->second.nodePtr;
447 previous_rcp_node_has_ownership =
true;
452 rcp_node_already_exists && rcp_node->
has_ownership() && previous_rcp_node_has_ownership,
454 "RCPNodeTracer::addNewRCPNode(rcp_node): Error, the client is trying to create a new\n" 455 "RCPNode object to an existing managed object in another RCPNode:\n" 457 " New " << convertRCPNodeToString(rcp_node) <<
"\n" 459 " Existing " << convertRCPNodeToString(previous_rcp_node) <<
"\n" 461 " Number current nodes = " << rcp_node_list()->size() <<
"\n" 463 "This may indicate that the user might be trying to create a weak RCP to an existing\n" 464 "object but forgot make it non-ownning. Perhaps they meant to use rcpFromRef(...)\n" 465 "or an equivalent function?\n" 467 << getCommonDebugNotesString();
476 (*rcp_node_list()).insert(
478 std::make_pair(map_key_void_ptr, RCPNodeInfo(info, rcp_node))
487 ++loc_rcpNodeStatistics().totalNumRCPNodeAllocations;
488 loc_rcpNodeStatistics().maxNumRCPNodes =
489 TEUCHOS_MAX(loc_rcpNodeStatistics().maxNumRCPNodes, numActiveRCPNodes());
494 #define TEUCHOS_RCPNODE_REMOVE_RCPNODE(CONDITION, RCPNODE) \ 495 TEUCHOS_TEST_FOR_EXCEPTION((CONDITION), \ 497 "RCPNodeTracer::removeRCPNode(node_ptr): Error, the " \ 498 << convertRCPNodeToString(RCPNODE) << " is not found in the list of" \ 499 " active RCP nodes being traced even though all nodes should be traced." \ 500 " This should not be possible and can only be an internal programming error!") 503 void RCPNodeTracer::removeRCPNode(
RCPNode* rcp_node )
516 typedef rcp_node_list_t::iterator itr_t;
517 typedef std::pair<itr_t, itr_t> itr_itr_t;
519 const itr_itr_t itr_itr =
520 rcp_node_list()->equal_range(get_map_key_void_ptr(rcp_node));
521 const bool rcp_node_exists = itr_itr.first != itr_itr.second;
523 #ifdef HAVE_TEUCHOS_DEBUG_RCP_NODE_TRACING 527 TEUCHOS_RCPNODE_REMOVE_RCPNODE(!rcp_node_exists, rcp_node);
536 if (rcp_node_exists) {
537 #ifdef RCP_NODE_DEBUG_TRACE_PRINT 539 <<
"RCPNodeTracer::removeRCPNode(...): Removing " 540 << convertRCPNodeToString(rcp_node) <<
" ...\n";
542 bool foundRCPNode =
false;
543 for(itr_t itr = itr_itr.first; itr != itr_itr.second; ++itr) {
544 if (itr->second.nodePtr == rcp_node) {
545 rcp_node_list()->erase(itr);
546 ++loc_rcpNodeStatistics().totalNumRCPNodeDeletions;
552 TEUCHOS_RCPNODE_REMOVE_RCPNODE(!foundRCPNode, rcp_node);
558 RCPNode* RCPNodeTracer::getExistingRCPNodeGivenLookupKey(
const void* p)
560 typedef rcp_node_list_t::iterator itr_t;
561 typedef std::pair<itr_t, itr_t> itr_itr_t;
564 const itr_itr_t itr_itr = rcp_node_list()->equal_range(p);
565 for (itr_t itr = itr_itr.first; itr != itr_itr.second; ++itr) {
566 RCPNode* rcpNode = itr->second.nodePtr;
577 std::string RCPNodeTracer::getActiveRCPNodeHeaderString()
581 "\n*** Warning! The following Teuchos::RCPNode objects were created but have" 582 "\n*** not been destroyed yet. A memory checking tool may complain that these" 583 "\n*** objects are not destroyed correctly." 585 "\n*** There can be many possible reasons that this might occur including:" 587 "\n*** a) The program called abort() or exit() before main() was finished." 588 "\n*** All of the objects that would have been freed through destructors" 589 "\n*** are not freed but some compilers (e.g. GCC) will still call the" 590 "\n*** destructors on static objects (which is what causes this message" 591 "\n*** to be printed)." 593 "\n*** b) The program is using raw new/delete to manage some objects and" 594 "\n*** delete was not called correctly and the objects not deleted hold" 595 "\n*** other objects through reference-counted pointers." 597 "\n*** c) This may be an indication that these objects may be involved in" 598 "\n*** a circular dependency of reference-counted managed objects." 604 std::string RCPNodeTracer::getCommonDebugNotesString()
607 "NOTE: To debug issues, open a debugger, and set a break point in the function where\n" 608 "the RCPNode object is first created to determine the context where the object first\n" 609 "gets created. Each RCPNode object is given a unique insertionNumber to allow setting\n" 610 "breakpoints in the code. For example, in GDB one can perform:\n" 612 "1) Open the debugger (GDB) and run the program again to get updated object addresses\n" 614 "2) Set a breakpoint in the RCPNode insertion routine when the desired RCPNode is first\n" 615 "inserted. In GDB, to break when the RCPNode with insertionNumber==3 is added, do:\n" 617 " (gdb) b 'Teuchos::RCPNodeTracer::addNewRCPNode( [TAB] ' [ENTER]\n" 618 " (gdb) cond 1 insertionNumber==3 [ENTER]\n" 620 "3) Run the program in the debugger. In GDB, do:\n" 622 " (gdb) run [ENTER]\n" 624 "4) Examine the call stack when the program breaks in the function addNewRCPNode(...)\n" 634 ActiveRCPNodesSetup::ActiveRCPNodesSetup()
636 #ifdef TEUCHOS_SHOW_ACTIVE_REFCOUNTPTR_NODE_TRACE 637 std::cerr <<
"\nCalled ActiveRCPNodesSetup::ActiveRCPNodesSetup() : count = " << count_ <<
"\n";
638 #endif // TEUCHOS_SHOW_ACTIVE_REFCOUNTPTR_NODE_TRACE 639 if (!rcp_node_list())
640 rcp_node_list() =
new rcp_node_list_t;
645 ActiveRCPNodesSetup::~ActiveRCPNodesSetup()
647 #ifdef TEUCHOS_SHOW_ACTIVE_REFCOUNTPTR_NODE_TRACE 648 std::cerr <<
"\nCalled ActiveRCPNodesSetup::~ActiveRCPNodesSetup() : count = " << count_ <<
"\n";
649 #endif // TEUCHOS_SHOW_ACTIVE_REFCOUNTPTR_NODE_TRACE 650 if( --count_ == 0 ) {
651 #ifdef TEUCHOS_SHOW_ACTIVE_REFCOUNTPTR_NODE_TRACE 652 std::cerr <<
"\nPrint active nodes!\n";
653 #endif // TEUCHOS_SHOW_ACTIVE_REFCOUNTPTR_NODE_TRACE 654 std::cout << std::flush;
657 RCPNodeTracer::getRCPNodeStatistics();
658 if (rcpNodeStatistics.maxNumRCPNodes
659 && RCPNodeTracer::getPrintRCPNodeStatisticsOnExit())
661 RCPNodeTracer::printRCPNodeStatistics(rcpNodeStatistics, std::cout);
663 if (RCPNodeTracer::getPrintActiveRcpNodesOnExit()) {
664 RCPNodeTracer::printActiveRCPNodes(std::cerr);
666 delete rcp_node_list();
679 int Teuchos::ActiveRCPNodesSetup::count_ = 0;
687 void RCPNodeHandle::unbindOne()
693 if (node_->strong_count()==1 && strength()==RCP_STRONG) {
705 local_activeRCPNodesSetup.foo();
706 RCPNodeTracer::removeRCPNode(node_);
710 if ( (node_->strong_count() + node_->weak_count()) == 1 ) {
721 node_->deincr_count(strength());
735 void Teuchos::throw_null_ptr_error(
const std::string &type_name )
739 type_name <<
" : You can not call operator->() or operator*()" 740 <<
" if getRawPtr()==0!" );
Null reference error exception class.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Modified boost::any class, which is a container for a templated value.
void has_ownership(bool has_ownership_in)
Node class to keep track of address and the reference count for a reference-counted utility class and...
virtual const std::string get_base_obj_type_name() const =0
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
std::string typeName() const
Return the name of the type.
EPrePostDestruction
Used to specify a pre or post destruction of extra data.
#define TEUCHOS_ASSERT(assertion_test)
This macro is throws when an assert fails.
Reference-counted pointer node classes.
Thrown if a duplicate owning RCP is creatd the the same object.
#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...