THRIFT-1621 Delphi Memory leaks
Patch: Jens Geyer

git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1346814 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/generate/t_delphi_generator.cc b/compiler/cpp/src/generate/t_delphi_generator.cc
index 729f2b9..0f83b85 100644
--- a/compiler/cpp/src/generate/t_delphi_generator.cc
+++ b/compiler/cpp/src/generate/t_delphi_generator.cc
@@ -1611,7 +1611,8 @@
   indent_impl(s_service_impl) << "TProtocolUtil.Skip(iprot, TType.Struct);" << endl;
   indent_impl(s_service_impl) << "iprot.ReadMessageEnd();" << endl;
   indent_impl(s_service_impl) << "x := TApplicationException.Create(TApplicationException.TExceptionType.UnknownMethod, 'Invalid method name: ''' + msg.Name + '''');" << endl;
-  indent_impl(s_service_impl) << "oprot.WriteMessageBegin(TMessageImpl.Create(msg.Name, TMessageType.Exception, msg.SeqID));" << endl;
+  indent_impl(s_service_impl) << "msg := TMessageImpl.Create(msg.Name, TMessageType.Exception, msg.SeqID);" << endl;
+  indent_impl(s_service_impl) << "oprot.WriteMessageBegin( msg);" << endl;
   indent_impl(s_service_impl) << "x.Write(oprot);" << endl;
   indent_impl(s_service_impl) << "oprot.WriteMessageEnd();" << endl;
   indent_impl(s_service_impl) << "oprot.Transport.Flush();" << endl;
@@ -1691,6 +1692,7 @@
   indent_up_impl();
   indent_impl(s_service_impl) << "args: " << args_intfnm << ";" << endl;
   if (!tfunction->is_oneway()) {
+    indent_impl(s_service_impl) << "msg: IMessage;" << endl;
     indent_impl(s_service_impl) << "ret: " << result_intfnm << ";" << endl;
   }
 
@@ -1760,7 +1762,8 @@
   }
 
   if (! tfunction->is_oneway()) {
-    indent_impl(s_service_impl) << "oprot.WriteMessageBegin( TMessageImpl.Create('" << tfunction->get_name() << "', TMessageType.Reply, seqid)); " << endl;
+    indent_impl(s_service_impl) << "msg := TMessageImpl.Create('" << tfunction->get_name() << "', TMessageType.Reply, seqid); " << endl;
+    indent_impl(s_service_impl) << "oprot.WriteMessageBegin( msg); " << endl;
     indent_impl(s_service_impl) << "ret.Write(oprot);" << endl;
     indent_impl(s_service_impl) << "oprot.WriteMessageEnd();" << endl;
     indent_impl(s_service_impl) << "oprot.Transport.Flush();" << endl;
diff --git a/lib/delphi/src/Thrift.Protocol.JSON.pas b/lib/delphi/src/Thrift.Protocol.JSON.pas
index 3c54386..2cc4bbd 100644
--- a/lib/delphi/src/Thrift.Protocol.JSON.pas
+++ b/lib/delphi/src/Thrift.Protocol.JSON.pas
@@ -62,7 +62,7 @@
       // This base context does nothing.
       TJSONBaseContext = class
       protected
-        FProto : IJSONProtocol;
+        FProto : Pointer;  // weak IJSONProtocol;
       public
         constructor Create( const aProto : IJSONProtocol);
         procedure Write;  virtual;
@@ -97,7 +97,7 @@
       // Holds up to one byte from the transport
       TLookaheadReader = class
       protected
-        FProto : IJSONProtocol;
+        FProto : Pointer;  // weak IJSONProtocol;
         constructor Create( const aProto : IJSONProtocol);
 
       private
@@ -335,7 +335,7 @@
 constructor TJSONProtocolImpl.TJSONBaseContext.Create( const aProto : IJSONProtocol);
 begin
   inherited Create;
-  FProto := aProto;
+  FProto := Pointer(aProto);
 end;
 
 
@@ -368,7 +368,7 @@
 begin
   if FFirst
   then FFirst := FALSE
-  else FProto.Transport.Write( COMMA);
+  else IJSONProtocol(FProto).Transport.Write( COMMA);
 end;
 
 
@@ -376,7 +376,7 @@
 begin
   if FFirst
   then FFirst := FALSE
-  else FProto.ReadJSONSyntaxChar( COMMA[0]);
+  else IJSONProtocol(FProto).ReadJSONSyntaxChar( COMMA[0]);
 end;
 
 
@@ -396,8 +396,8 @@
   end
   else begin
     if FColon
-    then FProto.Transport.Write( COLON)
-    else FProto.Transport.Write( COMMA);
+    then IJSONProtocol(FProto).Transport.Write( COLON)
+    else IJSONProtocol(FProto).Transport.Write( COMMA);
     FColon := not FColon;
   end;
 end;
@@ -411,8 +411,8 @@
   end
   else begin
     if FColon
-    then FProto.ReadJSONSyntaxChar( COLON[0])
-    else FProto.ReadJSONSyntaxChar( COMMA[0]);
+    then IJSONProtocol(FProto).ReadJSONSyntaxChar( COLON[0])
+    else IJSONProtocol(FProto).ReadJSONSyntaxChar( COMMA[0]);
     FColon := not FColon;
   end;
 end;
@@ -427,7 +427,7 @@
 constructor TJSONProtocolImpl.TLookaheadReader.Create( const aProto : IJSONProtocol);
 begin
   inherited Create;
-  FProto   := aProto;
+  FProto   := Pointer(aProto);
   FHasData := FALSE;
 end;
 
@@ -438,7 +438,7 @@
   then FHasData := FALSE
   else begin
     SetLength( FData, 1);
-    FProto.Transport.ReadAll( FData, 0, 1);
+    IJSONProtocol(FProto).Transport.ReadAll( FData, 0, 1);
   end;
   result := FData[0];
 end;
@@ -448,7 +448,7 @@
 begin
   if not FHasData then begin
     SetLength( FData, 1);
-    FProto.Transport.ReadAll( FData, 0, 1);
+    IJSONProtocol(FProto).Transport.ReadAll( FData, 0, 1);
     FHasData := TRUE;
   end;
   result := FData[0];