2

Using this simple program:

#include "fcgi_stdio.h"

int main(void)
{
  while(FCGI_Accept() >= 0)
  {
  }

  FCGI_Finish();

  return(0);
}

I get this result from valgrind:

Memcheck, a memory error detector
Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
Command: ./val

HEAP SUMMARY:
  in use at exit: 768 bytes in 1 blocks
total heap usage: 1 allocs, 0 frees, 768 bytes allocated

768 bytes in 1 blocks are still reachable in loss record 1 of 1
  at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  by 0x4E3D986: OS_LibInit (os_unix.c:171)
  by 0x4E3C80A: FCGX_Init (fcgiapp.c:2088)
  by 0x4E3C89A: FCGX_IsCGI (fcgiapp.c:1946)
  by 0x4E3CCA4: FCGI_Accept (fcgi_stdio.c:120)
  by 0x4006F6: main (in /home/[me]/kod/val)

LEAK SUMMARY:
  definitely lost: 0 bytes in 0 blocks
  indirectly lost: 0 bytes in 0 blocks
    possibly lost: 0 bytes in 0 blocks
  still reachable: 768 bytes in 1 blocks
       suppressed: 0 bytes in 0 blocks

For counts of detected and suppressed errors, rerun with: -v
ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

How do I free all memory correctly after using FCGI?

Olle Härstedt
  • 3,799
  • 1
  • 24
  • 57
  • This may be a bug in FCGI iteself. Is there a function such as FCGI_CLeanup? It seems to be a small amount of memory not freed. If it increases with running time, then you have a leak, otherwise you can ignore it (or fix FCGI ;) ). – roelofs Aug 15 '14 at 09:14
  • By 'running time' I mean actually having FCGI doing things as you would in production, over a period of time that a server would be running (or a process, or whatever the case may be). I'd suggest testing it with production code if you can. – roelofs Aug 15 '14 at 09:18

1 Answers1

2

I ran into the same issue. Seems like a bug in FCGI. Workaround is calling a library function directly for cleanup. OS_LibShutdown() frees the memory init by FCGI_Accept() which internally calls FCGX_Init(). For multithread apps, you have to call FCGX_Init() yourself.

// Declare this (extern "C" is only required if from CPP)...
extern "C"
{
void OS_LibShutdown(void);
}

// From clean up code, call this...
OS_LibShutdown();
Yasser Asmi
  • 1,150
  • 2
  • 12
  • 27