Skip to content

Instantly share code, notes, and snippets.

@deepcube
Last active December 16, 2015 10:49
Show Gist options
  • Save deepcube/5422810 to your computer and use it in GitHub Desktop.
Save deepcube/5422810 to your computer and use it in GitHub Desktop.
Continually create and destroy contexts and sockets to check for memory leaks. None found. To run download both files into a directory and run: sh many_test.sh
#include <czmq.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <zmq.h>
#define zcheck_err(eval, condition, fmt, args...) \
((void)({ \
errno = 0; \
if (!(condition)) { \
fprintf(stderr, "%s: " fmt "%s%s\n", prog_name, ##args, errno ? ": " : "", errno ? zmq_strerror(errno) : ""); \
exit(eval); \
} \
}))
char *prog_name;
/*
* Create and destory contexts and sockets
*
* Comment out any parts you don't want to test
*/
int main(int argc, char **argv)
{
zctx_t *ctx;
void *soc;
prog_name = argv[0];
zcheck_err(EXIT_FAILURE, argc == 2, "Supply address as only argument");
for (;;) {
zcheck_err(EXIT_FAILURE, ctx = zctx_new() , "Error creating context" );
zcheck_err(EXIT_FAILURE, soc = zsocket_new(ctx, ZMQ_DEALER) , "Error creating socket" );
zcheck_err(EXIT_FAILURE, 0 == zsocket_connect(soc, argv[1]) , "Error connecting to %s" , argv[1]);
zcheck_err(EXIT_FAILURE, 0 == zsocket_disconnect(soc, argv[1]), "Error disconnecting from %s", argv[1]);
zsocket_destroy(ctx, soc);
zctx_destroy(&ctx);
}
return 0;
}
#!/bin/sh
trap die INT TERM
die() {
[ -n "$pid" ] && kill -9 $pid
printf "$0: $@\n" 1>&2
exit
}
gcc -lzmq -lczmq -Wall -Werror -o many many.c || die "Error compiling many.c"
./many &
pid=$!
ps $pid > /dev/null 2>&1 || die "Error starting many"
echo "pid is $pid. Press [Enter] to continue."
read unused
while sleep 1; do
egrep "Vm" /proc/$pid/status | sed ':l; N; $!bl; s/[\n\t ]\+/ /g;'
done
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment