diff --git a/include/ByteStream.h b/include/ByteStream.h
index a5f13e18603fd4a300f1141d5ed51edb0b1edebf..9920f83ba9fb6d5140a04e404f2759842bccafd8 100644
--- a/include/ByteStream.h
+++ b/include/ByteStream.h
@@ -19,10 +19,13 @@
 #define _BYTESTREAM_H
 
 #include "PacketLibDefinition.h"
+#include "SharedPtr.h"
 
 namespace PacketLib
 {
 
+class ByteStream;
+typedef SharedPtr<ByteStream> ByteStreamPtr;
 
 ///	\brief Represent a stream of byte.
 class ByteStream
@@ -45,7 +48,7 @@ public:
     /// passed as input.
     /// It's possibile to pass 0 as pointer.
     /// The mamory of byte* is allocated.
-    ByteStream(ByteStream* b0, ByteStream* b1, ByteStream* b2);
+    ByteStream(ByteStreamPtr b0, ByteStreamPtr b1, ByteStreamPtr b2);
 
     ~ByteStream();
 
@@ -62,17 +65,17 @@ public:
     /// Returns a subset of the current stream. If there is problemas return NULL
     /// \remarks This method don't allocate a new stream structure in memory,
     /// but create only a new ByteStream object that points in the same memory area.
-    ByteStream* getSubByteStream(dword first, dword last);
+    ByteStreamPtr getSubByteStream(dword first, dword last);
 
     /// Returns a subset of the current stream. If there is problemas return NULL
     /// \remarks This method allocate a new stream structure in memory.
-    ByteStream* getSubByteStreamCopy(dword first, dword last);
+    ByteStreamPtr getSubByteStreamCopy(dword first, dword last);
 
     /// Sets the stream from arguments.
     bool setStream(byte* b, dword dim, bool bigendian, bool memory_sharing = true);
 
     /// Get the stream from another object of the same type. Don't allocate new memory
-    bool setStream(ByteStream* b, dword first, dword last);
+    bool setStream(ByteStreamPtr b, dword first, dword last);
 
     /// Copy the stream in argument of dimension dim.
     /// \remarks This method delete old stream and creates a new stream in memory
@@ -157,6 +160,7 @@ private:
     bool mem_allocation_constructor;
 
 };
+
 }
 
 
diff --git a/include/File.h b/include/File.h
index 3839cb67faae85357175ff7cd38bb4822d20b2a3..eafc1f1d2d6352cd96712d2253efab46bffbc1b0 100644
--- a/include/File.h
+++ b/include/File.h
@@ -61,7 +61,7 @@ public:
     ///  Reads a pair number of byte from opened file and manages the big or little endian
     ///  format. If the format of machine is little endian, a pair of byte is swapped.
     ///  \pre The file must be opened.
-    virtual ByteStream* getNByte( dword N = 1 );
+    virtual ByteStreamPtr getNByte( dword N = 1 );
 
     ///  Get the current line.
     ///  \pre The file must be opened.
@@ -110,7 +110,7 @@ public:
 
     ///  Writes a stream of byte into opend file.
     ///  \pre The file must be opened in w mode
-    bool writeByteStream(ByteStream* b) throw(PacketExceptionIO*);
+    bool writeByteStream(ByteStreamPtr b) throw(PacketExceptionIO*);
 
     ///  Count the number of string lines into a text file.
     long getNumberOfStringLines();
diff --git a/include/FileStream.h b/include/FileStream.h
index 61736dd8017f985c80a60c1630e76768c646415b..ebccb50e353e5e113a784b475496cafb5a47cac9 100644
--- a/include/FileStream.h
+++ b/include/FileStream.h
@@ -30,11 +30,11 @@ public:
 
     FileStream(bool prefix, bool bigen, word dimprefix, long startposition = 0);
 
-    ByteStream* readPrefix();
+    ByteStreamPtr readPrefix();
 
-    ByteStream* readHeader(unsigned int dimHeader);
+    ByteStreamPtr readHeader(unsigned int dimHeader);
 
-    ByteStream* readDataField(unsigned int dimDataField);
+    ByteStreamPtr readDataField(unsigned int dimDataField);
 
 private:
     ///  Indicates if it's present a prefix for each packet
@@ -42,11 +42,11 @@ private:
     ///  \li false if it isn't present
     bool thereIsPrefix;
 
-    ByteStream* header;
+    ByteStreamPtr header;
 
-    ByteStream* dataField;
+    ByteStreamPtr dataField;
 
-    ByteStream* prefix;
+    ByteStreamPtr prefix;
 
     word dimPrefix;
 
diff --git a/include/Input.h b/include/Input.h
index 49dd221013001d4fdb61f0177853c85505ab8154..031d7990a29083b3ff38eaf616fa7d0775bbe11b 100644
--- a/include/Input.h
+++ b/include/Input.h
@@ -35,7 +35,7 @@ public:
 
     virtual bool open( char** parameters ) throw(PacketExceptionIO*) = 0;
 
-    virtual ByteStream* readByteStream(dword n_byte) throw(PacketExceptionIO*) = 0;
+    virtual ByteStreamPtr readByteStream(dword n_byte) throw(PacketExceptionIO*) = 0;
 
     virtual char* readString() throw(PacketExceptionIO*) = 0;
 
diff --git a/include/InputFile.h b/include/InputFile.h
index b802db431042f2f40483dde88dc0488b378e4a10..0697cf010c1e657a496a4fbe727094c90433f9af 100644
--- a/include/InputFile.h
+++ b/include/InputFile.h
@@ -37,7 +37,7 @@ public:
 
      virtual void close() throw(PacketExceptionIO*);
 
-    virtual ByteStream* readByteStream(dword n_byte) throw(PacketExceptionIO*);
+    virtual ByteStreamPtr readByteStream(dword n_byte) throw(PacketExceptionIO*);
 
      virtual char* readString() throw(PacketExceptionIO*);
 
diff --git a/include/InputPacketStream.h b/include/InputPacketStream.h
index de3f7445bae4b900660cf281fffd9ef7f0138036..34a2e0587ab1c48a31b14f6a1d63d22fee2756cd 100644
--- a/include/InputPacketStream.h
+++ b/include/InputPacketStream.h
@@ -40,12 +40,12 @@ public:
     /// \param prefix A ByteStream that contains the prefix of packet (if present).
     /// \param packetHeader A ByteStream that contains the packet header.
     /// \param packetDataField A ByteStream that contains the packet data field.
-    int detPacketType(ByteStream* prefix, ByteStream* packetHeader, ByteStream* packetDataField);
+    int detPacketType(ByteStreamPtr prefix, ByteStreamPtr packetHeader, ByteStreamPtr packetDataField);
 
     /// \return The index of packet type if it's recognized. 0 if packet isn't recognized.
     /// \param prefix A ByteStream that contains the prefix of packet (if present).
     /// \param packet A ByteStream that contains the packet.
-    int detPacketType(ByteStream* prefix, ByteStream* packet);
+    int detPacketType(ByteStreamPtr prefix, ByteStreamPtr packet);
 
     ///It returns the index of the packet type contained in the stream. The stream* contains also the prefix (if present)
     /// \return The index of packet type if it's recognized. 0 if packet isn't recognized.
diff --git a/include/InputSerial.h b/include/InputSerial.h
index 30b397d5f684b416bfcff5521a673d7bc68876c5..c721e4d9ef18de8d6b45682605deecce1d61db3e 100644
--- a/include/InputSerial.h
+++ b/include/InputSerial.h
@@ -37,7 +37,7 @@ public:
 
     virtual void close() throw(PacketExceptionIO*);
 
-    virtual ByteStream* readByteStream(int n_byte) throw(PacketExceptionIO*);
+    virtual ByteStreamPtr readByteStream(int n_byte) throw(PacketExceptionIO*);
 
     virtual char* readString() throw(PacketExceptionIO*);
 
diff --git a/include/InputSocketServer.h b/include/InputSocketServer.h
index 7fcb8c4b2e7aa6a3f65fe5838808bc42eec086c3..30ac6327c3ccc6a07f84a26df65daee7a9f2ae7f 100644
--- a/include/InputSocketServer.h
+++ b/include/InputSocketServer.h
@@ -37,7 +37,7 @@ public:
 
     virtual void close() throw(PacketExceptionIO*);
 
-    virtual ByteStream* readByteStream(dword n_byte) throw(PacketExceptionIO*);
+    virtual ByteStreamPtr readByteStream(dword n_byte) throw(PacketExceptionIO*);
 
 
     virtual char* readString() throw(PacketExceptionIO*);
diff --git a/include/Output.h b/include/Output.h
index ab14441bf7df238d3fb05e29b385ce87564590f3..980dbf561b833b1d9734e6856aa6872051fe7889 100644
--- a/include/Output.h
+++ b/include/Output.h
@@ -39,7 +39,7 @@ public:
 
     virtual bool isBigendian();
 
-    virtual bool writeByteStream(ByteStream* b) throw(PacketExceptionIO*) = 0;
+    virtual bool writeByteStream(ByteStreamPtr b) throw(PacketExceptionIO*) = 0;
 
     virtual bool writeString(const char* str) throw(PacketExceptionIO*) = 0;
 
diff --git a/include/OutputFile.h b/include/OutputFile.h
index 893f9921234c359b7b7563251de2c87d9cb9bb87..bf0e98ba30a6a4a3d28876ae5f7b37557c60ae48 100644
--- a/include/OutputFile.h
+++ b/include/OutputFile.h
@@ -38,7 +38,7 @@ public:
 
     virtual bool open(char** parameters) throw(PacketExceptionIO*);
 
-    virtual bool writeByteStream(ByteStream* b) throw(PacketExceptionIO*);
+    virtual bool writeByteStream(ByteStreamPtr b) throw(PacketExceptionIO*);
 
     virtual bool writeString(const char* str) throw(PacketExceptionIO*);
 
diff --git a/include/OutputSerial.h b/include/OutputSerial.h
index 570c587f3856571f39694cad9e2c55bd35218510..0a316a0ad521b6ed8c497d4e01108346360e255b 100644
--- a/include/OutputSerial.h
+++ b/include/OutputSerial.h
@@ -38,7 +38,7 @@ public:
 
     virtual bool open(char** parameters) throw(PacketExceptionIO*);
 
-    virtual bool writeByteStream(ByteStream* b) throw(PacketExceptionIO*);
+    virtual bool writeByteStream(ByteStreamPtr b) throw(PacketExceptionIO*);
 
     virtual bool writeString(const char* str) throw(PacketExceptionIO*);
 
diff --git a/include/OutputSocketClient.h b/include/OutputSocketClient.h
index aa41ca3e0462c2de58ee71f6db04d2daa5a2d46c..1ad78388e5b9309357db614e268c1694525e76ef 100644
--- a/include/OutputSocketClient.h
+++ b/include/OutputSocketClient.h
@@ -37,7 +37,7 @@ public:
 
     virtual bool open(char** parameters) throw(PacketExceptionIO*);
 
-    virtual bool writeByteStream(ByteStream* b) throw(PacketExceptionIO*);
+    virtual bool writeByteStream(ByteStreamPtr b) throw(PacketExceptionIO*);
 
     virtual bool writeString(const char* str) throw(PacketExceptionIO*);
 
diff --git a/include/Packet.h b/include/Packet.h
index 5eec893c1ebbf4698c0cf27aaba23b3134be17c7..0e31e5503e3366a2c7d2fe2dea5ae1dc810cc144 100644
--- a/include/Packet.h
+++ b/include/Packet.h
@@ -55,14 +55,14 @@ public:
     /// contains a packet of this type. This method overloads another method.
     /// \post A side effects of this method is that the value of fields of packet are set with
     /// correct value.
-    virtual bool setAndVerifyPacketValue(ByteStream* prefix, ByteStream* packetHeader, ByteStream* packetDataField);
+    virtual bool setAndVerifyPacketValue(ByteStreamPtr prefix, ByteStreamPtr packetHeader, ByteStreamPtr packetDataField);
 
     /// This method verifies if the ByteStream on argument contains the correct value
     /// in the identifiers. If this is true, the method returns true and the stream
     /// contains a packet of this type. This method overloads another method.
     /// \post A side effect of this method is that the values of fields of packet are set with
     /// correct value
-    virtual bool setAndVerifyPacketValue(ByteStream* prefix, ByteStream* packet);
+    virtual bool setAndVerifyPacketValue(ByteStreamPtr prefix, ByteStreamPtr packet);
 
     /// Sets all the fields of the packet with correct value contained into the input ByteStream.
     ///	\pre The structure of the stream must be loaded.
@@ -70,21 +70,21 @@ public:
     /// \param packetHeader This is the header of the packet
     /// \param packetDataField This is the data field of the packet
     /// \post If return is true all the fields are set with the correct value.
-    virtual bool setPacketValue(ByteStream* prefix, ByteStream* packetHeader, ByteStream* packetDataField);
+    virtual bool setPacketValue(ByteStreamPtr prefix, ByteStreamPtr packetHeader, ByteStreamPtr packetDataField);
 
     /// Sets all the fields of the packet with correct value contained into the input ByteStream.
     ///	\pre The structure of the stream must be loaded.
     /// \param prefix This is the prefix of the packet
     /// \param packet This is the packet
     /// \post If return is true all the fields are set with the correct value.
-    virtual bool setPacketValue(ByteStream* prefix, ByteStream* packet);
+    virtual bool setPacketValue(ByteStreamPtr prefix, ByteStreamPtr packet);
 
     /// Verifies if within the ByteStream passed with arguments it's present a correct packet.
     ///	\pre The structure of the stream must be loaded.
     /// \param prefix This is the prefix of the packet
     /// \param packet This is the packet
     /// \return True if the ByteStream contains a packet
-    bool verifyPacketValue(ByteStream* prefix, ByteStream* packet);
+    bool verifyPacketValue(ByteStreamPtr prefix, ByteStreamPtr packet);
 
     /// Verifies if within the byte* stream passed with arguments it's present a correct packet. The stream* contains also the prefix (if present)
     /// \param stream A pointer to the stream of byte, with prefix and packet
@@ -100,7 +100,7 @@ public:
     /// \param packetHeader This is the header of the packet.
     /// \param packetDataField This is the data field of the packet.
     /// \return True if the ByteStream contains a packet.
-    bool verifyPacketValue(ByteStream* prefix, ByteStream* packetHeader, ByteStream* packetDataField);
+    bool verifyPacketValue(ByteStreamPtr prefix, ByteStreamPtr packetHeader, ByteStreamPtr packetDataField);
 
     /// Prints to stdout the value of packet data field in a formatted mode.
     virtual void printPacketValue();
@@ -148,12 +148,12 @@ public:
 
     
     /// Gets the ByteStream received as input
-    ByteStream* getInputStream();
+    ByteStreamPtr getInputStream();
 
     
     /// Gets the packet generated with the generateStream() method. The output
     /// packet is generated during this call.
-    ByteStream* getOutputStream();
+    ByteStreamPtr getOutputStream();
 
     
     /// Gets the name of packet.
@@ -174,10 +174,6 @@ public:
     virtual void printIdentifiers();
 
     
-    /// Deletes the ByteStream passed as arguments.
-    virtual void deleteExternalByteStream();
-
-    
     /// Writes property of byte packetID.
     virtual void setPacketID(const byte& value);
 
@@ -202,15 +198,15 @@ public:
 
     
     /// the prefix of the packet.
-    ByteStream* prefix;
+    ByteStreamPtr prefix;
 
     
     /// The ByteStrem of the packet read
-    ByteStream* packet;
+    ByteStreamPtr packet;
 
     
     /// This is the ByteStream generated with generateStream().
-    ByteStream* packet_output;
+    ByteStreamPtr packet_output;
 
 
 protected:
@@ -246,21 +242,21 @@ protected:
 
     unsigned number_of_identifier;
 
-    bool setPacketValuePrefix(ByteStream* prefix);
+    bool setPacketValuePrefix(ByteStreamPtr prefix);
 
-    bool setPacketValueVerify(ByteStream* prefix, ByteStream* packetHeader, ByteStream* packetDataField);
+    bool setPacketValueVerify(ByteStreamPtr prefix, ByteStreamPtr packetHeader, ByteStreamPtr packetDataField);
 
-    bool setPacketValueDataFieldHeader(ByteStream* packetDataField);
+    bool setPacketValueDataFieldHeader(ByteStreamPtr packetDataField);
 
-    bool setPacketValueSourceDataField(ByteStream* packetDataField);
+    bool setPacketValueSourceDataField(ByteStreamPtr packetDataField);
 
-    bool setPacketValueHeader(ByteStream* header);
+    bool setPacketValueHeader(ByteStreamPtr header);
 
-    bool setPacketValueTail(ByteStream* packetDataField);
+    bool setPacketValueTail(ByteStreamPtr packetDataField);
 
-    void memByteStream(ByteStream* prefix, ByteStream* packetHeader, ByteStream* packetDataField);
+    void memByteStream(ByteStreamPtr prefix, ByteStreamPtr packetHeader, ByteStreamPtr packetDataField);
 
-    void memByteStream(ByteStream* prefix, ByteStream* packet);
+    void memByteStream(ByteStreamPtr prefix, ByteStreamPtr packet);
 
     bool bigendian;
 
@@ -274,15 +270,15 @@ protected:
 
 private:
 
-    ByteStream* tempHeader;
+    ByteStreamPtr tempHeader;
 
-    ByteStream* tempDataField;
+    ByteStreamPtr tempDataField;
 
-    ByteStream* tempDataFieldHeader;
+    ByteStreamPtr tempDataFieldHeader;
 
-    ByteStream* tempPacketDataField;
+    ByteStreamPtr tempPacketDataField;
 
-    ByteStream* tempTail;
+    ByteStreamPtr tempTail;
 
     bool first_output_stream_setted;
 
diff --git a/include/PacketDataField.h b/include/PacketDataField.h
index 4a92aadb7c5cdca6cbf6fbbceb4e9d5c1abbbf35..d562466fd27c4640a81fa91609c849c54f8b48df 100644
--- a/include/PacketDataField.h
+++ b/include/PacketDataField.h
@@ -53,10 +53,10 @@ public:
     dword getMaxDimension();
 
     /// Creates the outputstream ByteStream for the generation of the output stream
-    virtual bool setOutputStream(ByteStream* os, word first);
+    virtual bool setOutputStream(ByteStreamPtr os, word first);
 
 
-    virtual ByteStream* generateStream(bool bigendian);
+    virtual ByteStreamPtr generateStream(bool bigendian);
 
     /// Represents the data field header.
     DataFieldHeader *dataFieldHeader;
diff --git a/include/PacketNotRecognized.h b/include/PacketNotRecognized.h
index 7d48d9b4f2de87587e2b8352acb6d80a724182c1..814fe707c7e4620f637184d01fe997525d67b4e1 100644
--- a/include/PacketNotRecognized.h
+++ b/include/PacketNotRecognized.h
@@ -35,7 +35,7 @@ public:
 
     virtual bool createPacketType(char* fileName, bool prefix, word dimprefix) throw (PacketException*);
 
-    virtual bool setPacketValue(ByteStream* prefix, ByteStream* packetHeader, ByteStream* packetDataField);
+    virtual bool setPacketValue(ByteStreamPtr prefix, ByteStreamPtr packetHeader, ByteStreamPtr packetDataField);
 };
 
 }
diff --git a/include/PartOfPacket.h b/include/PartOfPacket.h
index 74a6d1e3369808c6e6da9e179b1f4e05ecc36a8f..0b25c7a88c226d4b2911c3af3e9520d3b46d67e6 100644
--- a/include/PartOfPacket.h
+++ b/include/PartOfPacket.h
@@ -243,27 +243,24 @@ public:
     };
 
     /// Sets the stream of byte. This method assigns the value of stream for each field of part of packet
-    virtual bool setByteStream(ByteStream* s);
+    virtual bool setByteStream(ByteStreamPtr s);
 
     /// Get the current ByteStream
-    virtual ByteStream* getByteStream()
+    virtual ByteStreamPtr getByteStream()
     {
     	return stream;
     }
 
-    /// Delete the current ByteStream
-    virtual void deleteByteStream();
-
-    virtual void memByteStream(ByteStream* stream);
+    virtual void memByteStream(ByteStreamPtr stream);
 
     /// Represent current stream writes to output.
-    ByteStream* outputstream;
+    ByteStreamPtr outputstream;
 
     /// Generates the stream.
-    virtual ByteStream* generateStream(bool bigendian);
+    virtual ByteStreamPtr generateStream(bool bigendian);
 
     /// Creates the outputstream ByteStream for the generation of the output stream
-    virtual bool setOutputStream(ByteStream* os, dword first);
+    virtual bool setOutputStream(ByteStreamPtr os, dword first);
 
     /// In a recoursive structure, the PartOfPacket that contains this PartOfPacket
     PartOfPacket* previous;
@@ -273,7 +270,7 @@ public:
 protected:
 
     /// Represent current stream reads from input.
-    ByteStream* stream;
+    ByteStreamPtr stream;
 
     /// List of field of part of packet.
     Field** fields;
diff --git a/include/SDFBVBlock.h b/include/SDFBVBlock.h
index a7963853255d5208b4583e7c73f6fb741ae58d7c..9ad1b7ccd3c2260702fe5ae4b21756576ac9c1e8 100644
--- a/include/SDFBVBlock.h
+++ b/include/SDFBVBlock.h
@@ -47,9 +47,9 @@ public:
      /// Total max dimension in bytes of block
     dword getMaxDimension();
 
-    bool setOutputStream(ByteStream* os, dword first);
+    bool setOutputStream(ByteStreamPtr os, dword first);
 
-    ByteStream* generateStream(bool bigendian);
+    ByteStreamPtr generateStream(bool bigendian);
 
     virtual word getNumberOfRealElement();
 
diff --git a/include/SDFBlockFixed.h b/include/SDFBlockFixed.h
index c3035bdc80b92c277775ab63796a84c03fce24ec..7fea2befa638326d086f925110ced70c394317d9 100644
--- a/include/SDFBlockFixed.h
+++ b/include/SDFBlockFixed.h
@@ -45,7 +45,7 @@ public:
 
     virtual word getNumberOfFields();
 
-    virtual bool setByteStream(ByteStream* s);
+    virtual bool setByteStream(ByteStreamPtr s);
 
     /// Prints the value of each field of this part of packet.
     virtual char** printValue(char* addString = "");
@@ -87,9 +87,9 @@ public:
     /// \param value The value must be set.
     virtual void setFieldValue(word block, word index, word value);
 
-    virtual bool setOutputStream(ByteStream* os, dword first);
+    virtual bool setOutputStream(ByteStreamPtr os, dword first);
 
-    virtual ByteStream* generateStream(bool bigendian);
+    virtual ByteStreamPtr generateStream(bool bigendian);
 
     /// Returns the number of fields for each block.
     virtual word getNumberOfFields(word block);
@@ -97,7 +97,7 @@ public:
 private:
     SDFBFBlock *block;
 
-    ByteStream* tempBlock;
+    ByteStreamPtr tempBlock;
 };
 
 }
diff --git a/include/SDFBlockVariable.h b/include/SDFBlockVariable.h
index fe02eac7afb1cd763d07bb37bd418cd269bb4374..11bbd54a00c1e9498ab69a813a9bb23437d568c4 100644
--- a/include/SDFBlockVariable.h
+++ b/include/SDFBlockVariable.h
@@ -48,7 +48,7 @@ public:
     virtual word getNumberOfFields();
 
 
-    virtual bool setByteStream(ByteStream* s);
+    virtual bool setByteStream(ByteStreamPtr s);
 
 
     virtual dword getDimension();
@@ -99,10 +99,10 @@ public:
     virtual void setFieldValue(word block, word index, word value);
 
 
-    virtual bool setOutputStream(ByteStream* os, dword first);
+    virtual bool setOutputStream(ByteStreamPtr os, dword first);
 
 
-    virtual ByteStream* generateStream(bool bigendian);
+    virtual ByteStreamPtr generateStream(bool bigendian);
 
 
     /// Get the number of elements for each block
@@ -124,7 +124,7 @@ private:
 
     SDFBVBlock* blocks;
 
-    ByteStream* tempBlock;
+    ByteStreamPtr tempBlock;
 };
 
 }
diff --git a/include/SDFRBBlock.h b/include/SDFRBBlock.h
index 525211efac29ff3ce4f0ef7c633b79f58fe60045..c1ed4e34d607c9cb778fb46afecfaa364f783e47 100644
--- a/include/SDFRBBlock.h
+++ b/include/SDFRBBlock.h
@@ -142,13 +142,13 @@ public:
         return ID;
     };
 
-    virtual bool setOutputStream(ByteStream* os, dword first);
+    virtual bool setOutputStream(ByteStreamPtr os, dword first);
 
-    virtual ByteStream* generateStream(bool bigendian);
+    virtual ByteStreamPtr generateStream(bool bigendian);
 
-    virtual bool setByteStream(ByteStream* s);
+    virtual bool setByteStream(ByteStreamPtr s);
 
-    virtual ByteStream* getByteStream()
+    virtual ByteStreamPtr getByteStream()
     {
         return stream;
     }
@@ -212,7 +212,7 @@ protected:
 
     bool reset_output_stream;
 
-    ByteStream* tempBlock1;
+    ByteStreamPtr tempBlock1;
 
     SDFRBBlockType* type;
 
diff --git a/include/SDFRBlock.h b/include/SDFRBlock.h
index ee13cdf11a7758853332c77ed4ee73da2fb155fe..177e5e59b98ad00320868bc3619080a80a1249b5 100644
--- a/include/SDFRBlock.h
+++ b/include/SDFRBlock.h
@@ -66,11 +66,11 @@ public:
 
     virtual dword getDimension();
 
-    virtual bool setByteStream(ByteStream* s);
+    virtual bool setByteStream(ByteStreamPtr s);
 
-    virtual bool setOutputStream(ByteStream* os, dword first);
+    virtual bool setOutputStream(ByteStreamPtr os, dword first);
 
-    virtual ByteStream* generateStream(bool bigendian);
+    virtual ByteStreamPtr generateStream(bool bigendian);
 
     
     /// Set the number of blocks (the number of times that a block of a
diff --git a/include/Socket.h b/include/Socket.h
index a3ec92a072b21074b551b9269f6e5b4874346ee5..7782e4f0c05a1b4b259ed06f2052b12432ac0fa5 100644
--- a/include/Socket.h
+++ b/include/Socket.h
@@ -51,9 +51,9 @@ public:
     }
 
     /// Data Transimission
-    virtual bool send ( ByteStream* b ) const throw(PacketExceptionIO*) ;
+    virtual bool send ( ByteStreamPtr b ) const throw(PacketExceptionIO*) ;
 
-    virtual ByteStream* recv (word dim, int & status) throw(PacketExceptionIO*) ;
+    virtual ByteStreamPtr recv (word dim, int & status) throw(PacketExceptionIO*) ;
 
     virtual bool connect ( const std::string host, const int port ) throw(PacketExceptionIO*);
 
diff --git a/src/ByteStream.cpp b/src/ByteStream.cpp
index 49b65c9002657e2aaf1ed4247419e66b2017e0bf..575dfe33e12e8bd3b84deca80474a4453b5ddae3 100644
--- a/src/ByteStream.cpp
+++ b/src/ByteStream.cpp
@@ -32,7 +32,7 @@ dword ByteStream::count_object_deleted2 = 0;
 /// Returns a pointer of a field in the list of fields of this part of packet.
 /// \remarks mem_allocation = true indicates that the allocated memory must be released by the destroyer.
 /// \remarks memory_sharing=false In all methods of constructor or set type accepting byte*, it indicates that the swap is applied. 
-/// In all methods of constructor or set type accepting ByteStream* the swap is never applied.
+/// In all methods of constructor or set type accepting ByteStreamPtr the swap is never applied.
 
 PacketLib::ByteStream::ByteStream(bool bigendian)
 {
@@ -76,7 +76,7 @@ PacketLib::ByteStream::ByteStream(byte* stream, dword dim, bool bigendian, bool
 }
 
 
-PacketLib::ByteStream::ByteStream(ByteStream* b0, ByteStream* b1, ByteStream* b2)
+PacketLib::ByteStream::ByteStream(ByteStreamPtr b0, ByteStreamPtr b1, ByteStreamPtr b2)
 {
     mem_allocation_constructor = true;
 
@@ -201,20 +201,20 @@ long PacketLib::ByteStream::getValue(dword start, word dim)
 
 
 
-ByteStream* PacketLib::ByteStream::getSubByteStream(dword first, dword last)
+ByteStreamPtr PacketLib::ByteStream::getSubByteStream(dword first, dword last)
 {
     DEMORET0;
     if(first > last)
         return NULL;
     if(last > byteInTheStream)
         return NULL;
-    ByteStream* b = new ByteStream((stream + first), last-first+1, bigendian, true);
+    ByteStreamPtr b = ByteStreamPtr(new ByteStream((stream + first), last-first+1, bigendian, true));
     return b;
 }
 
 
 
-ByteStream* PacketLib::ByteStream::getSubByteStreamCopy(dword first, dword last)
+ByteStreamPtr PacketLib::ByteStream::getSubByteStreamCopy(dword first, dword last)
 {
     DEMORET0;
     if(first > last)
@@ -224,8 +224,7 @@ ByteStream* PacketLib::ByteStream::getSubByteStreamCopy(dword first, dword last)
     byte* streamtemp = (byte*) new byte[last-first+1];
     for(dword i=0; i<last-first+1; i++)
         streamtemp[i] = stream[first+i];
-    ByteStream* b = new ByteStream(streamtemp, last-first+1, bigendian, false);
-    return b;
+    return ByteStreamPtr(new ByteStream(streamtemp, last-first+1, bigendian, false));
 }
 
 
@@ -292,7 +291,7 @@ bool PacketLib::ByteStream::setStream(byte* b, dword dim, bool bigendian, bool m
 
 
 
-bool PacketLib::ByteStream::setStream(ByteStream* b, dword first, dword last)
+bool PacketLib::ByteStream::setStream(ByteStreamPtr b, dword first, dword last)
 {
     if(first > last)
         return false;
diff --git a/src/File.cpp b/src/File.cpp
index 97459021b4ea965b225bbf42204aad72d8bbbac4..4c51abb0640a54e4d8420a2403bae930bb91f400 100644
--- a/src/File.cpp
+++ b/src/File.cpp
@@ -106,18 +106,18 @@ int File::getByte()
 }
 
 
-ByteStream* File::getNByte(dword N)
+ByteStreamPtr File::getNByte(dword N)
 {
     dword i = 0;
     int c1, c2;
     if(N == 0)
-        return new ByteStream(0, bigendian);
+        return ByteStreamPtr(new ByteStream(0, bigendian));
     //solo un numero pari di byte
     //if(N%2 != 0 || !fileOpened) return NULL;
 
     if(closed) return NULL;
 
-    //ByteStream* b = new ByteStream(N, bigendian);
+    //ByteStreamPtr b = new ByteStream(N, bigendian);
     byte* stream = (byte*) new byte[N];
 
     for(i = 0; i<N && (c1 = getByte()) != EOI && (c2 = getByte()) != EOI; i+=2)
@@ -141,7 +141,7 @@ ByteStream* File::getNByte(dword N)
     }
     /*if(i != N)
     {
-        ByteStream* b1 = new ByteStream(i, bigendian);
+        ByteStreamPtr b1 = new ByteStream(i, bigendian);
         for(int j = 0; j<i; j++)
             b1->stream[j] = b->stream[j];
         delete b;
@@ -150,7 +150,7 @@ ByteStream* File::getNByte(dword N)
     //for(; i<N; i++)
     //	b->stream[i] = 0;
     //return b;
-    return new ByteStream(stream, i, bigendian, false);
+    return ByteStreamPtr(new ByteStream(stream, i, bigendian, false));
 }
 
 
@@ -336,7 +336,7 @@ bool File::writeString(const char* str) throw(PacketExceptionIO*)
 
 
 
-bool File::writeByteStream(ByteStream* b) throw(PacketExceptionIO*)
+bool File::writeByteStream(ByteStreamPtr b) throw(PacketExceptionIO*)
 {
     byte* stream = b->getOutputStream();
     if(fwrite((void*)stream, b->getDimension(), 1, fp)<1)
diff --git a/src/FileStream.cpp b/src/FileStream.cpp
index 3ff1b550fe1db8bbb1629891f8e0b5b0a4da15c3..c0d539480e50bbc16bd592c781b3675422a1c998 100644
--- a/src/FileStream.cpp
+++ b/src/FileStream.cpp
@@ -27,21 +27,21 @@ FileStream::FileStream(bool prefix, bool bigen, word dimprefix, long startpositi
 }
 
 
-ByteStream* FileStream::readHeader(unsigned int dimHeader)
+ByteStreamPtr FileStream::readHeader(unsigned int dimHeader)
 {
     header = getNByte(dimHeader);
     return header;
 }
 
 
-ByteStream* FileStream::readDataField(unsigned int dimDataField)
+ByteStreamPtr FileStream::readDataField(unsigned int dimDataField)
 {
     dataField = getNByte(dimDataField);
     return dataField;
 }
 
 
-ByteStream* FileStream::readPrefix()
+ByteStreamPtr FileStream::readPrefix()
 {
     if(thereIsPrefix)
         prefix = getNByte(dimPrefix);
diff --git a/src/InputFile.cpp b/src/InputFile.cpp
index 69c93f2ed5b401f7e3725838762a59f77a422d0d..8176966f07e2b5abe376ed0aa55d55d7383a00aa 100644
--- a/src/InputFile.cpp
+++ b/src/InputFile.cpp
@@ -49,9 +49,9 @@ void InputFile::close()  throw(PacketExceptionIO*)
 }
 
 
-ByteStream* InputFile::readByteStream(dword n_byte) throw(PacketExceptionIO*)
+ByteStreamPtr InputFile::readByteStream(dword n_byte) throw(PacketExceptionIO*)
 {
-    ByteStream* bs;
+    ByteStreamPtr bs;
     if(!closed)
         bs = file->getNByte(n_byte);
     else
diff --git a/src/InputPacketStream.cpp b/src/InputPacketStream.cpp
index a58055a41ecc3a1fa4f069a72fa5882f6a58cb7d..3edb07d785a37a5cfb699584f7dedf32746e2202 100644
--- a/src/InputPacketStream.cpp
+++ b/src/InputPacketStream.cpp
@@ -40,7 +40,7 @@ InputPacketStream::~InputPacketStream()
 }
 
 
-int InputPacketStream::detPacketType(ByteStream* prefix, ByteStream* packetHeader, ByteStream* packetDataField)
+int InputPacketStream::detPacketType(ByteStreamPtr prefix, ByteStreamPtr packetHeader, ByteStreamPtr packetDataField)
 {
     ///  Iterate through list and output each element.
     ///  The packetType 0 is the packet not recognized
@@ -55,7 +55,7 @@ int InputPacketStream::detPacketType(ByteStream* prefix, ByteStream* packetHeade
 
 
 
-int InputPacketStream::detPacketType(ByteStream* prefix, ByteStream* packet)
+int InputPacketStream::detPacketType(ByteStreamPtr prefix, ByteStreamPtr packet)
 {
     /// Iterate through list and output each element.
     /// The packetType 0 is the packet not recognized
@@ -93,7 +93,7 @@ Packet* InputPacketStream::readPacket() throw(PacketExceptionIO*)
 {
     unsigned dimHeader = getHeaderDimension();
     unsigned dimPrefix = getPrefixDimension();
-    ByteStream* b1 = 0, *b2 = 0, *b0 = 0;
+    ByteStreamPtr b0, b1, b2;
     dword pl, dim, pindex;
     try
     {
@@ -162,16 +162,15 @@ dword InputPacketStream::getPacketDimension(byte* stream) {
 		dword dimPre = 0;
 		if(prefix)
 			dimPre += dimPrefix;
-		//ByteStream* prefix = new ByteStream(stream, dimPre, bigendian);
+		//ByteStreamPtr prefix = new ByteStream(stream, dimPre, bigendian);
 
 		dword dim = 0;
 		dword dimHeader = headerReference->getDimension();
 		dim += dimHeader;
-		ByteStream* tempHeader = new ByteStream();
+		ByteStreamPtr tempHeader = ByteStreamPtr(new ByteStream());
 		tempHeader->setStream(stream+dimPre, dimHeader, bigendian);
 		headerReference->setByteStream(tempHeader);
 		dim += headerReference->getPacketLength();
-		delete tempHeader;
 		return dim;
 }
 
diff --git a/src/InputPacketStreamFile.cpp b/src/InputPacketStreamFile.cpp
index 0abdb34e087a7e4533e91cf91753ab9b31be27ea..da260b06247f63e3f08ddef70dff76b15bd1fbe6 100644
--- a/src/InputPacketStreamFile.cpp
+++ b/src/InputPacketStreamFile.cpp
@@ -90,7 +90,7 @@ bool InputPacketStreamFile::openInputStream() throw(PacketExceptionIO*)
 
 bool InputPacketStreamFile::freeRun() throw(PacketExceptionIO*)
 {
-    ByteStream* b1 = 0, *b2 = 0, *b0 = 0;
+    ByteStreamPtr b0, b1, b2;
     word pl, dim;
     long pointer;
 
@@ -110,10 +110,8 @@ bool InputPacketStreamFile::freeRun() throw(PacketExceptionIO*)
         pointer = inputStream->getpos();
         b0 = inputStream->readPrefix();
         if(inputStream->isEOF())
-        {
-            delete b0;
             return true;
-        }
+
         b1 = inputStream->readHeader(dimHeader);
         if(b1->getDimension() != dimHeader)
         {
@@ -121,8 +119,6 @@ bool InputPacketStreamFile::freeRun() throw(PacketExceptionIO*)
                 pindex = 0;
             else
             {
-                delete b0;
-                delete b1;
                 numberOfFileStreamPointer = count;
                 return false;
             }
@@ -139,9 +135,6 @@ bool InputPacketStreamFile::freeRun() throw(PacketExceptionIO*)
                     pindex = 0;
                 else
                 {
-                    delete b0;
-                    delete b1;
-                    delete b2;
                     numberOfFileStreamPointer = count;
                     return false;
                 }
@@ -149,9 +142,6 @@ bool InputPacketStreamFile::freeRun() throw(PacketExceptionIO*)
             else
                 pindex = detPacketType(b0, b1, b2);
         }
-        delete b0;
-        delete b1;
-        delete b2;
         FileStreamPointer* fsp = new FileStreamPointer;
         fsp->typeOfPacket = pindex;
         fsp->pointerStart = pointer;
@@ -187,7 +177,7 @@ Packet* InputPacketStreamFile::getPacketFromFileStreamPointer(int index, bool ne
     //int i;
     long pos;
     int type;
-    ByteStream* b1 = 0, *b2 = 0, *b0 = 0;
+    ByteStreamPtr b0, b1, b2;
     word pl;
 
     if(index > numberOfFileStreamPointer)
@@ -275,7 +265,7 @@ long InputPacketStreamFile::getNumberOfFileStreamPointer()
 
 Packet* InputPacketStreamFile::getPacketFromStream() throw (PacketExceptionIO * )
 {
-    ByteStream* b1 = 0, *b2 = 0, *b0 = 0;
+    ByteStreamPtr b0, b1, b2;
     word pl, dim;
     unsigned dimHeader = headerReference->getDimension();
 
@@ -283,29 +273,17 @@ Packet* InputPacketStreamFile::getPacketFromStream() throw (PacketExceptionIO *
         return 0;
     b0 = inputStream->readPrefix();
     if(inputStream->isEOF())
-    {
-        delete b0;
         return 0;
-    }
     b1 = inputStream->readHeader(dimHeader);
     if(b1->getDimension() != dimHeader)
-    {
-        delete b0;
-        delete b1;
         return 0;
-    }
 
     headerReference->setByteStream(b1);
     pl = headerReference->getPacketLength();
     b2 = inputStream->readDataField(pl);
     dim = b2->getDimension();
     if(dim != pl)
-    {
-        delete b0;
-        delete b1;
-        delete b2;
         return 0;
-    }
 
     Packet* p;
     for (int i = 1; i<numberOfPacketType; i++)
@@ -316,9 +294,7 @@ Packet* InputPacketStreamFile::getPacketFromStream() throw (PacketExceptionIO *
             return p;
         }
     }
-    p->deleteExternalByteStream();
     return 0;
-
 }
 
 
diff --git a/src/InputSerial.cpp b/src/InputSerial.cpp
index 22345590e21bffb819dbbd68511a34b202f7ed7f..90f5d7536ce0a5dce4fd9df9dc781ca7a03275c5 100644
--- a/src/InputSerial.cpp
+++ b/src/InputSerial.cpp
@@ -56,10 +56,10 @@ void InputSerial::close()  throw(PacketExceptionIO*)
 
 
 
-ByteStream* InputSerial::readByteStream(int n_byte) throw(PacketExceptionIO*)
+ByteStreamPtr InputSerial::readByteStream(int n_byte) throw(PacketExceptionIO*)
 {
     //cout << "waiting " << n_byte << endl;
-    ByteStream* bs;
+    ByteStreamPtr bs;
     byte* buff = new byte[n_byte];
     int current = 0;
     if(!closed)
@@ -71,12 +71,12 @@ ByteStream* InputSerial::readByteStream(int n_byte) throw(PacketExceptionIO*)
             current += readed;
             if( current  == 0 )
             {
-                bs = new ByteStream(0, bigendian);
+                bs = ByteStreamPtr(new ByteStream(0, bigendian));
                 break;
             }
         }
         if(current != 0)
-            bs = new ByteStream( buff, n_byte, bigendian );
+            bs = ByteStreamPtr(new ByteStream(buff, n_byte, bigendian));
     }
     else
         return 0;
diff --git a/src/InputSocketServer.cpp b/src/InputSocketServer.cpp
index 00f5feb1f0b56212645a6953a8fafe599ddf01c5..7cb5853a6f64da8d6074298d9bf532778bd39549 100644
--- a/src/InputSocketServer.cpp
+++ b/src/InputSocketServer.cpp
@@ -57,7 +57,7 @@ void InputSocketServer::close() throw(PacketExceptionIO*)
 
 
 
-ByteStream* InputSocketServer::readByteStream(dword n_byte) throw(PacketExceptionIO*)
+ByteStreamPtr InputSocketServer::readByteStream(dword n_byte) throw(PacketExceptionIO*)
 {
     int status = 0;
     if(!accepted)
@@ -68,7 +68,7 @@ ByteStream* InputSocketServer::readByteStream(dword n_byte) throw(PacketExceptio
         eof = false;
     }
 
-    ByteStream* bl = new_sock->recv(n_byte, status);
+    ByteStreamPtr bl = new_sock->recv(n_byte, status);
     if(status == 0)
     {
         accepted = false;
diff --git a/src/OutputFile.cpp b/src/OutputFile.cpp
index 3b3f9e2499e8851784929808b20d888402381521..f6a93dbc5109b6088ea099885e09685a6899d354 100644
--- a/src/OutputFile.cpp
+++ b/src/OutputFile.cpp
@@ -52,7 +52,7 @@ bool OutputFile::open(char** parameters) throw(PacketExceptionIO*)
 
 
 
-bool OutputFile::writeByteStream(ByteStream* b) throw(PacketExceptionIO*)
+bool OutputFile::writeByteStream(ByteStreamPtr b) throw(PacketExceptionIO*)
 {
     if(!isclosed)
         file->writeByteStream(b);
diff --git a/src/OutputPacketStream.cpp b/src/OutputPacketStream.cpp
index 9ab1236b471c1c1bac92363b50168c638fc0bd82..4297eeb7a33386e48c22f442ec6a8e257008ca94 100644
--- a/src/OutputPacketStream.cpp
+++ b/src/OutputPacketStream.cpp
@@ -35,14 +35,13 @@ OutputPacketStream::OutputPacketStream(const char* fileNameConfig) : PacketStrea
 
 bool OutputPacketStream::writePacket(Packet* p) throw(PacketExceptionIO*)
 {
-    ByteStream* bs;
+    ByteStreamPtr bs;
     try
     {
         bs = p->getOutputStream();
         if(out == 0)
             throw new PacketExceptionIO("No output set..");
         out->writeByteStream(bs);
-        delete bs;
         return true;
     }
     catch(PacketExceptionIO* e)
diff --git a/src/OutputSerial.cpp b/src/OutputSerial.cpp
index 73d5699dac22d7ac5709728436cd8648d08e7ed2..24039360c41eef08b3329d7b90695abc93b805f6 100644
--- a/src/OutputSerial.cpp
+++ b/src/OutputSerial.cpp
@@ -53,7 +53,7 @@ bool OutputSerial::open(char** parameters) throw(PacketExceptionIO*)
 
 
 
-bool OutputSerial::writeByteStream(ByteStream* b) throw(PacketExceptionIO*)
+bool OutputSerial::writeByteStream(ByteStreamPtr b) throw(PacketExceptionIO*)
 {
     byte* buff = b->getStream();
     unsigned n_byte = b->getDimension();
diff --git a/src/OutputSocketClient.cpp b/src/OutputSocketClient.cpp
index e06b4f22e31166f0a04858cf175ad112e5dc9fb5..1a0f707bad3caa2017cf7f8ec2219d4729828ded 100644
--- a/src/OutputSocketClient.cpp
+++ b/src/OutputSocketClient.cpp
@@ -52,7 +52,7 @@ bool OutputSocketClient::open(char** argv) throw(PacketExceptionIO*)
 
 
 
-bool OutputSocketClient::writeByteStream(ByteStream* b) throw(PacketExceptionIO*)
+bool OutputSocketClient::writeByteStream(ByteStreamPtr b) throw(PacketExceptionIO*)
 {
     if(!isclosed)
         socketclient->send(b);
diff --git a/src/Packet.cpp b/src/Packet.cpp
index 2b1379571980c82b20413de2aebb0ce6f38a6858..53c9307237ec948b0f9253242e65f4f4ccb7c748 100644
--- a/src/Packet.cpp
+++ b/src/Packet.cpp
@@ -27,20 +27,16 @@ Packet::Packet(bool bigendian)
     header = (PacketHeader*) new PacketHeader();
     dataField = (PacketDataField*) new PacketDataField();
     name = 0;
-    prefix = 0;
 //    tail = new PartOfPacket();
     number_of_identifier = 0;
     identifiers = 0;
-    prefix = 0;
-    packet = 0;
-    packet_output = 0;
     this->bigendian = bigendian;
     /// temp
-    tempHeader = new ByteStream();
-    tempDataField = new ByteStream();
-    tempDataFieldHeader = new ByteStream();
-    tempPacketDataField = new ByteStream();
-    tempTail = new ByteStream();
+    tempHeader = ByteStreamPtr(new ByteStream);
+    tempDataField = ByteStreamPtr(new ByteStream);
+    tempDataFieldHeader = ByteStreamPtr(new ByteStream);
+    tempPacketDataField = ByteStreamPtr(new ByteStream);
+    tempTail = ByteStreamPtr(new ByteStream);
     first_output_stream_setted = false;
 
 }
@@ -57,19 +53,6 @@ Packet::~Packet()
     for(unsigned i = 0; i < number_of_identifier; i++)
         delete identifiers[i];
     delete[] identifiers;
-    delete packet_output;
-    packet_output = 0;
-    delete tempHeader;
-    tempHeader = 0;
-    delete tempDataField;
-    tempDataField = 0;
-    //ANDREA: ricordato di indagare perche' non si riesce ad effettuare il delete
-    delete tempDataFieldHeader;
-    tempDataFieldHeader = 0;
-    delete tempPacketDataField;
-    tempPacketDataField = 0;
-    delete tempTail;
-    tempTail = 0;
 }
 
 
@@ -184,7 +167,7 @@ bool Packet::createPacketType(char* fileName, bool isprefix, word dimprefix) thr
                                             /// Memory allocation for the output stream
                                             dword dimpo = getMaxDimension();
                                             dword dimpr = (isprefix?dimprefix:0);
-                                            packet_output = new ByteStream(dimpo + dimpr, bigendian);
+                                            packet_output = ByteStreamPtr(new ByteStream(dimpo + dimpr, bigendian));
 
                                             file.close();
                                             return true;
@@ -310,11 +293,11 @@ void Packet::printIdentifiers()
 }
 
 
-bool Packet::setPacketValue(ByteStream* prefix, ByteStream* packetHeader, ByteStream* packetDataField)
+bool Packet::setPacketValue(ByteStreamPtr prefix, ByteStreamPtr packetHeader, ByteStreamPtr packetDataField)
 {
     //cout << "@ " << packetDataField->getDimension() << endl;
     memByteStream(prefix, packetHeader, packetDataField);
-    ByteStream* packet = new ByteStream(packetHeader, packetDataField, 0);
+    ByteStreamPtr packet = ByteStreamPtr(new ByteStream(packetHeader, packetDataField, 0));
     memByteStream(prefix, packet);
     /// 1)
     if(!setPacketValueVerify(prefix, packetHeader, packetDataField))
@@ -358,7 +341,7 @@ bool Packet::setPacketValue(ByteStream* prefix, ByteStream* packetHeader, ByteSt
 
 
 
-bool Packet::setPacketValue(ByteStream* prefix, ByteStream* packet)
+bool Packet::setPacketValue(ByteStreamPtr prefix, ByteStreamPtr packet)
 {
     dword dimHeader = header->getDimension();
     memByteStream(prefix, packet);
@@ -369,7 +352,7 @@ bool Packet::setPacketValue(ByteStream* prefix, ByteStream* packet)
 
 
 
-bool Packet::verifyPacketValue(ByteStream* prefix, ByteStream* packet)
+bool Packet::verifyPacketValue(ByteStreamPtr prefix, ByteStreamPtr packet)
 {
     dword dimHeader = header->getDimension();
     memByteStream(prefix, packet);
@@ -380,7 +363,7 @@ bool Packet::verifyPacketValue(ByteStream* prefix, ByteStream* packet)
 
 
 
-bool Packet::verifyPacketValue(ByteStream* prefix, ByteStream* packetHeader, ByteStream* packetDataField)
+bool Packet::verifyPacketValue(ByteStreamPtr prefix, ByteStreamPtr packetHeader, ByteStreamPtr packetDataField)
 {
     bool verified = true;
     memByteStream(prefix, packetHeader, packetDataField);
@@ -441,7 +424,7 @@ bool Packet::verifyPacketValue(ByteStream* prefix, ByteStream* packetHeader, Byt
 }
 
 
-bool Packet::setAndVerifyPacketValue(ByteStream* prefix, ByteStream* packet)
+bool Packet::setAndVerifyPacketValue(ByteStreamPtr prefix, ByteStreamPtr packet)
 {
     dword dimHeader = header->getDimension();
     memByteStream(prefix, packet);
@@ -451,10 +434,10 @@ bool Packet::setAndVerifyPacketValue(ByteStream* prefix, ByteStream* packet)
 }
 
 
-bool Packet::setAndVerifyPacketValue(ByteStream* prefix, ByteStream* packetHeader, ByteStream* packetDataField)
+bool Packet::setAndVerifyPacketValue(ByteStreamPtr prefix, ByteStreamPtr packetHeader, ByteStreamPtr packetDataField)
 {
     memByteStream(prefix, packetHeader, packetDataField);
-    ByteStream* packet = new ByteStream(packetHeader, packetDataField, 0);
+    ByteStreamPtr packet = ByteStreamPtr(new ByteStream(packetHeader, packetDataField, 0));
     memByteStream(prefix, packet);
     if(verifyPacketValue(prefix, packetHeader, packetDataField)==false)
     {
@@ -572,7 +555,7 @@ void Packet::generateStream()
 
 
 
-bool Packet::setPacketValueVerify(ByteStream* prefix, ByteStream* packetHeader, ByteStream* packetDataField)
+bool Packet::setPacketValueVerify(ByteStreamPtr prefix, ByteStreamPtr packetHeader, ByteStreamPtr packetDataField)
 {
     /// 1) Checking
     /// First check: pointers control
@@ -586,7 +569,7 @@ bool Packet::setPacketValueVerify(ByteStream* prefix, ByteStream* packetHeader,
 
 
 
-bool Packet::setPacketValuePrefix(ByteStream* prefix)
+bool Packet::setPacketValuePrefix(ByteStreamPtr prefix)
 {
     //2) prefix
     this->prefix = prefix;
@@ -595,7 +578,7 @@ bool Packet::setPacketValuePrefix(ByteStream* prefix)
 
 
 
-bool Packet::setPacketValueDataFieldHeader(ByteStream* packetDataField)
+bool Packet::setPacketValueDataFieldHeader(ByteStreamPtr packetDataField)
 {
     bool b;
     dword packetLength;
@@ -613,7 +596,7 @@ bool Packet::setPacketValueDataFieldHeader(ByteStream* packetDataField)
 }
 
 
-bool Packet::setPacketValueSourceDataField(ByteStream* packetDataField)
+bool Packet::setPacketValueSourceDataField(ByteStreamPtr packetDataField)
 {
     bool b;
     dword packetLength;
@@ -652,7 +635,7 @@ bool Packet::setPacketValueSourceDataField(ByteStream* packetDataField)
 
 }
 
-bool Packet::setPacketValueTail(ByteStream* packetDataField)
+bool Packet::setPacketValueTail(ByteStreamPtr packetDataField)
 {
     bool b;
     dword s, e;
@@ -678,7 +661,7 @@ bool Packet::setPacketValueTail(ByteStream* packetDataField)
 
 
 
-bool Packet::setPacketValueHeader(ByteStream* packetHeader)
+bool Packet::setPacketValueHeader(ByteStreamPtr packetHeader)
 {
     /// 3) Header
 	/// Reading and setting the packet header
@@ -688,38 +671,7 @@ bool Packet::setPacketValueHeader(ByteStream* packetHeader)
         return true;
 }
 
-
-
-void Packet::deleteExternalByteStream()
-{
-    if(prefix != 0)
-        if(prefix->getMemAllocation())
-        {
-            delete prefix;
-            prefix = 0;
-        }
-    if(packet !=0)
-        if(packet->getMemAllocation())
-        {
-            delete packet;
-            packet = 0;
-        }
-    if(dataField->getByteStream() != 0)
-        if(dataField->getByteStream()->getMemAllocation())
-        {
-            dataField->deleteByteStream();
-
-        }
-    if(header->getByteStream() != 0)
-        if(header->getByteStream()->getMemAllocation())
-        {
-            header->deleteByteStream();
-        }
-}
-
-
-
-void Packet::memByteStream(ByteStream* prefix, ByteStream* packet)
+void Packet::memByteStream(ByteStreamPtr prefix, ByteStreamPtr packet)
 {
     this->prefix = prefix;
     this->packet = packet;
@@ -727,7 +679,7 @@ void Packet::memByteStream(ByteStream* prefix, ByteStream* packet)
 
 
 
-void Packet::memByteStream(ByteStream* prefix, ByteStream* packetHeader, ByteStream* packetDataField)
+void Packet::memByteStream(ByteStreamPtr prefix, ByteStreamPtr packetHeader, ByteStreamPtr packetDataField)
 {
     this->prefix = prefix;
     //this->packet = 0;
@@ -737,17 +689,17 @@ void Packet::memByteStream(ByteStream* prefix, ByteStream* packetHeader, ByteStr
 
 
 
-ByteStream* Packet::getOutputStream()
+ByteStreamPtr Packet::getOutputStream()
 {
     generateStream();
-    ByteStream* b = new ByteStream(packet_output->stream, getDimension() + (thereisprefix?dimPrefix:0), bigendian);
+    ByteStreamPtr b = ByteStreamPtr(new ByteStream(packet_output->stream, getDimension() + (thereisprefix?dimPrefix:0), bigendian));
     return b;
 }
 
-ByteStream* Packet::getInputStream()
+ByteStreamPtr Packet::getInputStream()
 {
     if(packet == 0)
-        packet = new ByteStream(header->getByteStream(), dataField->getByteStream(), 0);
+        packet = ByteStreamPtr(new ByteStream(header->getByteStream(), dataField->getByteStream(), 0));
     return packet;
 }
 
@@ -841,7 +793,7 @@ bool Packet::verifyPacketValue(byte* stream) {
 	dword dimPre = 0;
 	if(thereisprefix)
 	        dimPre += dimPrefix;
-	ByteStream* prefix = new ByteStream(stream, dimPre, bigendian);
+	ByteStreamPtr prefix = ByteStreamPtr(new ByteStream(stream, dimPre, bigendian));
 
 	dword dim = 0;
 	dword dimHeader = header->getDimension();
@@ -849,7 +801,7 @@ bool Packet::verifyPacketValue(byte* stream) {
 	tempHeader->setStream(stream+dimPre, dimHeader, bigendian);
 	header->setByteStream(tempHeader);
 	dim += header->getDimensionOfPacketLength() + 1;
-	ByteStream* packet = new ByteStream(stream+dimPre, dim, bigendian);
+	ByteStreamPtr packet = ByteStreamPtr(new ByteStream(stream+dimPre, dim, bigendian));
 
 	return verifyPacketValue(prefix, packet);
 }
@@ -858,7 +810,7 @@ bool Packet::setPacketValue(byte* stream) {
 	dword dimPre = 0;
 	if(thereisprefix)
 			dimPre += dimPrefix;
-	ByteStream* prefix = new ByteStream(stream, dimPre, bigendian);
+	ByteStreamPtr prefix = ByteStreamPtr(new ByteStream(stream, dimPre, bigendian));
 
 	dword dim = 0;
 	dword dimHeader = header->getDimension();
@@ -866,7 +818,7 @@ bool Packet::setPacketValue(byte* stream) {
 	tempHeader->setStream(stream+dimPre, dimHeader, bigendian);
 	header->setByteStream(tempHeader);
 	dim += header->getPacketLength() + 1;
-	ByteStream* packet = new ByteStream(stream+dimPre, dim, bigendian);
+	ByteStreamPtr packet = ByteStreamPtr(new ByteStream(stream+dimPre, dim, bigendian));
 
 	return setPacketValue(prefix, packet);
 
diff --git a/src/PacketDataField.cpp b/src/PacketDataField.cpp
index 48ce7856279f8be619f6739e10c3c324be1a6823..c0f274ec37c1fbe3a6ccf9689dcd864deeedb698 100644
--- a/src/PacketDataField.cpp
+++ b/src/PacketDataField.cpp
@@ -98,10 +98,9 @@ void PacketDataField::setNumberOfRealDataBlock(word number)
 
 
 
-bool PacketDataField::setOutputStream(ByteStream* os, word first)
+bool PacketDataField::setOutputStream(ByteStreamPtr os, word first)
 {
-    delete outputstream;
-    outputstream = new ByteStream((os->stream + first), getDimension(), os->isBigendian());
+    outputstream = ByteStreamPtr(new ByteStream((os->stream + first), getDimension(), os->isBigendian()));
     dataFieldHeader->setOutputStream(os, first);
     word sdfstart = first + dataFieldHeader->getDimension();
     sourceDataField->setOutputStream(os, sdfstart);
@@ -115,7 +114,7 @@ bool PacketDataField::setOutputStream(ByteStream* os, word first)
 
 
 
-ByteStream* PacketDataField::generateStream(bool bigendian)
+ByteStreamPtr PacketDataField::generateStream(bool bigendian)
 {
     dataFieldHeader->generateStream(bigendian);
     sourceDataField->generateStream(bigendian);
diff --git a/src/PacketNotRecognized.cpp b/src/PacketNotRecognized.cpp
index f2b01ebae3de93a7ab95be34aba0ee9a96cc56d9..c7eac34b90b0915d22dc372c7c04646295807f8f 100644
--- a/src/PacketNotRecognized.cpp
+++ b/src/PacketNotRecognized.cpp
@@ -72,7 +72,7 @@ PacketNotRecognized::~PacketNotRecognized()
 
 
 
-bool PacketNotRecognized::setPacketValue(ByteStream* prefix, ByteStream* packetHeader, ByteStream* packetDataField)
+bool PacketNotRecognized::setPacketValue(ByteStreamPtr prefix, ByteStreamPtr packetHeader, ByteStreamPtr packetDataField)
 {
     /// It reads and sets the packet header 
     if(!header->setByteStream(packetHeader))
diff --git a/src/PartOfPacket.cpp b/src/PartOfPacket.cpp
index e2db0684be4985ca57e3b2b96d07db99243c9cfa..4194a4e2035cee16cb1c9a686aa632145a4f7faa 100644
--- a/src/PartOfPacket.cpp
+++ b/src/PartOfPacket.cpp
@@ -25,7 +25,7 @@ word PacketLib::pattern[] = {0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16
 PartOfPacket::PartOfPacket(const char* popName)
 {
     fieldsDimension = 0;
-    stream = new ByteStream();
+    stream = ByteStreamPtr(new ByteStream());
     numberOfFields = 0;
     fields = 0;
     outputstream = 0;
@@ -201,7 +201,7 @@ MemoryBuffer* PartOfPacket::loadFieldsInBuffer(InputText & fp)
 }
 
 
-bool PartOfPacket::setByteStream(ByteStream* s)
+bool PartOfPacket::setByteStream(ByteStreamPtr s)
 {
     Field* ftemp;
 
@@ -384,7 +384,7 @@ void PartOfPacket::deleteFields()
 }
 
 
-ByteStream* PartOfPacket::generateStream(bool bigendian)
+ByteStreamPtr PartOfPacket::generateStream(bool bigendian)
 {
     word w = 0, wtemp = 0;
     int posbit = 0;
@@ -393,7 +393,7 @@ ByteStream* PartOfPacket::generateStream(bool bigendian)
     /// Dimension of the current field
     byte dimbit = 0;
     if(outputstream == 0)
-        outputstream = new ByteStream(getDimension(), bigendian);
+        outputstream = ByteStreamPtr(new ByteStream(getDimension(), bigendian));
     for(unsigned i = 0; i<numberOfFields; i++)
     {
         if(!fields[i]->thereIsPredefinedValue())
@@ -446,10 +446,9 @@ ByteStream* PartOfPacket::generateStream(bool bigendian)
 };
 
 
-bool PartOfPacket::setOutputStream(ByteStream* os, dword first)
+bool PartOfPacket::setOutputStream(ByteStreamPtr os, dword first)
 {
-    delete outputstream;
-    outputstream = new ByteStream((os->stream + first), getDimension(), os->isBigendian());
+    outputstream = ByteStreamPtr(new ByteStream((os->stream + first), getDimension(), os->isBigendian()));
     return true;
 }
 
@@ -582,11 +581,6 @@ void PartOfPacket::setFieldValue_4_13(word index, signed long value) throw(Packe
     setFieldValue_3_14(index, value2);
 }
 
-void PacketLib::PartOfPacket::deleteByteStream() {
-	delete stream;
-	stream = 0;
-}
-
-void PacketLib::PartOfPacket::memByteStream(ByteStream* stream) {
+void PacketLib::PartOfPacket::memByteStream(ByteStreamPtr stream) {
 	this->stream = stream;
 }
diff --git a/src/SDFBVBlock.cpp b/src/SDFBVBlock.cpp
index 1a2389ff2b463bbcc12fe49d32f6f3e7a76f40e9..dd73be3c16040d0eb295b8104d4f25a6bc495292 100644
--- a/src/SDFBVBlock.cpp
+++ b/src/SDFBVBlock.cpp
@@ -83,7 +83,7 @@ dword SDFBVBlock::getMaxDimension()
 
 
 
-bool SDFBVBlock::setOutputStream(ByteStream* os, dword first)
+bool SDFBVBlock::setOutputStream(ByteStreamPtr os, dword first)
 {
     fixed.setOutputStream(os, first);
     dword nrb = fixed.getNumberOfRealElement();
@@ -98,7 +98,7 @@ bool SDFBVBlock::setOutputStream(ByteStream* os, dword first)
 
 
 
-ByteStream*  SDFBVBlock::generateStream(bool bigendian)
+ByteStreamPtr  SDFBVBlock::generateStream(bool bigendian)
 {
     fixed.generateStream(bigendian);
     word nrb = fixed.getNumberOfRealElement();
diff --git a/src/SDFBlockFixed.cpp b/src/SDFBlockFixed.cpp
index c7735d4bc4ddfa4c6b24ba5c3270a2fb435173b6..02c0462c849a4ae66b436e665a5a524c48d6bce3 100644
--- a/src/SDFBlockFixed.cpp
+++ b/src/SDFBlockFixed.cpp
@@ -88,19 +88,15 @@ SDFBlockFixed::SDFBlockFixed() : SourceDataField("SDF Block Fixed")
     rblock = false;
     block = NULL;
     subFromNBlock[0] = 0;
-    tempBlock = new ByteStream();
+    tempBlock = ByteStreamPtr(new ByteStream());
 }
 
 
 
 SDFBlockFixed::~SDFBlockFixed()
 {
-    delete tempBlock;
-    tempBlock = 0;
     //for(int i=0; i< maxNumberOfBlock; i++)
     //	delete &block[i];
-    delete[] block;
-    block = 0;
 }
 
 
@@ -177,7 +173,7 @@ word SDFBlockFixed::getNumberOfFields()
 
 
 
-bool SDFBlockFixed::setByteStream(ByteStream* s)
+bool SDFBlockFixed::setByteStream(ByteStreamPtr s)
 {
     bool b;
     word bytestart=0, bytestop=0;
@@ -296,12 +292,11 @@ string* SDFBlockFixed::printStructure()
 }
 
 
-bool SDFBlockFixed::setOutputStream(ByteStream* os, dword first)
+bool SDFBlockFixed::setOutputStream(ByteStreamPtr os, dword first)
 {
     dword mnb = getNumberOfRealDataBlock();
     dword start = first;
-    delete outputstream;
-    outputstream = new ByteStream((os->stream + first), getDimension(), os->isBigendian());
+    outputstream = ByteStreamPtr(new ByteStream((os->stream + first), getDimension(), os->isBigendian()));
     for(dword i = 0; i<mnb; i++)
     {
         block[i].setOutputStream(os, start);
@@ -311,7 +306,7 @@ bool SDFBlockFixed::setOutputStream(ByteStream* os, dword first)
 }
 
 
-ByteStream* SDFBlockFixed::generateStream(bool bigendian)
+ByteStreamPtr SDFBlockFixed::generateStream(bool bigendian)
 {
     word mnb = getNumberOfRealDataBlock();
     for(word i = 0; i<mnb; i++)
diff --git a/src/SDFBlockVariable.cpp b/src/SDFBlockVariable.cpp
index e89c979c492c0b32e92c419747acbe8ed528ab02..b166fc242e3a05b7e601b33aa131c10de033a0db 100644
--- a/src/SDFBlockVariable.cpp
+++ b/src/SDFBlockVariable.cpp
@@ -161,7 +161,7 @@ SDFBlockVariable::SDFBlockVariable() : SourceDataField("SDF Block Variable")
     fixed = false;
     rblock = false;
     blocks = 0;
-    tempBlock = new ByteStream();
+    tempBlock = ByteStreamPtr(new ByteStream());
 }
 
 
@@ -189,7 +189,7 @@ word SDFBlockVariable::getNumberOfFields()
 
 
 
-bool SDFBlockVariable::setByteStream(ByteStream* s)
+bool SDFBlockVariable::setByteStream(ByteStreamPtr s)
 {
     word bytestart=0, bytestop=0;
     word number_of_real_element;
@@ -396,12 +396,11 @@ void SDFBlockVariable::setFieldValue(word block, word index, word value)
 
 
 
-bool SDFBlockVariable::setOutputStream(ByteStream* os, dword first)
+bool SDFBlockVariable::setOutputStream(ByteStreamPtr os, dword first)
 {
     dword mnb = getNumberOfRealDataBlock();
     dword start = first;
-    delete outputstream;
-    outputstream = new ByteStream((os->stream + first), getDimension(), os->isBigendian());
+    outputstream = ByteStreamPtr(new ByteStream((os->stream + first), getDimension(), os->isBigendian()));
     for(dword i = 0; i<mnb; i++)
     {
         blocks[i].setOutputStream(os, start);
@@ -412,7 +411,7 @@ bool SDFBlockVariable::setOutputStream(ByteStream* os, dword first)
 
 
 
-ByteStream* SDFBlockVariable::generateStream(bool bigendian)
+ByteStreamPtr SDFBlockVariable::generateStream(bool bigendian)
 {
     word mnb = getNumberOfRealDataBlock();
     for(word i = 0; i<mnb; i++)
diff --git a/src/SDFRBBlock.cpp b/src/SDFRBBlock.cpp
index 6e44308c0a350313a631d2c1cab08116a8a56e90..a9520802eb409cc0e2e2e483eb3155992d8392e0 100644
--- a/src/SDFRBBlock.cpp
+++ b/src/SDFRBBlock.cpp
@@ -211,7 +211,7 @@ bool SDFRBBlockType::loadType(InputText& fp) throw(PacketException*)
 
 SDFRBBlock::SDFRBBlock() : block(0)
 {
-    tempBlock1 = new ByteStream();
+    tempBlock1 = ByteStreamPtr(new ByteStream());
     counter++;
     //cout << counter << " " << sizeof(SDFRBBlock) << endl;
 
@@ -449,15 +449,14 @@ word SDFRBBlock::getCurrentNumberOfBlocks()
     return nblock;
 }
 
-bool SDFRBBlock::setOutputStream(ByteStream* os, dword first)
+bool SDFRBBlock::setOutputStream(ByteStreamPtr os, dword first)
 {
     dword start = first;
     /// It sets the output stream for the fixed part (if present)
     if(type->fixedPresent)
     {
         fixed.setOutputStream(os, start);
-        delete outputstream;
-        outputstream = new ByteStream((os->stream + start), getDimension(), os->isBigendian());
+        outputstream = ByteStreamPtr(new ByteStream((os->stream + start), getDimension(), os->isBigendian()));
         start += fixed.getDimension();
     }
     if(type->variablePresent)
@@ -481,7 +480,7 @@ bool SDFRBBlock::setOutputStream(ByteStream* os, dword first)
     return true;
 }
 
-ByteStream* SDFRBBlock::generateStream(bool bigendian)
+ByteStreamPtr SDFRBBlock::generateStream(bool bigendian)
 {
     if(type->fixedPresent)
         fixed.generateStream(bigendian);
@@ -507,15 +506,15 @@ ByteStream* SDFRBBlock::generateStream(bool bigendian)
     return outputstream;
 }
 
-bool SDFRBBlock::setByteStream(ByteStream* s)
+bool SDFRBBlock::setByteStream(ByteStreamPtr s)
 {
-	//cout << "bool SDFRBBlock::setByteStream(ByteStream* s)" << " " << s << endl; //AB
+	//cout << "bool SDFRBBlock::setByteStream(ByteStreamPtr s)" << " " << s << endl; //AB
     dword bytestart=0;
     dword bytestop=0;
     this->stream->setStream(s, 0, s->getDimension() - 1);
 
-    //ByteStream*  tmpstream = new ByteStream(s->stream, s->getDimension(), s->isBigendian());
-    //ByteStream* s = new ByteStream(k->stream, k->getDimension(), k->isBigendian());
+    //ByteStreamPtr  tmpstream = new ByteStream(s->stream, s->getDimension(), s->isBigendian());
+    //ByteStreamPtr s = new ByteStream(k->stream, k->getDimension(), k->isBigendian());
     // It sets the output stream for the fixed part (if present)
     if(type->fixedPresent)
     {
diff --git a/src/SDFRBlock.cpp b/src/SDFRBlock.cpp
index 5206bb085677805eac782a92ed8591a8a1f6b1bd..f92b40d78527751cc282db66ddc94f22b9a3c331 100644
--- a/src/SDFRBlock.cpp
+++ b/src/SDFRBlock.cpp
@@ -97,17 +97,17 @@ word SDFRBlock::getNumberOfRealDataBlock(word rblockIndex)
 }
 
 
-bool SDFRBlock::setOutputStream(ByteStream* os, dword first)
+bool SDFRBlock::setOutputStream(ByteStreamPtr os, dword first)
 {
     return block[0].setOutputStream(os, first);
 }
 
-ByteStream* SDFRBlock::generateStream(bool bigendian)
+ByteStreamPtr SDFRBlock::generateStream(bool bigendian)
 {
     return block[0].generateStream(bigendian);
 }
 
-bool SDFRBlock::setByteStream(ByteStream* s)
+bool SDFRBlock::setByteStream(ByteStreamPtr s)
 {
     stream = s;
     return block[0].setByteStream(s);
diff --git a/src/Socket.cpp b/src/Socket.cpp
index c364aa3f36b7d346382635809740b3722f645825..760a8414b5d2885ce24ce32b824022beac6eec09 100644
--- a/src/Socket.cpp
+++ b/src/Socket.cpp
@@ -75,7 +75,7 @@ bool Socket::close() throw(PacketExceptionIO*)
 
 
 
-bool Socket::send ( ByteStream * b ) const throw(PacketExceptionIO*)
+bool Socket::send ( ByteStreamPtr b ) const throw(PacketExceptionIO*)
 {
     byte* stream = b->getOutputStream();
     //MSG_NOSIGNAL
@@ -93,9 +93,9 @@ bool Socket::send ( ByteStream * b ) const throw(PacketExceptionIO*)
 
 
 
-ByteStream* Socket::recv (word dim, int & status ) throw(PacketExceptionIO*)
+ByteStreamPtr Socket::recv (word dim, int & status ) throw(PacketExceptionIO*)
 {
-    //ByteStream* b = new ByteStream(dim, bigendian);
+    //ByteStreamPtr b = new ByteStream(dim, bigendian);
     byte* stream = (byte*) new byte[dim];
     /*byte* temp = (byte*) new byte[1];
     int i;
@@ -130,7 +130,7 @@ ByteStream* Socket::recv (word dim, int & status ) throw(PacketExceptionIO*)
     }
     else
     {
-        ByteStream* b = new ByteStream(stream, dim, bigendian, false);
+        ByteStreamPtr b = ByteStreamPtr(new ByteStream(stream, dim, bigendian, false));
         return b;
     }