|
Boost : |
Subject: Re: [boost] discussion of garbage collection in C++
From: Simonson, Lucanus J (lucanus.j.simonson_at_[hidden])
Date: 2009-04-21 14:31:02
Bastek wrote:
> struct User;
> struct Group
> {
> std::vector<User*> users;
> };
>
> struct User
> {
> Group* group;
> };
>
> Taking into account that the data can be used in multiple modules.
> Do you know when and where to release the memory for this type of
> objects?
>
> C++ language, enforces restrictions on mapping the structures of
> databases, because that does not have a Garbage Collector.
Yes. The problem arises because the design is incomplete. Let me finish it for you:
struct DataBase {
std::list<User> users;
std::list<Group> groups;
}
You release the memory when you release the data base and the code is exception safe and won't leak if you need to destroy the entire database due to a fatal error like screwing up the integrity of those pointers. You can also add and remove users and groups incrementally, of course. Not associating them with a database object is like associating them to a global object. That is bad design. This way you need to know which database you are working with by passing it into the functions that manipulate it (or make them member functions if you are old school), but not having a database object is not having a OO design. I never need to type new and delete to use this design. There is no question that writing C-style code in C++ is harder than writing Java, but that's not a fair comparison.
Regards,
Luke
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk