By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,098 Members | 1,881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,098 IT Pros & Developers. It's quick & easy.

gSOAP server problem (memory leak)

P: 1
I don't know if anybody here worked with gSOAP software, but I do hope so. I've made some simple server app using gSOAP example. Everythig worked fine until I switched it into ssl mode. Using it results in huge memory leak.

My question is if anybody has any idea what am I doing wrong or what am I missing?

Here is some of my code:
-----------------------------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. #include "bmdsoap_server.h"
  2. #include "soapH.h"
  3. #include <signal.h>
  4. #include <stdio.h>
  5. #include <wchar.h>
  6. #include <bmd/libbmdpr/bmd_datagram/bmd_datagram_set.h>
  7. #include <bmd/libbmd/libbmd.h>
  8. #include <bmd/libbmdconfig/libbmdconfig.h>
  9. #include <bmd/libbmdts/libbmdts.h>
  10. //#include <conf_sections.h>
  11. #include <stdlib.h>
  12. #include <openssl/ssl.h>
  13. #include <openssl/x509.h>
  14. #include <openssl/ssl.h>
  15. #include <openssl/err.h>
  16. #include <openssl/bio.h>
  17.  
  18. #include <sys/types.h>
  19. #include <sys/wait.h>
  20.  
  21. void sigchld_handler(int id)
  22. {
  23.     int pid;
  24.     while ((pid = waitpid (-1, &id, WNOHANG)) > 0);
  25. }
  26.  
  27. void *server_function(void *soap)
  28. {
  29.  
  30.      if(soap_serve(soap)!=SOAP_OK)
  31.     {
  32.         printf("Error while serving client's request\n");
  33.         soap_print_fault(soap,stderr);
  34.         }
  35.     count++;
  36.     printf(" \tClient request served\n");
  37.  
  38.     soap_destroy((struct soap*)soap);
  39.       soap_end((struct soap*)soap);
  40.       soap_done((struct soap*)soap);
  41.       free(soap);
  42.  
  43.     return NULL;
  44. }
  45.  
  46. int main(int argc, char *argv[])
  47. {
  48.  
  49. int status=0;
  50. struct soap soap;
  51. struct soap *psoap=NULL;
  52. int m,s;
  53. pid_t pid=0;
  54.  
  55.     signal(SIGCHLD,sigchld_handler);
  56.  
  57.     system("clear");
  58.     printf("soap server puppy started\n");
  59.     soap_ssl_init();
  60.  
  61.     soap_init(&soap);
  62.     soap.bind_flags=SO_REUSEADDR;
  63.     soap.send_timeout = 10; // 60 seconds
  64.     soap.recv_timeout = 10; // 60 seconds
  65.     soap.max_keep_alive = 60; // max keep-alive sequence 
  66.  
  67.     if(soap_ssl_server_context(&soap,SOAP_SSL_REQUIRE_CLIENT_AUTHENTICATION," cert.pem","12345678",NULL,"cert_path",NULL,NULL,argv[0]))
  68.     {
  69.         printf("Error in setting encryption context\n");
  70.         soap_print_fault(&soap,stderr);
  71.         return -1;
  72.     }
  73.  
  74.     m=soap_bind(&soap,"127.0.0.1",9999,1000);
  75.     if(m<0)
  76.     {
  77.         printf("Error in binding socket\n");
  78.         soap_print_fault(&soap,stderr);
  79.         return -1;
  80.     }
  81.  
  82.     for(;;)
  83.     {
  84.         s = soap_accept(&soap);
  85.                 if(s<0)
  86.                 {
  87.             printf("Error in accepting connection\n");
  88.                         soap_print_fault(&soap,stderr);
  89.                         continue;
  90.                 }
  91.  
  92.         psoap=soap_copy(&soap);
  93.                 if(psoap==NULL)
  94.         {
  95.             printf("Error in duplication connection pointer\n");
  96.                     break;
  97.         }
  98.  
  99.         if(soap_ssl_accept(psoap))
  100.         {
  101.             printf("Error in ssl accepting connection\n");
  102.             soap_print_fault(&soap,stderr);
  103.             soap_end(psoap);
  104.             soap_done(psoap);
  105.             free(psoap);
  106.             //soap_free(psoap);
  107.             continue;
  108.         }
  109.         else
  110.         {
  111.             X509 *peer_cert=NULL;
  112.             peer_cert=SSL_get_peer_certificate(psoap->ssl);
  113.             if(peer_cert!=NULL)
  114.             {
  115.                 char issuer[256];
  116.                 char subject[256];
  117.                 X509_NAME_oneline(X509_get_issuer_name(peer_cert), issuer, 256);
  118.                 printf("Client certificate's issuer: %s\n",issuer);
  119.                 X509_NAME_oneline(X509_get_subject_name(peer_cert), subject, 256);
  120.                 printf("Client certificate's subject: %s\n",subject);
  121.             }
  122.             else
  123.             {
  124.                 printf("peer chujnia...\n");
  125.             }
  126.         }
  127.  
  128.         pid=fork();
  129.         if(pid==0)
  130.         {
  131.  
  132.             server_function((void *)psoap);
  133.             exit(0);
  134.         }
  135.         else
  136.         {
  137.             //soap_done(psoap);
  138.             free(psoap); psoap=NULL;
  139.             close(s);
  140.             /*czynności serwera :) */
  141.         }
  142.     }
  143.     return BMD_OK;
  144. }
  145.  
  146. int ns__testConnection(struct soap *soap, char **result)
  147. {
  148.     *result = (char*) soap_malloc(soap, 100);
  149.     sprintf(*result, "OK %i",1);
  150.     return SOAP_OK;
  151. }
-----------------------------------------------------------------------------------------------------

And here is what valgrind sais about emory leaks:
-----------------------------------------------------------------------------------------------------
92,971,064 (53,332 direct, 92,917,732 indirect) bytes in 199 blocks are definitely lost in loss record 5 of 10
==21641== at 0x40063C0: malloc (vg_replace_malloc.c:149)
==21641== by 0xBB9C5D: (within /lib/libcrypto.so.0.9.8b)
==21641== by 0xBBA2DE: CRYPTO_malloc (in /lib/libcrypto.so.0.9.8b)
==21641== by 0xCE8299: SSL_new (in /lib/libssl.so.0.9.8b)
==21641== by 0x80502D1: soap_ssl_accept (stdsoap2.c:3040)
==21641== by 0x804A863: main (bmdsoap_server.c:117)
==21641==
==21641==
==21641== 653,513 (23,296 direct, 630,217 indirect) bytes in 4 blocks are definitely lost in loss record 6 of 10
==21641== at 0x40063C0: malloc (vg_replace_malloc.c:149)
==21641== by 0x4BEA20C0: zcalloc (in /usr/lib/libz.so.1.2.3)
==21641== by 0x4BE9FB31: deflateInit2_ (in /usr/lib/libz.so.1.2.3)
==21641== by 0x4BE9FD61: deflateInit_ (in /usr/lib/libz.so.1.2.3)
==21641== by 0xC60927: (within /lib/libcrypto.so.0.9.8b)
==21641== by 0xC6055A: COMP_CTX_new (in /lib/libcrypto.so.0.9.8b)
==21641== by 0xCDCB6A: tls1_change_cipher_state (in /lib/libssl.so.0.9.8b)
==21641== by 0xCD0D65: ssl3_accept (in /lib/libssl.so.0.9.8b)
==21641== by 0xCE60B9: SSL_accept (in /lib/libssl.so.0.9.8b)
==21641== by 0xCD9CEC: ssl23_get_client_hello (in /lib/libssl.so.0.9.8b)
==21641== by 0xCDA52A: ssl23_accept (in /lib/libssl.so.0.9.8b)
==21641== by 0xCE60B9: SSL_accept (in /lib/libssl.so.0.9.8b)
==21641==
==21641==
==21641== 2,659,453 bytes in 118 blocks are possibly lost in loss record 7 of 10
==21641== at 0x40063C0: malloc (vg_replace_malloc.c:149)
==21641== by 0xBB9C5D: (within /lib/libcrypto.so.0.9.8b)
==21641== by 0xBBA2DE: CRYPTO_malloc (in /lib/libcrypto.so.0.9.8b)
==21641== by 0xC6086A: (within /lib/libcrypto.so.0.9.8b)
==21641== by 0xC6055A: COMP_CTX_new (in /lib/libcrypto.so.0.9.8b)
==21641== by 0xCDCB6A: tls1_change_cipher_state (in /lib/libssl.so.0.9.8b)
==21641== by 0xCD0D65: ssl3_accept (in /lib/libssl.so.0.9.8b)
==21641== by 0xCE60B9: SSL_accept (in /lib/libssl.so.0.9.8b)
==21641== by 0xCD9CEC: ssl23_get_client_hello (in /lib/libssl.so.0.9.8b)
==21641== by 0xCDA52A: ssl23_accept (in /lib/libssl.so.0.9.8b)
==21641== by 0xCE60B9: SSL_accept (in /lib/libssl.so.0.9.8b)
==21641== by 0x80504FC: soap_ssl_accept (stdsoap2.c:3062)
==21641==
==21641==
==21641== 32,831,536 bytes in 530 blocks are possibly lost in loss record 9 of 10
==21641== at 0x40063C0: malloc (vg_replace_malloc.c:149)
==21641== by 0x4BEA20C0: zcalloc (in /usr/lib/libz.so.1.2.3)
==21641== by 0x4BEA2251: inflateInit2_ (in /usr/lib/libz.so.1.2.3)
==21641== by 0x4BEA2312: inflateInit_ (in /usr/lib/libz.so.1.2.3)
==21641== by 0xC608BC: (within /lib/libcrypto.so.0.9.8b)
==21641== by 0xC6055A: COMP_CTX_new (in /lib/libcrypto.so.0.9.8b)
==21641== by 0xCDCB6A: tls1_change_cipher_state (in /lib/libssl.so.0.9.8b)
==21641== by 0xCD0D65: ssl3_accept (in /lib/libssl.so.0.9.8b)
==21641== by 0xCE60B9: SSL_accept (in /lib/libssl.so.0.9.8b)
==21641== by 0xCD9CEC: ssl23_get_client_hello (in /lib/libssl.so.0.9.8b)
==21641== by 0xCDA52A: ssl23_accept (in /lib/libssl.so.0.9.8b)
==21641== by 0xCE60B9: SSL_accept (in /lib/libssl.so.0.9.8b)
==21641==
==21641== LEAK SUMMARY:
==21641== definitely lost: 76,628 bytes in 203 blocks.
==21641== indirectly lost: 93,547,949 bytes in 6,315 blocks.
==21641== possibly lost: 35,490,989 bytes in 648 blocks.
==21641== still reachable: 5,806,217 bytes in 36,061 blocks.
==21641== suppressed: 0 bytes in 0 blocks.
==21641== Reachable blocks (those to which a pointer was found) are not shown.
==21641== To see them, rerun with: --show-reachable=yes
-----------------------------------------------------------------------------------------------------

Please help my, because it blows my mind...

Regards.
Jan 15 '08 #1
Share this Question
Share on Google+
1 Reply


P: 1
I just skimmed over the code. Don't the soap objects (in your case, 'psoap' in the main function, and 'soap' in server_function()) have a destructor? I think it's in the stdsoap2.cpp file.

In any case, I notice that you're freeing up the memory using soap_destroy(), soap_end(), and soap_done(). Essentially, you are doing what the destructor does. So, perhaps you can just do 'delete psoap;' instead.

Also, after you do that, you don't need to do 'free(psoap);'

Try that and write back if it works or not. Good luck.
Mar 18 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.