Uploaded image for project: 'Qt Creator'
  1. Qt Creator
  2. QTCREATORBUG-23130

Debug a std::unique_ptr with custom deleter fails

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • Qt Creator 4.10.1
    • Debugger
    • All

    Description

      Variables of type std::unique_ptr<some_t> are correctly shown during a debug.
      Variables of type std::unique_ptr<some_t, some_deleter> appear as containing a wrong value.

      Example

      #include <iostream>
      #include <memory>
      
      // The most basic deleter
      template <typename Allocator>
      struct deleter {
          Allocator allocator;
          deleter() :
                  allocator() {
          }
          deleter(Allocator& allocator) :
                  allocator(allocator) {
          }
          void operator()(typename std::allocator_traits<Allocator>::value_type* ptr) {
              std::allocator_traits<Allocator>::destroy(allocator, ptr);
              std::allocator_traits<Allocator>::deallocate(allocator, ptr, 1);
          }
      };
      
      // A function that allocates and constructrs (using args) a value and returns a unique_ptr pointing to
      template <typename Allocator, typename... Args>
      std::unique_ptr<typename std::allocator_traits<Allocator>::value_type, deleter<Allocator>>
      allocate(Allocator& allocator, Args&&... args) {
          auto* ptr = std::allocator_traits<Allocator>::allocate(allocator, 1);
          std::allocator_traits<Allocator>::construct(allocator, ptr, std::forward<Args>(args)...);
          return {ptr, deleter(allocator)};
      }
      
      int main() {
          std::allocator<int> al;
          std::unique_ptr<int> a(allocate(al, 123456).release());                               // Case 1
          std::unique_ptr<int, deleter<std::allocator<int>>> b(allocate(al, 123456).release()); // Case 2
          std::cout << "a: " << *a << std::endl;
          std::cout << "b: " << *b << std::endl;
          return 0;
      }
      

      Both a and b point to the same integer value, output is

      a: 123456
      b: 123456
      

      However, if we try to debug this simple program, we see b as not accessible

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            hjk hjk
            barsan.md Mihai Barsan
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes