On 03/02/2010 04:09 PM, John McCabe-Dansted wrote:
On Tue, Mar 2, 2010 at 1:30 AM, Abdelrazak Younes<you...@lyx.org> wrote:
Problem is you are returning a pointer for a local variable... This is wrong as
the model should be persistent of course. So you need to somewhat cache the
buffer language model:
Thanks.
+QAbstractItemModel * GuiApplication::languageModel(Buffer const * buffer)
+{
+ if (!buffer)
+ return 0;
+ QSortFilterProxyModel * model =
d->buffer_language_models_.value(buffer, 0);
+ if (!model) {
+ model = new QSortFilterProxyModel(this);
+ d->buffer_language_models_[buffer] = model;
+ }
+ updateLanguageModel(model, buffer->getLanguages());
+ return model;
Wouldn't this leak memory when a Buffer is closed?
Well, not really a memory leak because the QSortFilterProxyModel object
is still here but you're right, it should be deleted once the buffer is
closed.
In Java this would be solved using Weak References but don't I think
we have those in C++ unless we use a custom memory manager.
There are weak pointers in boost or in Qt but we don't want to use this
here.
How would we solve this in LyX? From a memory management point of view
it would make sense to put buffer_language_models_ in Buffer, but
presumably not from a separation of GUI POV.
Well, ideally yes, it should be in Buffer but I am not sure where we are
here with regard to using Qt code in src/...
I am not sure where buffer closing is done nowadays (either in GuiView
or in GuiApplication) but if not in GuiApplication, we need to
deregister the closing buffer indeed. Maybe a new method
GuiApplication::deregisterBuffer(Buffer *), or maybe we should just
maintain this buffer language model in GuiView instead and use the
current document buffer:
QAbstractItemModel * GuiView::bufferLanguageModel()
{
Buffer * const buffer = documentBufferView()
? &documentBufferView()->buffer() : 0;
if (!buffer)
return 0;
if (!d->buffer_language_model_)
d->buffer_language_model_ = new QSortFilterProxyModel(this);
updateLanguageModel(d->buffer_language_model_, buffer->getLanguages());
return ->buffer_language_model_;
}