Issue in wxDocument

35 views
Skip to first unread message

David Connet

unread,
Oct 29, 2025, 2:17:30 PM (12 days ago) Oct 29
to wx-...@googlegroups.com
I just noticed that closing a wxDocument is working differently since
3.1.6 (I only tested this in 3.1.6, 3.3.1, trunk - so I'm not sure when
it changed)

In the past wxDocument::OnCloseDocument was called when I closed my
program. Now it isn't. I do have an odd setup - it's a single doc /
multi view setup.

One thing I noticed is different is in 3.1.6
`wxDocManager::CloseDocument` directly calls `doc->Close()` - that is no
longer done and it relies on

```
    // Implicitly deletes the document when
    // the last view is deleted
    doc->DeleteAllViews();
```

Evidently, I must be doing something that is preventing the doc
deletion. Any ideas on how I can work around this? Maybe something like
creating a derived wxDocManager and force closing the doc in an
overriden CloseDocument?

TIA

Dave

David Connet

unread,
Oct 29, 2025, 3:06:02 PM (12 days ago) Oct 29
to wx-...@googlegroups.com
As a follow up, I do see the document being deleted on the last view in 
wxDocument::OnChangedViewList. But OnCloseDocument never happens.

Vadim Zeitlin

unread,
Nov 2, 2025, 12:26:47 PM (8 days ago) Nov 2
to wx-...@googlegroups.com
On Wed, 29 Oct 2025 11:15:10 -0700 David Connet wrote:

DC> I just noticed that closing a wxDocument is working differently since
DC> 3.1.6 (I only tested this in 3.1.6, 3.3.1, trunk - so I'm not sure when
DC> it changed)
DC>
DC> In the past wxDocument::OnCloseDocument was called when I closed my
DC> program. Now it isn't. I do have an odd setup - it's a single doc /
DC> multi view setup.

There is nothing particularly odd about this, but I don't have a simple
way to test it and so would appreciate, as usual, if you could please
provide a minimal patch to the docview sample demonstrating the problem.

FWIW I can't reproduce it with just this:

--------------------------------- >8 --------------------------------------
diff --git a/samples/docview/doc.h b/samples/docview/doc.h
index 8313dacebd..72b014dc24 100644
--- a/samples/docview/doc.h
+++ b/samples/docview/doc.h
@@ -94,6 +94,7 @@ public:
// get direct access to our segments (for DrawingView)
const DoodleSegments& GetSegments() const { return m_doodleSegments; }

+ bool OnCloseDocument() override { wxMessageBox("Closing drawing document"); return true; }
private:
void DoUpdate();
--------------------------------- >8 --------------------------------------

i.e. the message box is shown in all cases (when closing, when exiting).

DC> One thing I noticed is different is in 3.1.6
DC> `wxDocManager::CloseDocument` directly calls `doc->Close()` - that is no
DC> longer done and it relies on
DC>
DC> ```
DC>     // Implicitly deletes the document when
DC>     // the last view is deleted
DC>     doc->DeleteAllViews();
DC> ```

I don't remember why exactly this was changed, but there were good reasons
for doing it like this, i.e. reverting this would reintroduce the bug which
was fixed by this and I'd rather not do it if we can fix this in some other
way.

Regards,
VZ

David Connet

unread,
Nov 3, 2025, 10:33:23 AM (7 days ago) Nov 3
to wx-...@googlegroups.com
On 11/2/2025 9:26 AM, Vadim Zeitlin wrote:
> On Wed, 29 Oct 2025 11:15:10 -0700 David Connet wrote:
>
> DC> I just noticed that closing a wxDocument is working differently since
> DC> 3.1.6 (I only tested this in 3.1.6, 3.3.1, trunk - so I'm not sure when
> DC> it changed)
> DC>
> DC> In the past wxDocument::OnCloseDocument was called when I closed my
> DC> program. Now it isn't. I do have an odd setup - it's a single doc /
> DC> multi view setup.
>
> There is nothing particularly odd about this, but I don't have a simple
> way to test it and so would appreciate, as usual, if you could please
> provide a minimal patch to the docview sample demonstrating the problem.

I'll try to get to the root of this... I suspect it's something to do
with my multiview/single-doc setup. (First thing to so is run the sample
in the debugger to see the path of how it closes. And then backtrack
mine to see where that deviates... But in the meantime, (sigh) the job
calls...)

In the meantime, I did find a workaround by

```
void CAgilityBookDoc::OnChangedViewList()
{
    // Same as base class, but added OnCloseDocument because it's not
happening.
    if (m_documentViews.empty() && OnSaveModified())
    {
#if wxCHECK_VERSION(3, 3, 0)
        OnCloseDocument();
#endif
        delete this;
    }
}
```

Dave

Vadim Zeitlin

unread,
Nov 3, 2025, 10:35:48 AM (7 days ago) Nov 3
to wx-...@googlegroups.com
On Mon, 3 Nov 2025 07:33:18 -0800 David Connet wrote:

DC> In the meantime, I did find a workaround by
DC>
DC> ```
DC> void CAgilityBookDoc::OnChangedViewList()
DC> {
DC>     // Same as base class, but added OnCloseDocument because it's not
DC> happening.
DC>     if (m_documentViews.empty() && OnSaveModified())
DC>     {
DC> #if wxCHECK_VERSION(3, 3, 0)
DC>         OnCloseDocument();
DC> #endif
DC>         delete this;
DC>     }
DC> }
DC> ```

This is going to result in double calls to OnCloseDocument() (with
possibly catastrophic results) when this is fixed. I recommend using
something like

#if wxCHECK_VERSION(3,3,0) && !wxCHECK_VERSION(3,3,2)

and updating the last number later if this is still not fixed in 3.3.2.

Regards,
VZ

David Connet

unread,
Nov 3, 2025, 10:51:25 AM (7 days ago) Nov 3
to wx-...@googlegroups.com
Good point. I'll also add one of my "#pragma PRAGMA_TODO(check this
comment)" lines here so it keeps nagging me (that resolves into a
"#pragma message")

Dave

David Connet

unread,
Nov 3, 2025, 1:19:50 PM (7 days ago) Nov 3
to wx-...@googlegroups.com
On 11/3/2025 7:35 AM, Vadim Zeitlin wrote:
Ohhhh....

Since I have a multiview/single-doc (the multiple views are each in a
tab), I don't want a view closing to delete the document. So I overrode
wxView::OnClose and prevented the wxDocument::OnClose call. So there is
no bug in wxDocument - it's just a change of behavior due to the bug fix
you mentioned.

So for my setup, it looks like the workaround I found above is actually
the right thing to do. (In my case, OnCloseDocument should never return
false so ignoring the return should be ok.)

I'll add more comments in my code so I remember why in the future! (in
both doc/view overrides I have)

Dave

Reply all
Reply to author
Forward
0 new messages