Sigh, welcome to asynchronous programming and state changing beneath you.

> if the pointer to the doc was freed in the meantime, DOC_VALID() usage would 
> lead to memory access over invalid pointer.

Document objects are never freed, so you will never get an invalid pointer, so 
thats one less worry.  So no need to check if its in the document array, it 
will be.

But since closed document objects are reused for a different file you are 
correct that simply testing `doc->valid` is not sufficient to ensure its the 
same document, that is what `doc->id` is for, it is unique for each opened file 
and `document_find_by_id()` should be used, then valid can be checked.

See [plugin API 
docs](https://www.geany.org/manual/reference/structGeanyData.html#a933a5623cf6d0938b6e3c7b9dad686de).

The exact semantics of this changed at one point (vague memory, it was a while 
ago) and I bet lots of plugins didn't get updated, and new plugin writers 
didn't RTF above documentation. :grin:

Using `document_get_current()` is fine, so long as the callback never assumes 
its the same document as last time it was called.

Also its not guaranteed that Geany callbacks "do the right thing" either, 
`document_find_by_id()` is only used twice in Geany.  In particular when 
dialogs are run state may change, but I think most of Geany assumes it doesn't. 
 On Gnome and clones its probably safe because it honours modality which 
prevents users from modifying state (eg closing a document with the dialog 
open) but other window managers (eg KDE) do not honour modality, search the 
issues if interested.



-- 
Reply to this email directly or view it on GitHub:
https://github.com/geany/geany/issues/3872#issuecomment-2106072124
You are receiving this because you are subscribed to this thread.

Message ID: <geany/geany/issues/3872/2106072...@github.com>

Reply via email to