axis-c-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Lilantha Darshana <ldarsh...@edocs.com>
Subject RE: Use of STL map (and memory leaks) - patch
Date Mon, 19 Apr 2004 00:08:43 GMT
The rule of thumb is if you use STL map and using dynamically allocated
objects in the value (in a key-value pair), it is your responsibility to 
deallocate all the values from the map. The std::map does not call the 
destructors of pointer variables stored in the std::pairs of std::map.

Our impl. of a map is not a good approach because, you can't defeat the 
power & performance of the STL impl.

Try to avoid STL as possible as you can, and go for a simple solution, and
go for STL where you can't avoid it -- for example if you want to store and
retrieve more than one key-value pairs and does not know exactly how many 
objects to store then you have to think about using std::map, because it has

everything you want. And need to think about portability as well.

Correct way to handle this is to pass in a allocator<T>, you can create your

own custom allocators and impl. void allocator::destroy(pointer p) and
void allocator::deallocate(pointer ptr, sise_type num) method correctly.


regards
-Lilantha


-----Original Message-----
From: Samisa Abeysinghe [mailto:samisa_abeysinghe@yahoo.com]
Sent: Wednesday, April 14, 2004 1:45 AM
To: axis-c-dev@ws.apache.org
Subject: Use of STL map (and memory leaks) - patch


Hi All,
    The use of STL map seems to cause memeory leaks (it leaves 'reachable
memory' behind after
client terminates)
	It is quite possible to do without the STL map in some cases. I have
done this for
SoapKeywordMapping. (see patch below)
	In cases it is difficult to replace maps with arrays (as an exampel
when key is a string) we may
have to look for alternatives.
	
	Is it possible to replace STL map with our own map? (I think to do
this without templates, we
have to stick to string keys)
	
	I also fixed some memory leaks in SoapKeywordMapping code. Need to
call
SoapKeywordMapping::uninitialize () from Axis.cpp uninitialize_module() to
make this effextive.

	Please update the CVS with the patch. (SoapKeywordMapping.h,
SoapKeywordMapping.cpp and Axis.cpp)
Thanks,
Samisa...	

Here is the patch...

---------------------------------------------

diff -u ../../original/ws-axis/c/src/soap/SoapKeywordMapping.h
./src/soap/SoapKeywordMapping.h
--- ../../original/ws-axis/c/src/soap/SoapKeywordMapping.h      2004-04-08
16:42:47.000000000
+0600
+++ ./src/soap/SoapKeywordMapping.h     2004-04-13 15:57:01.000000000 +0600
@@ -17,6 +17,7 @@
 #if !defined(AXIS_SOAPKEYWORDMAPPING_H__INCLUDED_)
 #define AXIS_SOAPKEYWORDMAPPING_H__INCLUDED_

+#include "../common/AxisUtils.h"
 #include <axis/server/SoapEnvVersions.h>
 #include <map>

@@ -43,11 +44,13 @@
     SoapKeywordMapping();
     virtual ~SoapKeywordMapping();
 private:
-    static map<int, SoapKeywordStruct> m_Map;
+    //static map<int, SoapKeywordStruct> m_Map;
+    static SoapKeywordStruct m_Map[VERSION_LAST];
     static volatile bool m_bInit;
 public:
     static void Initialize();
-    static const SoapKeywordStruct& Map(int nVersion);
+    static void uninitialize();
+    static const SoapKeywordStruct& Map(int nVersion);
 };

 #endif

 --------------------------------------------

diff -u ../../original/ws-axis/c/src/soap/SoapKeywordMapping.cpp
./src/soap/SoapKeywordMapping.cpp
--- ../../original/ws-axis/c/src/soap/SoapKeywordMapping.cpp    2004-04-08
15:40:27.000000000
+0600
+++ ./src/soap/SoapKeywordMapping.cpp   2004-04-13 15:59:01.000000000 +0600
@@ -21,9 +21,10 @@

 #include "SoapKeywordMapping.h"
 #include <axis/server/Attribute.h>
-#include "../common/AxisUtils.h"
+//#include "../common/AxisUtils.h"

-map<int, SoapKeywordStruct> SoapKeywordMapping::m_Map;
+//map<int, SoapKeywordStruct> SoapKeywordMapping::m_Map;
+SoapKeywordStruct SoapKeywordMapping::m_Map[VERSION_LAST];
 volatile bool SoapKeywordMapping::m_bInit = false;

 #define __TRC(X) AxisUtils::ToAxisXMLCh(X)
@@ -35,7 +36,7 @@

 SoapKeywordMapping::~SoapKeywordMapping()
 {
-
+//    m_Map.clear();
 }

 void SoapKeywordMapping::Initialize()
@@ -44,9 +45,9 @@
     {
         for (int sv = SOAP_VER_1_1; sv < VERSION_LAST; sv++)
         {
-            m_Map[sv].pchNamespaceUri =
+            m_Map[sv].pchNamespaceUri =
                 __TRC(gs_SoapEnvVersionsStruct[sv].pchNamespaceUri);
-            m_Map[sv].pchPrefix =
+            m_Map[sv].pchPrefix =
                 __TRC(gs_SoapEnvVersionsStruct[sv].pchPrefix);
             for (int sw = SKW_ENVELOPE; sw < SOAP_WORDS_LAST; sw++)
             {
@@ -72,6 +73,21 @@
     }
 }

+void SoapKeywordMapping::uninitialize()
+{
+    if (m_bInit)
+    {
+        /* soap 1.1 envelop attributes */
+        delete m_Map[SOAP_VER_1_1].pEnv;
+        delete m_Map[SOAP_VER_1_1].pXsi;
+        delete m_Map[SOAP_VER_1_1].pXsd;
+        delete m_Map[SOAP_VER_1_2].pEnv;
+        delete m_Map[SOAP_VER_1_2].pXsi;
+        delete m_Map[SOAP_VER_1_2].pXsd;
+        m_bInit = false;
+    }
+}
+
 const SoapKeywordStruct& SoapKeywordMapping::Map(int nVersion)
 {
     return m_Map[nVersion];

--------------------------------------------------------
diff -u ../../original/ws-axis/c/src/engine/Axis.cpp src/engine/Axis.cpp
--- ../../original/ws-axis/c/src/engine/Axis.cpp        2004-04-08
18:07:54.000000000 +0600
+++ src/engine/Axis.cpp 2004-04-13 11:22:27.000000000 +0600
@@ -316,8 +316,11 @@

 extern "C" int uninitialize_module ()
 {
-    // XMLPlatformUtils::Terminate();
+#ifdef USE_XERCES_PARSER
+    XMLPlatformUtils::Terminate ();
+#endif
     ModuleUnInitialize ();
+    SoapKeywordMapping::uninitialize ();
     return AXIS_SUCCESS;
 }




	
		
__________________________________
Do you Yahoo!?
Yahoo! Tax Center - File online by April 15th
http://taxes.yahoo.com/filing.html

Mime
View raw message