Hello everyone!
I am new to C++. I am learning GUI programming using gtkmm on linux. I have a frame class, and I have a library class (that writes data to sqlite) that belongs to that frame. How should I declare it - by pointer or reference? like -

class MyFrame : public Gtk::Window
{
public:
    library * NewLibrary;
}

or

class MyFrame : public Gtk::Window
{
public:
    library NewLibrary;
}

If I do it like in the first case, when I use the library's results vector<string>, say, to display database entries, when I press quit on my app there are some horrible glibc memory errors ("double free or corruption (fasttop)").
If I use the second case, everything is fine, no memory errors but it does not allow me to include a library destructor in MyFrame's destructor (causes a segmentation fault), and also it uses a bit more memory. I am afraid I might get memory leaks. What should I do?

Recommended Answers

All 4 Replies

if you assign memory on the heap in ur code then you have to release that too.. in the first case in your destructor you should delete the pointer to release the memory.

in the second case the creation and deletion of object will be taken care of by the class,you dont have to worry about calling the library class destructor in myframe, that will get called automatically..

Thanks a lot! That explains why by pointer there are have horrible memory errors!

actually your first qs is more interesting .. n something i'm trying to understand too... how to decide whether to keep it as a pointer or and object. i'll share whatever is my understanding..

what we are trying to do here is create a 'has-a' relationship.. a composition.. so basically your MyFrame class which derives from Window 'has-a' Library. if you keep it as a pointer then in future if your MyFrame class doesn't need a libarary any more you can simply assign the pointer to NULL. However adding a pointer means you need to worry about memory management. If you are sure that MyFrame will always have a library, you can keep it as an object, and MyFrame will take care of its creation and deletion. If the library Object is too large then it will add to size of MyFrame object.

will have to find the best possible solution for your case. My understanding at this point is basic, but it might help you to start off...

Thanks for the explanation. Well, it is a database project, so as soon as MyFrame loads it needs to create the library class which contains functions for accessing the data from the database. So it needs to be created and destroyed at the same time as MyFrame. I guess I will leave it as reference reference type (is that correct naming of it?) and see how it goes. The library class is not that big (about 500 lines including functions), so I guess it will not make that much of a difference. We'll see.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.