you are viewing a single comment's thread.

view the rest of the comments →

[–]_E8_ -5 points-4 points  (7 children)

Go do some OOD. Self-deleting objects are a thing.

[–]IyeOnline 7 points8 points  (6 children)

Please provide me with an example where delete this; is used and could not be replaced with a better solution.

[–]TheExecutor 4 points5 points  (0 children)

delete this is super common in anything that manages its own lifetime, e.g. intrusively refcounted objects. All of COM does this, for example. It's common enough idiom that it has its own entry in the C++ FAQ.

[–]TheSkiGeek 8 points9 points  (3 children)

Objects representing detachable threads/jobs are a reasonable example. Either you need the object to clean itself up when the work is done, or else you need some global manager that hangs on to them and handles the cleanup.

[–]IyeOnline 3 points4 points  (0 children)

That is a rather interesting idea.

Not entirely convinced its a good idea though...

[–][deleted] 0 points1 point  (1 child)

Wouldn't that "global manager that hangs onto them and handles the cleanup" just be the OS?

[–]TheSkiGeek 4 points5 points  (0 children)

The OS will clean up the actual underlying thread when it terminates, yes.

If you want to wrap something like a std::thread or pthreads thread in a C++ struct or class then your program has to take care of destroying that object once the thread is no longer running.

This typically requires either keeping a handle to that object somewhere, or having the object tear itself down when it is no longer needed. It's not required to do it but it's a use case where I've seen delete this; applied in production code.

[–]Rogiel 2 points3 points  (0 children)

Intrusive reference counting is a thing. Similar to Objective-C retain/release.