Page MenuHome

Fix T87489: Text Data-Blocks get deleted on Recursive Purge
ClosedPublic

Authored by Sebastian Parborg (zeddb) on Apr 15 2021, 4:04 PM.

Details

Summary

Text data block were not considered special in the recursive purge function.
So they would get deleted if they had no actual users.

To fix this we instead make text data block use "fake user" so that addon authors can specify script files that should be removed if nothing is using it anymore.

Per default, new text object have "fake user" set. So functionality wise, the user has to explicitly specify that they want the text object to be purge-able.

Diff Detail

Repository
rB Blender

Event Timeline

Bastien Montagne (mont29) requested changes to this revision.Apr 16 2021, 4:19 PM

Looks mostly fine, mainly missing a sub-version bump.

source/blender/blenkernel/intern/text.c
177–178

you can even revert those two blocks, and create/assign the Text pointer after you checked ID users

(unrelated to that patch, but potential cleanup/refactor would be to move that check to generic calling code actually...)

298

deleted by default

299

This last sentence is closer to user manual, not needed in code comment (as not related to this piece of code at all).

474

...`have no real user but have 'fake user' enabled by default'

494–496

Same remarks as above, can even be fully removed here (doubling with the note in the function's docstring).

source/blender/blenloader/intern/versioning_290.c
711–717

This requires a sub-version bump, otherwise it would re-enable fake user even on texts where user purposely disabled it.

This revision now requires changes to proceed.Apr 16 2021, 4:19 PM
Sebastian Parborg (zeddb) updated this revision to Diff 36225.EditedApr 16 2021, 5:30 PM
Sebastian Parborg (zeddb) marked 5 inline comments as done.

Updated with latest feedback.

I didn't do the version bump yet as I guess we want this merged into both the 2.93 and the 3.0 branch.
I'm not sure how I should handle that (as the file version has diverged between the two branches)

Updated with versioning feedback from blender.chat

Patch looks good to me now...

Would not mind borrowing an eye from one of our historical devs (@Brecht Van Lommel (brecht), @Campbell Barton (campbellbarton) ?), who might know the reasoning/history behind current text usercount handling?

This revision is now accepted and ready to land.Apr 22 2021, 1:58 PM

Yes, some extra eyes sounds good. I'll hold off on commit this then.

Text datablocks have always been handled similar to scenes as far as I know. It's not clear to me why that is being changed to fake user now as a fix, instead of adding it to the list of exceptions.

The new fake user button in the text editor is not helpful to most end users, it's mostly a button that helps you lose data by accident. If the datablock selector is redesigned and this moves into a menu it's less of a risk. I guess for text datablocks autogenerated by scripts there may be a use case.

Also, we should stop adding things like this in new code:

if (ELEM(GS(id->name), ID_WM, ID_WS, ID_SCE, ID_SCR, ID_LI)) {

That's what IDTypeInfo is for.

It's not clear to me why that is being changed to fake user now as a fix, instead of adding it to the list of exceptions.

Right, my initial fix was to just add the text data blocks to be excluded from the recursive purge.
However after talking to Bastien, he told me that he thought that wasn't what we wanted to do.

Instead we would implement fake user for text data blocks so that text generated by scripts or
plugins that are tied to an object can be cleaned up when the object is removed.

I talked to Demeter about this as well, and he agreed with Bastien that having this functionality for addons/scripts is nice.
(As it can be a PITA to clean up auto generated script files that are not used anymore)

I agree that the current "fake user" button in the editor might not be optimal.
But after I discussed some alternatives with Julian, I think that having it there will make this functionality be discoverable by script and addon writers.
(And regular users of course)
However, I fully agree with you that for most people this is probably useless information and/or a liability in that they could uncheck it and lose data.
But this is also the case in other parts of blender as well, so I don't know if we want to go the "dummy proof" route or not.

Also, we should stop adding things like this in new code:

if (ELEM(GS(id->name), ID_WM, ID_WS, ID_SCE, ID_SCR, ID_LI)) {

That's what IDTypeInfo is for.

So I could change that the the preferred IDTypeInfo in the patch?
Just checking as I didn't add it, but I just changed the current code.

Also, we should stop adding things like this in new code:

if (ELEM(GS(id->name), ID_WM, ID_WS, ID_SCE, ID_SCR, ID_LI)) {

That's what IDTypeInfo is for.

This is entirely unrelated to that patch, this code is there since ages. Of course at some point it would be a nice cleanup, like ton of other similar cases in existing code. But please do not mix it with a bugfix/behavior change patch

Text datablocks have always been handled similar to scenes as far as I know. It's not clear to me why that is being changed to fake user now as a fix, instead of adding it to the list of exceptions.

The new fake user button in the text editor is not helpful to most end users, it's mostly a button that helps you lose data by accident. If the datablock selector is redesigned and this moves into a menu it's less of a risk. I guess for text datablocks autogenerated by scripts there may be a use case.

I do not see how having that kind of undocumented, spread all around the code exceptions is a good thing. We have an official mechanism to keep data around even if unused (fake user), I would much rather remove exceptions and weird special corner cases for some specific types, rather than spread that behavior into more generic ID management code. Unless there is a strong valid reason to do so.

That being said, might be worth checking if purge should not take into account the 'ensure real' flag too... But issue is this flag is primarily intended for editors (aka 'weak' actual data users)... Using it would prevent e.g. deletion of an image shown in an image editor, even if it is not actually used by any other ID.

To be clear, don't let my comments hold up a fix for this issue since it's pretty critical. The review is still marked as approved.

The current solution works, and you don't need my approval since I'm no longer chief architect.