[Geany-Devel] On document pointer recycling

Thomas Martitz thomas.martitz at xxxxx
Thu Oct 24 07:39:49 UTC 2013

Am 24.10.2013 05:39, schrieb Lex Trotman:
> PS On the recycling of doc structures and doc->is_valid, this does 
> have the advantage (for a structure where miscellaneous pointers to 
> the structure are going to exist in Geany and plugins) that doc 
> pointers will always point to a geanydocument struct.  So the is_valid 
> test is always right.  If the memory was returned and re-cycled into 
> some other struct, the old doc pointers could point to anything, and 
> could just as easily appear a valid document.  So its safer than the 
> alternative, but the requirement to check is_valid really does need 
> more visibility since its an unusual idiom.

The recycling & is_valid thing is completely bogus. It is wastes memory 

The is_valid thing is giving false security. Because a doc becomes valid 
again after it was recycled you cannot be sure that the document is 
still the same. Yet DOC_VALID() gives a false positive here if code 
missed the document-swap behind the scenes. So basically code that cares 
about GeanyDocument cannot rely on DOC_VALID() anyway.

The _only_ way to make sure things are alright is to cleanup 
per-document stuff in the "document-close" signal handler. If you don't 
connect to this signal your per-document code is broken, regardless of 
doc->is_valid. Even worse, this brokenness is hidden by valid doc 
pointers, whereas it would otherwise crash and suggest debugging the 
problem properly. The right way is to NULL'ify any cached document 
pointer in the signal-handler which makes recycling pointers unnecessary.

Also, recycling the pointers is only possible because the document array 
never shrinks. So if you had opened a large number of documents and 
close many of them you waste a lot of memory.

I vote for removing this idiom and transition the documents array to a 
GList which gives other nice advantages.

Best regards.

More information about the Devel mailing list