From 5317cca031d2bf7ed4d68c29c6bcc5b4896a0b9c Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Wed, 10 Jun 2015 11:34:53 +0200 Subject: [PATCH] base/allocator.h: clarify use of delete operator Issue #1571 --- repos/base/include/base/allocator.h | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/repos/base/include/base/allocator.h b/repos/base/include/base/allocator.h index 4a35014d5..17729af42 100644 --- a/repos/base/include/base/allocator.h +++ b/repos/base/include/base/allocator.h @@ -269,7 +269,23 @@ void Genode::destroy(DEALLOC && dealloc, T *obj) /* call destructors */ obj->~T(); - /* free memory at the allocator */ + /* + * Free memory at the allocator + * + * We have to use the delete operator instead of just calling + * 'dealloc.free' because the 'obj' pointer might not always point to the + * begin of the allocated block: + * + * If 'T' is the base class of another class 'A', 'obj' may refer + * to an instance of 'A'. In particular when 'A' used multiple inheritance + * with 'T' not being the first inhertited class, the pointer to the actual + * object differs from 'obj'. + * + * Given the pointer to the base class 'T', however, the delete operator is + * magically (by the means of the information found in the vtable of 'T') + * able to determine the actual pointer to the instance of 'A' and passes + * this pointer to 'free'. + */ operator delete (obj, dealloc); }