Memory leak in watermeter C program.

Post Reply
eeweew
Posts: 5
Joined: Monday 02 November 2015 16:34
Target OS: Raspberry Pi
Domoticz version:
Contact:

Memory leak in watermeter C program.

Post by eeweew » Saturday 25 June 2016 19:06

So I did decide do reinvent the wheel in yet another way. I am reading my watermeter with a CNY70 and a comparator. I did optimize all the resistor values as best as I could by calculations and experiments. When I tried to use wiringPiISR I found a lot of false and/our double interrupts, but I didn't want to go to polling so I developed an hybrid system, resulting in the following C program.

Code: Select all

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <wiringPi.h>
#include <limits.h>
#include <sys/time.h>

void myInterrupt1(void);

void printtime(){
	char buffer[30];
	struct timeval tv;
	
	time_t curtime;
	
	gettimeofday(&tv, NULL); 
	curtime=tv.tv_sec;

	strftime(buffer,30,"%m-%d-%Y  %T.",localtime(&curtime));
	printf("%s%ld\n",buffer,tv.tv_usec);
	fflush(stdout);
}

void callJson(void){
	struct timeval tv;
	gettimeofday(&tv, NULL);
	int time = tv.tv_sec;
	static int timeprev = 0;
	
	if (time - timeprev > 1){
		timeprev = time;
		system("./watermeter.sh");
		printf("SEND!!!!\n");
		fflush(stdout);
	}
}
	

void myInterrupt0(void){
	if (digitalRead(1)==1){
		wiringPiISR(1, INT_EDGE_FALLING, &myInterrupt1);
		printf("rising\n");
		printtime();
		callJson();
	}
}

void myInterrupt1(void){
	if (digitalRead(1)==0){
		wiringPiISR(1, INT_EDGE_RISING, &myInterrupt0);
		printf("falling\n");
		printtime();
	}
}

int main (void){
	wiringPiSetup();
	if (digitalRead(1)==0){
		wiringPiISR(1, INT_EDGE_RISING, &myInterrupt0);
	}
	else{
		wiringPiISR(1, INT_EDGE_FALLING, &myInterrupt1);
	}
//	printtime();
	for(;;){
		sleep(UINT_MAX);
	}	
	return 0;
There is currently some clutter that should not be there (the whole printtime function is there for diagnostic reasons only), but what I do is is in essence: On initial startup check pin, set up interrupt to look for falling, else for falling. On interrupts look if state really is what it should be, if this is true set the other interrupt, do system call to sh script to communicate with Domoticz in falling interrupt from a function that checked that it did happen long enough ago to filter double interrupts.

This approach uses a lot of workarounds but it does seem to work reliability, there is only one problem. After about 24 hours (depending on the water usage), the program has a vmem size of more than 3GB and stops working, and I really have no idea why. I don't do any memory management myself, so I don't see how there could be a leak. One hypothesis I had is that WiringPi didn't like initializing the pin again all the time, so I am now running a version that uses this construction:

Code: Select all

system("gpio edge 1 rising");
wiringPiISR(1, INT_EDGE_SETUP, &myInterrupt0);
in the interrupts, but that does seem to do the same thing. Any idea's about what could be going on here?

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests