Add weak reference to delegate object 77/170577/1
authorJunghoon Park <jh9216.park@samsung.com>
Wed, 21 Feb 2018 01:33:48 +0000 (10:33 +0900)
committerJunghoon Park <jh9216.park@samsung.com>
Wed, 21 Feb 2018 01:33:48 +0000 (10:33 +0900)
- The weak reference of service object was added to check if it is valid or
  not

Change-Id: I39fb246d1d220c9af0bdd8d1cb4aa3af69091a35
Signed-off-by: Junghoon Park <jh9216.park@samsung.com>
idlc/cs_gen/cs_gen_base.cc

index fb15b47..ba9d1a3 100644 (file)
@@ -272,7 +272,7 @@ std::string CsGeneratorBase::ConvertTypeToDeserializer(
       ret = n + " ";
 
     if (IsDelegateType(type.ToString())) {
-      ret += id + " = new " + n +"(port);\n";
+      ret += id + " = new " + n +"(port, new WeakReference(b));\n";
       ret += "CallbackBase.";
     } else {
       ret += id + " = new " + n +"();\n";
@@ -467,9 +467,10 @@ void CsGeneratorBase::GenCallback(std::ofstream& stream,
       "{\n" \
       "}\n" \
       "\n" \
-      "internal $$(IntPtr port) : base((int)DelegateId.$$)\n" \
+      "internal $$(IntPtr port, WeakReference service) : base((int)DelegateId.$$)\n" \
       "{\n" \
       "    _port = port;\n" \
+      "    _service = service;\n" \
       "}\n";
 
     GenTemplate(AddIndent(TAB_SIZE * 4, ctor), stream,
@@ -489,6 +490,7 @@ void CsGeneratorBase::GenCallback(std::ofstream& stream,
     stream << NLine(1);
 
     stream << Tab(4) << "private IntPtr _port;" << NLine(1);
+    stream << Tab(4) << "private WeakReference _service;" << NLine(1);
     stream << Tab(4) << "public delegate void Callback(";
     GenParameters(stream, decl.GetParameters());
     stream << ");" << NLine(1);
@@ -532,8 +534,10 @@ void CsGeneratorBase::GenReceivedEvent(std::ofstream& stream,
 void CsGeneratorBase::GenInvokeMethod(std::ofstream& stream,
                                       const Declaration& decl) {
   const char* pre =
-      "if (_port == IntPtr.Zero)\n" \
+      "if (_port == IntPtr.Zero || _service == null)\n" \
       "    throw new InvalidOperationException(\"Not connected!\");\n" \
+      "if (!_service.IsAlive)\n" \
+      "    throw new InvalidOperationException(\"Invalid delegate object!\");\n" \
       "\n" \
       "Interop.LibRPCPort.Parcel.Create(out IntPtr p);\n" \
       "Interop.LibRPCPort.Parcel.WriteInt32(p, (int)MethodId.Callback);\n" \