1 // Copyright (c) 2005, Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the names of its 15 // contributors may be used to endorse or promote products derived from 16 // this software without specific prior written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 30 // --- 31 // All Rights Reserved. 32 // 33 // Author: Maxim Lifantsev 34 // 35 // A file to ensure that components of heap leak checker run before 36 // all global object constructors and after all global object 37 // destructors. 38 // 39 // This file must be the last library any binary links against. 40 // Otherwise, the heap checker may not be able to run early enough to 41 // catalog all the global objects in your program. If this happens, 42 // and later in the program you allocate memory and have one of these 43 // "uncataloged" global objects point to it, the heap checker will 44 // consider that allocation to be a leak, even though it's not (since 45 // the allocated object is reachable from global data and hence "live"). 46 47 #include <gperftools/malloc_extension.h> 48 #include "base/abort.h" 49 50 // A dummy variable to refer from heap-checker.cc. This is to make 51 // sure this file is not optimized out by the linker. 52 bool heap_leak_checker_bcad_variable; 53 54 extern void HeapLeakChecker_AfterDestructors(); // in heap-checker.cc 55 56 // A helper class to ensure that some components of heap leak checking 57 // can happen before construction and after destruction 58 // of all global/static objects. 59 class HeapLeakCheckerGlobalPrePost { 60 public: 61 HeapLeakCheckerGlobalPrePost() { 62 if (count_ == 0) { 63 // The 'new int' will ensure that we have run an initial malloc 64 // hook, which will set up the heap checker via 65 // MallocHook_InitAtFirstAllocation_HeapLeakChecker. See malloc_hook.cc. 66 // This is done in this roundabout fashion in order to avoid self-deadlock 67 // if we directly called HeapLeakChecker_BeforeConstructors here. 68 delete new int; 69 // This needs to be called before the first allocation of an STL 70 // object, but after libc is done setting up threads (because it 71 // calls setenv, which requires a thread-aware errno). By 72 // putting it here, we hope it's the first bit of code executed 73 // after the libc global-constructor code. 74 MallocExtension::Initialize(); 75 } 76 ++count_; 77 } 78 ~HeapLeakCheckerGlobalPrePost() { 79 if (count_ <= 0) tcmalloc::Abort(); 80 --count_; 81 if (count_ == 0) HeapLeakChecker_AfterDestructors(); 82 } 83 private: 84 // Counter of constructions/destructions of objects of this class 85 // (just in case there are more than one of them). 86 static int count_; 87 }; 88 89 int HeapLeakCheckerGlobalPrePost::count_ = 0; 90 91 // The early-construction/late-destruction global object. 92 static const HeapLeakCheckerGlobalPrePost heap_leak_checker_global_pre_post; 93