os/ossrv/genericopenlibs/cppstdlib/test/runtime/src/terminate_handler_test.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // Name        : terminate_handler_test.cpp
    15 // Part of     : standard c++ tests.
    16 // 
    17 //
    18 
    19 #include <exception>
    20 #include "test_decls.h"
    21 #include <cstddef>
    22 #include <cstdlib>
    23 
    24 int glob_val = 0;
    25 
    26 class UserClass{
    27 public:
    28 	~UserClass() {
    29 		throw int (0); //  This causes std::terminate to be called since the 
    30 						// destructor is throwing while someone is unwinding the stack.
    31 						// Since we ahve the terminate_handler set, my_handler must get called.
    32 	}
    33 };
    34 
    35 
    36 void foo() {
    37 	UserClass uc;
    38 	throw int (1); // causes stack unwinding - hence calls ~UserClass()
    39 }
    40 
    41 
    42 void bar() {
    43 	try {
    44 	foo();
    45 	}catch(...){}
    46 }
    47 
    48 void my_handler()
    49 {
    50 	glob_val = 1;
    51 	CPP_TESTS_ASSERT_ALLWAYS(1);
    52 	abort();
    53 }
    54 
    55 int main()
    56 {
    57 	std::terminate_handler h_old, h_new;
    58 	h_new = (std::terminate_handler)&my_handler;
    59 	h_old = std::set_terminate(h_new);
    60 	h_old = h_old;
    61 	bar();
    62 
    63 	//Failed to call terminate handler
    64 	CPP_TESTS_ASSERT_ALLWAYS(0);
    65 
    66 	return 0;
    67 }