30 static void make_large_number_of_copies(RCP<int>
ptr) {
31 std::vector<RCP<int> > ptrs(10000, ptr);
67 RCP<int>
ptr(
new int);
68 std::vector<std::thread> threads;
69 for (
int i = 0; i < numThreads; ++i) {
70 threads.push_back(std::thread(make_large_number_of_copies,
ptr));
72 for (
unsigned int i = 0; i < threads.size(); ++i) {
83 static void create_independent_rcp_objects() {
85 while (!ThreadTestManager::s_bAllowThreadsToRun) {}
89 RCP<int>
ptr(
new int );
118 std::vector<std::thread> threads;
120 ThreadTestManager::s_bAllowThreadsToRun =
false;
121 for (
int i = 0; i < numThreads; ++i) {
122 threads.push_back(std::thread(create_independent_rcp_objects));
125 ThreadTestManager::s_bAllowThreadsToRun =
true;
126 for (
unsigned int i = 0; i < threads.size(); ++i) {
139 static void create_independent_rcp_without_ownership() {
140 for(
int n = 0;
n < 10000; ++
n ) {
141 int * intPtr =
new int;
143 RCP<int>
ptr( intPtr,
false );
168 std::vector<std::thread> threads;
169 for (
int i = 0; i < numThreads; ++i) {
170 threads.push_back(std::thread(create_independent_rcp_without_ownership));
172 for (
unsigned int i = 0; i < threads.size(); ++i) {
184 template<
typename SOURCE_RCP_TYPE>
185 static void thread_gets_a_copy_of_rcp(SOURCE_RCP_TYPE
ptr) {
189 while(!ThreadTestManager::s_bAllowThreadsToRun) {}
217 for(
int cycleIndex = 0; cycleIndex < numCycles; ++cycleIndex) {
219 CatchMemoryLeak::s_countAllocated = 0;
221 RCP<CatchMemoryLeak>
ptr(
new CatchMemoryLeak);
223 ThreadTestManager::s_bAllowThreadsToRun =
false;
224 std::vector<std::thread> threads;
225 for (
int threadIndex = 0; threadIndex < numThreads; ++threadIndex) {
226 threads.push_back(std::thread(
227 thread_gets_a_copy_of_rcp<RCP<CatchMemoryLeak>>, ptr));
233 ThreadTestManager::s_bAllowThreadsToRun =
true;
234 for (
unsigned int i = 0; i < threads.size(); ++i) {
238 convenience_log_progress(cycleIndex, numCycles);
239 if (CatchMemoryLeak::s_countAllocated != 0) {
253 void deallocCatchMemoryLeak(CatchMemoryLeak* ptr)
256 ++CatchMemoryLeak::s_countDeallocs;
282 for(
int cycleIndex = 0; cycleIndex < numCycles; ++cycleIndex) {
283 CatchMemoryLeak::s_countDeallocs = 0;
285 Teuchos::deallocFunctorDelete<CatchMemoryLeak>(deallocCatchMemoryLeak));
287 ThreadTestManager::s_bAllowThreadsToRun =
false;
288 std::vector<std::thread> threads;
289 for (
int threadIndex = 0; threadIndex < numThreads; ++threadIndex) {
290 threads.push_back(std::thread(
291 thread_gets_a_copy_of_rcp<RCP<CatchMemoryLeak>>, ptr));
297 ThreadTestManager::s_bAllowThreadsToRun =
true;
298 for (
unsigned int i = 0; i < threads.size(); ++i) {
302 convenience_log_progress(cycleIndex, numCycles);
303 if (CatchMemoryLeak::s_countDeallocs != 1) {
314 void deallocHandleCatchMemoryLeak(CatchMemoryLeak** handle)
316 ++CatchMemoryLeak::s_countDeallocs;
317 CatchMemoryLeak *ptr = *handle;
344 for(
int cycleIndex = 0; cycleIndex < numCycles; ++cycleIndex) {
345 CatchMemoryLeak::s_countDeallocs = 0;
347 Teuchos::deallocFunctorHandleDelete<CatchMemoryLeak>(
348 deallocHandleCatchMemoryLeak));
350 ThreadTestManager::s_bAllowThreadsToRun =
false;
351 std::vector<std::thread> threads;
352 for (
unsigned int threadIndex = 0; threadIndex <
353 numThreads; ++threadIndex) {
354 threads.push_back(std::thread(
355 thread_gets_a_copy_of_rcp<RCP<CatchMemoryLeak>>, ptr));
361 ThreadTestManager::s_bAllowThreadsToRun =
true;
362 for (
unsigned int i = 0; i < threads.size(); ++i) {
366 convenience_log_progress(cycleIndex, numCycles);
367 if (CatchMemoryLeak::s_countDeallocs != 1) {
379 static void call_release_on_rcp_if_flag_is_set(RCP<CatchMemoryLeak> ptr,
380 int numCopies,
bool bCallsRelease) {
382 while(!ThreadTestManager::s_bAllowThreadsToRun) {}
407 bool bFailure =
false;
409 for(
int cycleIndex = 0; cycleIndex < numCycles; ++cycleIndex) {
410 CatchMemoryLeak::s_countAllocated = 0;
411 CatchMemoryLeak * pMemoryToLeak =
new CatchMemoryLeak;
412 RCP<CatchMemoryLeak>
ptr(pMemoryToLeak);
414 ThreadTestManager::s_bAllowThreadsToRun =
false;
415 std::vector<std::thread> threads;
416 for (
int threadIndex = 0; threadIndex < numThreads; ++threadIndex) {
417 bool bCallRelease = (threadIndex==0);
418 threads.push_back(std::thread(call_release_on_rcp_if_flag_is_set,
419 ptr, 1, bCallRelease));
422 ThreadTestManager::s_bAllowThreadsToRun =
true;
423 for (
unsigned int i = 0; i < threads.size(); ++i) {
426 convenience_log_progress(cycleIndex, numCycles);
427 if (CatchMemoryLeak::s_countAllocated != 1) {
433 delete pMemoryToLeak;
446 class ExtraDataTest {
448 static RCP<ExtraDataTest<T> > create(T *ptr)
449 {
return rcp(
new ExtraDataTest(ptr)); }
450 ~ExtraDataTest() {
delete [] ptr_; }
453 ExtraDataTest(T *ptr) : ptr_(ptr) {}
456 ExtraDataTest(
const ExtraDataTest&);
457 ExtraDataTest& operator=(
const ExtraDataTest&);
480 for(
int cycleIndex = 0; cycleIndex < numCycles; ++cycleIndex) {
481 CatchMemoryLeak::s_countAllocated = 0;
483 RCP<CatchMemoryLeak>
ptr(
new CatchMemoryLeak[1]);
485 Teuchos::set_extra_data( ExtraDataTest<CatchMemoryLeak>::create(
486 ptr.getRawPtr()),
"dealloc", Teuchos::inOutArg(ptr));
488 ThreadTestManager::s_bAllowThreadsToRun =
false;
489 std::vector<std::thread> threads;
490 for (
unsigned int threadIndex = 0; threadIndex < numThreads;
492 threads.push_back(std::thread(
493 thread_gets_a_copy_of_rcp<RCP<CatchMemoryLeak>>, ptr));
499 ThreadTestManager::s_bAllowThreadsToRun =
true;
500 for (
unsigned int i = 0; i < threads.size(); ++i) {
504 convenience_log_progress(cycleIndex, numCycles);
505 if (CatchMemoryLeak::s_countAllocated != 0) {
547 for(
int cycleIndex = 0; cycleIndex < numCycles; ++cycleIndex) {
548 CatchMemoryLeak::s_countAllocated = 0;
550 RCP<CatchMemoryLeak>
ptr(
new CatchMemoryLeak);
552 ThreadTestManager::s_bAllowThreadsToRun =
false;
553 std::vector<std::thread> threads;
554 bool bToggleStrong =
true;
555 for (
int threadIndex = 0; threadIndex < numThreads; ++threadIndex) {
557 threads.push_back(std::thread(
558 thread_gets_a_copy_of_rcp<RCP<CatchMemoryLeak>>,
559 ptr.create_strong()));
562 threads.push_back(std::thread(
563 thread_gets_a_copy_of_rcp<RCP<CatchMemoryLeak>>,
566 bToggleStrong = !bToggleStrong;
572 ThreadTestManager::s_bAllowThreadsToRun =
true;
573 for (
unsigned int i = 0; i < threads.size(); ++i) {
577 convenience_log_progress(cycleIndex, numCycles);
578 if (CatchMemoryLeak::s_countAllocated != 0) {
589 static std::atomic<int> s_count_successful_conversions(0);
590 static std::atomic<int> s_count_failed_conversions(0);
591 template<
class SOURCE_RCP_TYPE>
592 static void attempt_make_a_strong_ptr(SOURCE_RCP_TYPE ptr) {
594 while(!ThreadTestManager::s_bAllowThreadsToRun) {}
596 RCP<CatchMemoryLeak> possibleStrongPtr = ptr.create_strong_thread_safe();
597 if (possibleStrongPtr.is_null()) {
598 ++s_count_failed_conversions;
601 ++s_count_successful_conversions;
632 s_count_successful_conversions = 0;
633 s_count_failed_conversions = 0;
635 for(
int cycleIndex = 0; cycleIndex < numCycles; ++cycleIndex) {
636 CatchMemoryLeak::s_countAllocated = 0;
638 RCP<CatchMemoryLeak>
ptr(
new CatchMemoryLeak);
640 ThreadTestManager::s_bAllowThreadsToRun =
false;
641 std::vector<std::thread> threads;
642 bool bCycleStrong =
true;
643 for (
int threadIndex = 0; threadIndex < numThreads; ++threadIndex) {
645 threads.push_back(std::thread(
646 attempt_make_a_strong_ptr<RCP<CatchMemoryLeak>>,
647 ptr.create_strong()));
650 threads.push_back(std::thread(
651 attempt_make_a_strong_ptr<RCP<CatchMemoryLeak>>,
654 bCycleStrong = !bCycleStrong;
660 ThreadTestManager::s_bAllowThreadsToRun =
true;
661 for (
unsigned int i = 0; i < threads.size(); ++i) {
665 if (CatchMemoryLeak::s_countAllocated != 0) {
671 std::cout <<
"Weak converted with null " << s_count_failed_conversions <<
672 " times and success " << s_count_successful_conversions
673 <<
" times. We want to see a mix of each. ";
#define TEST_INEQUALITY_CONST(v1, v2)
Assert the inequality of v1 and constant v2.
#define TEUCHOS_THREAD_SAFE_UNIT_TESTS_THREADS_USED
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
Ptr< T > ptr(T *p)
Create a pointer to an object from a raw pointer.
Smart reference counting pointer class for automatic garbage collection.
RCP< T > rcpWithDealloc(T *p, Dealloc_T dealloc, bool owns_mem=true)
Initialize from a raw pointer with a deallocation policy.
Reference-counted pointer node classes.
static int numActiveRCPNodes()
Print the number of active RCPNode objects currently being tracked.
Reference-counted pointer class and non-member templated function implementations.