[odb-users] What is the proper practice in using smart pointer in querying

Boris Kolpackov boris at codesynthesis.com
Tue Mar 27 09:28:58 EDT 2012


Hi Wayne,

Wayne Hackle <hacklew at hotmail.com> writes:

> The new question is: what is the proper practice in using shared_ptr 
> as default pointer? I ran into very annoying errors that indicate memory
> management fault (destructors especially).
> Here is my code:
>
> [...]

I don't see anything "illegal" in your code itself (though I don't
know what the person class looks like). The only problematic part
is vector<person>. You probably want to store shared pointers there
instead of copies of the objects. I would also move the session
closer to where it is actually needed:

auto_ptr<odb::database> db(
  new odb::mysql::database("root",
                           "somepassword",
                           "somedatabase",
                           "localhost"));

vector<shared_ptr<person> > v_person;

{
  session s;
  transaction t(db->begin());

  result<person> r(db->query<person>());
  for (result<person>::iterator i(r.begin()); i != r.end(); ++i)
  {	
    shared_ptr<person> p(i.load());
    v_person.push_back(p);
  }

  t.commit();
}

Boris



More information about the odb-users mailing list