++ed by:
Author image David Leadbeater


Devel::Valgrind::Client - Make valgrind memcheck client requests


  # Run the following code under Valgrind

  use Devel::Valgrind::Client qw(leak_check);

  my $result = leak_check {
    # Code to check here.

    # The following two lines are deliberately leaky:
    my $x = "a" x 1_000;
    Internals::SvREFCNT($x, 2); # Don't do this in real code, please

  # Should print just over 1000 (i.e. SvLEN + sizeof(SVPV))
  warn "Lost $result->{leaked} bytes";


Valgrind provides the ability for a program that is running under Valgrind's memcheck tool ("being valgrinded") to make requests to the Valgrind VM through macros defined in <valgrind/memcheck.h>.

This module provides a way to access some of these calls from Perl, such as to find out if a program is running under memcheck, to force a leak check operation and retrieve statistics of the check.

The reason this module was created was to test XS code does not leak, however note that for many cases Test::Valgrind will be a better choice. The difference compared to Test::Valgrind is this runs within the same process; it is your responsibility to arrange for the program to run under Valgrind.

(Potentially it can also be used to count how many bytes are allocated -- by deliberately leaking memory -- although there are other choices for that, such as Devel::Mallinfo)

To compile this module you will need Valgrind installed, as it uses a header file Valgrind provides.


Nothing by default. The following are available by request:

  • leak_check($coderef)

    A high level interface.

    The steps performed are as follows:

    • Request memcheck to run a leak check and record the statistics.

    • Run the code reference given as an argument.

    • Request memcheck to perform a leak check again and take the values that were reported previously from the leak statistics.

    The return value is as per /count_leaks, with the statistics adjusted appropriately.

  • is_in_memcheck

    Returns true if perl is running under memcheck, false otherwise.

  • count_leaks

    Returns a hash reference: leaked dubious reachable suppressed

  • count_leaks_blocks

  • do_leak_check

  • do_leak_check_quick


This program is free software. It comes without any warranty, to the extent permitted by applicable law. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See http://sam.zoy.org/wtfpl/COPYING or Software::License::WTFPL_2 for more details.


David Leadbeater <dgl@dgl.cx>, 2010