[완료] 메모리 누수현상

newtypes의 이미지

안녕하세요

아래의 서버 프로그램을 수행중 지속적으로 메모리 누수가 생기는 것을 확인하여
여러므로 수정해 보았으나 문제가 해결되지 않아 질문드립니다.

해당 프로그램은 데몬으로 수행하면서 클라이언트에서 보낸 파일을 해당 디렉토리에 저장하는 프로그램입니다.

목적하는 작업은 정상적으로 수행되고 있지만 메모리 누수가 1시간에 4Kbytes 정도 생기고 있습니다.

어느부분이 잘못된 것인지 지적해 주시면 고맙겠습니다.

PS. 즐거운 명절 보내세요 ^^

#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include 
#include  
#include  

#define SOCKFILE "/tmp/infosock" 

// ------------------------------
// 전역 자료 선언
// ------------------------------
typedef struct ClientData
{
	unsigned long int addr; 
	int port;

} ClientDataInfo;

using namespace std;

multimap ClientMonitorData; 

// 쓰레드 전역 변수 
pthread_mutex_t sync_mutex;
pthread_cond_t  sync_cond;
pthread_mutex_t gmutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t  gcond  = PTHREAD_COND_INITIALIZER;

// 쓰레드 전역 함수
void *thread_comm(void *);
// ------------------------------
// END
// ------------------------------


void *thread_comm(void *data)
{
	int sockfd;
	ClientDataInfo clientinfo; 

	struct sockaddr_in clientaddr; 
	int client_len = sizeof(clientaddr), fncnt=0;
	char buf[256];
	char filename[128];
	FILE *fp;

	pthread_detach(pthread_self());

	memcpy((void *)&sockfd, (char *)data, sizeof(int)); 
	memcpy((void *)&clientaddr, (char *)data+sizeof(int), client_len); 

	clientinfo.addr = clientaddr.sin_addr.s_addr;
	clientinfo.port = ntohs(clientaddr.sin_port);

	ClientMonitorData.insert(pair(clientinfo.addr, clientinfo));
	memset(filename, 0x00, 128);

	while(1)
	{	
		memset(buf, 0x00, 256);
		read(sockfd, buf, 256);

		if(fncnt == 0) {
			sprintf(filename, "/usr/local/apache/htdocs/report/%s",buf);
			unlink(filename);
			fncnt=1;
			memset(buf, 0x00, 256);
			continue;
		}

		fp = fopen(filename, "a+");

		
		if (strncmp(buf, "end", strlen(buf)) == 0)
		{
			fclose(fp);
			close(sockfd);
			memset(buf, 0x00, 256);
			pthread_exit((void *)NULL);
		}
		fputs(buf, fp);
		fclose(fp);
	}

	fncnt = 0;
}


int main(int argc, char **argv)
{
	pthread_t p_thread;
	struct sockaddr_in clientaddr, serveraddr;
	char th_data[256];

	int server_sockfd, client_sockfd, client_len; 

	memset(th_data, 0x00, 256);

	signal(SIGPIPE, SIG_IGN);

	if (argc != 2)
	{
		fprintf(stdout,"Usage: info_server [port]\n예   : info_server 44\n");
		exit(0);
	}

	if ((server_sockfd = socket(AF_INET, SOCK_STREAM, 0)) 
익명사용자의 이미지

현 소스상에서는 멀티맵(ClientMoniterData)에 들어간 데이터를 지우는 부분이 없어서 생기는 현상 같습니다.

newtypes의 이미지

필요없는 멀티맵 부분을 없에고 다시 컴파일 하여 실행중입니다.

그냥 간과했던 부분이 였는데 역시 문제가 되는 모양이네

지적 감사합니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.