U++ framework
Do not panic. Ask here before giving up.

Home » U++ Library support » U++ Core » Doubt with Buffer<> of a trivially destructible type
Re: Doubt with Buffer<> of a trivially destructible type [message #61224 is a reply to message #61223] Thu, 12 December 2024 16:05 Go to previous messageGo to previous message
koldo is currently offline  koldo
Messages: 3459
Registered: August 2008
Senior Veteran
Thank you Oblivion

Yes, there are simple solutions. This case is to alloc data to be filled by a DLL made in Delphi, with particular alignment requirements.

I have reviewed deeper in how U++ handles this, and it takes care of that in run time:
void Free() {
	if(ptr) {
		if(std::is_trivially_destructible<T>::value)  // <<========
			MemoryFree(ptr);
		else {
			void *p = (byte *)ptr - 16;
			size_t size = *(size_t *)p;
			Destroy(ptr, ptr + size);             // <<======== Destroy() is called only if there is a destructor
			MemoryFree(p);
		}
	}
}

However the compiler detects this:
template <class T>
inline void Destruct(T *t)
{
	t->~T();                        // error: object expression of non-scalar type 'double[3]' cannot be used in a pseudo-destructor expression
}

template <class T>
inline void Destroy(T *t, const T *end)
{
	while(t != end)
		Destruct(t++);
}


Best regards
IƱaki
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: How to respond when memory is exceeded
Next Topic: Stream Load serialization fired twice
Goto Forum:
  


Current Time: Sun May 24 23:05:32 GMT+2 2026

Total time taken to generate the page: 0.00875 seconds