[odb-users] std bad allocation

Сергей Никонов shprotello at mail.ru
Tue Nov 28 05:35:51 EST 2017


I receive the std::bad_allocation exception when try to update a BLOB field in the persistent object with two BLOBs put in two sections:

#pragma db object
    class product 
      typedef std::uint64_t id_t;
      typedef std::vector<char> image_t;
      typedef std::vector<char> bson_t;

      ~product() {}

      id_t id() const { return m_id; }

      const odb::section& image_section() const { return m_image_section; }
      odb::section& image_section() { return m_image_section; }

      const odb::section& bson_section() const { return m_bson_section; }
      odb::section& bson_section() { return m_bson_section; }

      // ...

      friend class odb::access;

      product() : m_id(0) {}

#pragma db id auto
      id_t m_id;
#pragma db load(lazy) update(manual)
      odb::section m_image_section;
#pragma db null type("BYTEA") section(m_image_section)
      image_t m_image;
#pragma db load(lazy) update(manual)
      odb::section m_bson_section;
#pragma db null type("BYTEA") section(m_bson_section)
      image_t m_bson;

This is a sample code that generates the exception:

auto product = db.load<product>(product_id);
// The line below throws std::bad_alloc
db.load(*product, product->bson_section());
// -------------------------------------------------------
db.update(*product, product->bson_section());

This is the place in the generated code that throws the exception.  i.m_image_size here is 14829735431805717965, but at the same time i.m_image_null is true (m_image field is NULL in the database). The error takes place if the m_bson field isn't NULL in the database.

bool access::object_traits_impl< ::product, id_pgsql >::bson_section_traits::
  grow (image_type& i,
        bool* t)

    bool grew (false);

    // m_id
    t[0UL] = 0;

    // m_owner
    t[1UL] = 0;

    // m_item
    if (t[2UL])
      i.m_item_value.capacity (i.m_item_size);
      grew = true;

    // m_image
    if (t[3UL])
      i.m_image_value.capacity (i.m_image_size);
      grew = true;

    // m_bson
    if (t[4UL])
      i.m_bson_value.capacity (i.m_bson_size);
      grew = true;

    return grew;

Similar error happens from time to time for other classes in similar situations.


More information about the odb-users mailing list