int main(void) { int x; int y = 0; int* reachable; int* dubious; int* leaked; long n_reachable = 0; long n_dubious = 0; long n_leaked = 0; long n_suppressed = 0; /* we require these longs to have same size as a machine word */ assert(sizeof(long) == sizeof(void*)); /* Error counting */ printf("errors: %d\n", VALGRIND_COUNT_ERRORS); if (x == 0) { y++; } else { y--; } printf("errors: %d\n", VALGRIND_COUNT_ERRORS); /* Leak checking */ VALGRIND_DO_LEAK_CHECK; VALGRIND_COUNT_LEAKS(n_leaked, n_dubious, n_reachable, n_suppressed); if (n_reachable == 24) n_reachable = 0; /* handle glibc differences */ printf("leaks: %ldB, %ldB, %ldB, %ldB\n", n_leaked, n_dubious, n_reachable, n_suppressed); leaked = malloc(77); leaked = 0; dubious = malloc(88); dubious += 10; reachable = malloc(99); VALGRIND_DO_LEAK_CHECK; VALGRIND_DO_LEAK_CHECK; VALGRIND_COUNT_LEAKS(n_leaked, n_dubious, n_reachable, n_suppressed); if (n_reachable == 123) n_reachable = 99; /* handle glibc differences */ printf("leaks: %ldB, %ldB, %ldB, %ldB\n", n_leaked, n_dubious, n_reachable, n_suppressed); printf("errors: %d\n", VALGRIND_COUNT_ERRORS); return 0; }
void xcu_valgrind_pre_case () { int l, d, r, s; VALGRIND_DO_LEAK_CHECK; VALGRIND_COUNT_LEAKS(l, d, r, s); pre_test_leaks = l; }
void xcu_valgrind_post_case () { int l, d, r, s; VALGRIND_DO_LEAK_CHECK; VALGRIND_COUNT_LEAKS(l, d, r, s); if (l > pre_test_leaks) { CU_FAIL ("Memory leak detected"); } }
static int memleaks() { int leaked = 0, dubious = 0, reachable = 0, suppressed = 0; VALGRIND_DO_LEAK_CHECK; VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed); printf("memleaks: sure:%d dubious:%d reachable:%d suppress:%d\n", leaked, dubious, reachable, suppressed); // dubious+reachable are normally non-zero because of globals... // return leaked+dubious+reachable; return leaked; }
void memory_status_calibrate (const char *marker) { int l, d, r, s; VALGRIND_PRINTF ("Calibrating: %s\n", marker); VALGRIND_DO_LEAK_CHECK; VALGRIND_COUNT_LEAKS(l, d, r, s); _memory_baseline_leaks = l; _memory_baseline_errors = VALGRIND_COUNT_ERRORS; }
/* Signal handler which invokes valgrind magic. */ static RETSIGTYPE count_leaks(int signum) { unsigned long leaked, dubious, reachable, suppressed; /* Put the signal handler back in place */ signal(signum, count_leaks); /* Run a leak check */ VALGRIND_DO_LEAK_CHECK; /* Get valgrind's current leak counts. */ VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed); fprintf(stderr, "%s: valgrind: leaked=%lu, dubious=%lu, reachable=%lu, " "suppressed=%lu\n", progname, leaked, dubious, reachable, suppressed); }
int memory_status_verify (const char *marker) { int l, d, r, s, status; VALGRIND_PRINTF ("Verifying: %s\n", marker); status = MEMORY_OK; if (VALGRIND_COUNT_ERRORS > _memory_baseline_errors) status |= MEMORY_ERROR; VALGRIND_DO_LEAK_CHECK; VALGRIND_COUNT_LEAKS(l, d, r, s); if (l > _memory_baseline_leaks) status |= MEMORY_LEAK; return status; }
int LeakTracker::check_leaks() { int leaked = 0; int dubious = 0; int reachable = 0; int suppressed = 0; VALGRIND_DO_LEAK_CHECK; VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed); printf("memleaks: sure:%d dubious:%d reachable:%d suppress:%d\n", leaked, dubious, reachable, suppressed); bool has_leaks = (m_leaks != leaked); m_leaks = leaked; m_dubious = dubious; m_reachable = reachable; m_suppressed = suppressed; return has_leaks; }
static void memStats(StoreEntry * sentry) { storeBuffer(sentry); memReport(sentry); memStringStats(sentry); memBufStats(sentry); storeBufferFlush(sentry); #if WITH_VALGRIND if (RUNNING_ON_VALGRIND) { long int leaked = 0, dubious = 0, reachable = 0, suppressed = 0; storeAppendPrintf(sentry, "Valgrind Report:\n"); storeAppendPrintf(sentry, "Type\tAmount\n"); debug(13, 1) ("Asking valgrind for memleaks\n"); VALGRIND_DO_LEAK_CHECK; debug(13, 1) ("Getting valgrind statistics\n"); VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed); storeAppendPrintf(sentry, "Leaked\t%ld\n", leaked); storeAppendPrintf(sentry, "Dubious\t%ld\n", dubious); storeAppendPrintf(sentry, "Reachable\t%ld\n", reachable); storeAppendPrintf(sentry, "Suppressed\t%ld\n", suppressed); } #endif }