# Chapter 4 - memory leaks while using main DisposeBag

**URL:** <https://forums.kodeco.com/t/chapter-4-memory-leaks-while-using-main-disposebag/35191>\
**Category:** RxSwift\
**Created:** [October 4, 2017, 1:04pm UTC](https://forums.kodeco.com/t/chapter-4-memory-leaks-while-using-main-disposebag/35191 "2017-10-04T13:04:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![akozin](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/akozin/32/81494_2.png) [@akozin](https://forums.kodeco.com/u/akozin)\
**Post date:** [October 4, 2017, 1:04pm UTC](https://forums.kodeco.com/t/chapter-4-memory-leaks-while-using-main-disposebag/35191/1 "2017-10-04T13:04:54Z")

</div>

Hello, everyone.  
I just recently have read Chapter 4 and have faced to some memory leak problems.  
On page 88 usage of `DisposeBag` is described and stated, that using `.dispose(by: bag)` leads to memory leaks, because of bag would never released from memory.  
But later on page 92 code `.disposed(by: bag)` was used again to dispose from PhotoWriter subscriber. As one would expect this leads to memory leaks about which RxSwict.Recources.total reports.  
Also on challenge solution in folder 04-observables-in-practice/challenges/ there is usage on code `.disposed(by: bag)` again.

What should be the **correct** solution for disposing subscription in `actionSave` method and in challenge solution?  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![icanzilb](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/icanzilb/32/109333_2.png) [@icanzilb](https://forums.kodeco.com/u/icanzilb)\
**Post date:** [October 4, 2017, 1:20pm UTC](https://forums.kodeco.com/t/chapter-4-memory-leaks-while-using-main-disposebag/35191/2 "2017-10-04T13:20:02Z")

</div>

Subscriptions are being disposed of when the sequence terminates - e.g. if the observable emits a completed or an error event. The dispose bag is used to dispose of subscriptions that don’t terminate naturally (or haven’t terminated just yet)

---

<div class="post-metadata">

**Author:** ![akozin](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/akozin/32/81494_2.png) [@akozin](https://forums.kodeco.com/u/akozin)\
**Post date:** [October 5, 2017, 8:19am UTC](https://forums.kodeco.com/t/chapter-4-memory-leaks-while-using-main-disposebag/35191/3 "2017-10-05T08:19:49Z")

</div>

Thanks for reply!  
I also have read in book, that it’s better and simpler to add subscription to dispose bag and forget about it.  
And again we need somehow to cleanup DisposeBag in MainViewController.  
Can you suggest a correct way to do this?

---

<div class="post-metadata">

**Author:** ![icanzilb](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/icanzilb/32/109333_2.png) [@icanzilb](https://forums.kodeco.com/u/icanzilb)\
**Post date:** [October 5, 2017, 9:35am UTC](https://forums.kodeco.com/t/chapter-4-memory-leaks-while-using-main-disposebag/35191/4 "2017-10-05T09:35:03Z")

</div>

Since `MainViewController` is always present you do not need to dispose the subscriptions in its bag. If you wish to manually dispose all subscriptions just release the bag from memory, e.g. set the `bag` property to `nil`.

---

<div class="post-metadata">

**Author:** ![akozin](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/akozin/32/81494_2.png) [@akozin](https://forums.kodeco.com/u/akozin)\
**Post date:** [October 5, 2017, 11:20am UTC](https://forums.kodeco.com/t/chapter-4-memory-leaks-while-using-main-disposebag/35191/5 "2017-10-05T11:20:59Z")

</div>

But every time I click, for example, “Save” button, `actionSave()` is called and subscriptions count is increased by one and memory leaks…

```
@IBAction func actionSave() {
  guard let image = imagePreview.image else { return }
  PhotoWriter.save(image)
  .subscribe(onError: { [weak self] error in
    self?.showMessage("Error", description: error.localizedDescription)
  }, onCompleted: { [weak self] in
    self?.showMessage("Saved")
    self?.actionClear()
  })
  .disposed(by: bag)

```

}

I cannot understand.  
Why on page 88 is stated, that usage of .dispose(by: bag) leads to memory leaks?  
Here we exactly create subscription to MainViewController’s bag.

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [March 20, 2018, 5:04am UTC](https://forums.kodeco.com/t/chapter-4-memory-leaks-while-using-main-disposebag/35191/6 "2018-03-20T05:04:58Z")

</div>

This topic was automatically closed after 166 days. New replies are no longer allowed.
