Fix memory leak 34/307934/3
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 14 Mar 2024 10:40:45 +0000 (19:40 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Thu, 14 Mar 2024 10:49:47 +0000 (19:49 +0900)
The allocated GSource should be released.
Calling g_source_unref() is needed.

Change-Id: Icee8d138ee8b165b9cef5c5b1203d8e7b2228c07
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
tizen_base/source.cc
tizen_base/source.h
tizen_base/stub.cc
tizen_base/task.cc

index 3a6e57823f487025ac2e5930a2d1540d226c7d14..91815d984da4bbe49995e9255c606ca3c73593fc 100644 (file)
@@ -91,8 +91,8 @@ Source::~Source() {
     }
     // LCOV_EXCL_STOP
 
-    if (!g_source_is_destroyed(handle_))
-      g_source_destroy(handle_);
+    if (!g_source_is_destroyed(handle_)) g_source_destroy(handle_);
+    if (!attached_) g_source_unref(handle_);
 
     source_manager.Erase(handle_);
   }
@@ -126,6 +126,8 @@ void Source::Attach(const std::shared_ptr<Context>& context) {
   attached_ = true;
 }
 
+void Source::SetAttached(bool attached) { attached_ = attached; }
+
 bool Source::IsAttached() const { return attached_; }
 
 GSource* Source::GetHandle() const { return handle_; }
index 1b981e113af362d8120162325b5df6f2f126d2c2..34fa51b6b285a23f5e72bdd5400a7562b235f78d 100644 (file)
@@ -42,6 +42,7 @@ class EXPORT_API Source : public ISource,
   void AddPoll(std::shared_ptr<PollFd> poll_fd) override;
   void RemovePoll(const std::shared_ptr<PollFd>& poll_fd) override;
   void Attach(const std::shared_ptr<Context>& context) override;
+  void SetAttached(bool attached);
   bool IsAttached() const;
   GSource* GetHandle() const;
   void RefSelf() override;
index 556bf30e78ddd23abb6d3433bb1226aeef585a19..f59d9c479fad26d62e1fd61857c58e59a2966801 100644 (file)
@@ -431,7 +431,7 @@ API int tizen_core_source_destroy(tizen_core_source_h source) {
     return TIZEN_CORE_ERROR_INVALID_PARAMETER;
   }
 
-  auto* handle = static_cast<tizen_base::tizen_core::Source*>(source);
+  auto* handle = static_cast<SourceExt*>(source);
   handle->UnrefSelf();
   return TIZEN_CORE_ERROR_NONE;
 }
@@ -556,7 +556,7 @@ API int tizen_core_poll_fd_create(tizen_core_poll_fd_h* poll_fd) {
 
   auto handle = std::make_shared<tizen_base::tizen_core::PollFd>();
   if (handle == nullptr) {
-    _E("Out of memory"); // LCOV_EXCL_LINE
+    _E("Out of memory");  // LCOV_EXCL_LINE
     return TIZEN_CORE_ERROR_OUT_OF_MEMORY;  // LCOV_EXCL_LINE
   }
 
index f97fe218529c9d994a78addce241eaf239ff7906..01e3c22838eaaa38bc8472f5b7085c0b1f021b54 100644 (file)
@@ -260,6 +260,7 @@ std::shared_ptr<ISource> Task::AddChannel(
   auto source = std::make_shared<ChannelSource<T>>(
       shared_from_this(), std::move(receiver), std::move(cb));
   AddSource(source);
+  source->SetAttached(true);
   return source;
 }
 
@@ -269,6 +270,7 @@ std::shared_ptr<ISource> Task::AddEvent(
   auto source =
       std::make_shared<EventSource<T>>(shared_from_this(), std::move(broker));
   AddSource(source);
+  source->SetAttached(true);
   AddEventSource(source);
   return source;
 }