Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Developing U++ » U++ Developers corner » U++ Allocator & Vulkan
U++ Allocator & Vulkan [message #57461] Sat, 21 August 2021 17:47 Go to next message
Xemuth is currently offline  Xemuth
Messages: 387
Registered: August 2018
Location: France
Senior Member
I installed my Vulkan work on my Debian desktop and tried to compile the most basic code :

//#include <Core/Core.h>
#include <iostream>
#include <vulkan/vulkan.h>

int main(int argc, const char *argv[])
//CONSOLE_APP_MAIN
{
	VkInstance instance;
	VkApplicationInfo appInfo{};
    appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
    appInfo.pApplicationName = "Hello Triangle";
    appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
    appInfo.pEngineName = "No Engine";
    appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
    appInfo.apiVersion = VK_API_VERSION_1_0;

    VkInstanceCreateInfo createInfo{};
    createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    createInfo.pApplicationInfo = &appInfo;
    createInfo.enabledExtensionCount = 0;
    createInfo.ppEnabledExtensionNames = nullptr;
    createInfo.enabledLayerCount = 0;

    if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
        #ifndef CORE_H
			std::cout << "Error\n";
        #else
			Upp::Cout() << "Error\n";
        #endif
    }else{
		vkDestroyInstance(instance, nullptr);
    }
}


A simple vulkan instance creation using default allocator. Here is my probblem:

If I include Core in my project without using USEMALLOC flag. This simple code result in memory leak :
https://i.imgur.com/7e7x4Gc.png
with USEMALLOC (or removing Core package) no problem happen.

From my knowledge, Vulkan, in order to work properly must have allocator that's aligned on 4 octet. May it's the reason of this error ?
Re: U++ Allocator & Vulkan [message #57463 is a reply to message #57461] Sun, 22 August 2021 09:37 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Xemuth wrote on Sat, 21 August 2021 17:47
I installed my Vulkan work on my Debian desktop and tried to compile the most basic code :

//#include <Core/Core.h>
#include <iostream>
#include <vulkan/vulkan.h>

int main(int argc, const char *argv[])
//CONSOLE_APP_MAIN
{
	VkInstance instance;
	VkApplicationInfo appInfo{};
    appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
    appInfo.pApplicationName = "Hello Triangle";
    appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
    appInfo.pEngineName = "No Engine";
    appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
    appInfo.apiVersion = VK_API_VERSION_1_0;

    VkInstanceCreateInfo createInfo{};
    createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    createInfo.pApplicationInfo = &appInfo;
    createInfo.enabledExtensionCount = 0;
    createInfo.ppEnabledExtensionNames = nullptr;
    createInfo.enabledLayerCount = 0;

    if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
        #ifndef CORE_H
			std::cout << "Error\n";
        #else
			Upp::Cout() << "Error\n";
        #endif
    }else{
		vkDestroyInstance(instance, nullptr);
    }
}


A simple vulkan instance creation using default allocator. Here is my probblem:

If I include Core in my project without using USEMALLOC flag. This simple code result in memory leak :
https://i.imgur.com/7e7x4Gc.png
with USEMALLOC (or removing Core package) no problem happen.

From my knowledge, Vulkan, in order to work properly must have allocator that's aligned on 4 octet. May it's the reason of this error ?


U++ allocator aligns to 16 bytes. More likely Vulkan is calling new/delete and it is "system" new/delete - there is no solution to the problem AFAIK. Same issue we encountered on macos; partial solution is to use U++ allocator where U++ is using it directly (which is still plentiful) and use standard new/delete elsewhere. I think it is flagSTDNEWDELETE - check PLATFORM_MACOS.
Re: U++ Allocator & Vulkan [message #57464 is a reply to message #57463] Sun, 22 August 2021 16:50 Go to previous message
Xemuth is currently offline  Xemuth
Messages: 387
Registered: August 2018
Location: France
Senior Member
mirek wrote on Sun, 22 August 2021 09:37


U++ allocator aligns to 16 bytes. More likely Vulkan is calling new/delete and it is "system" new/delete - there is no solution to the problem AFAIK. Same issue we encountered on macos; partial solution is to use U++ allocator where U++ is using it directly (which is still plentiful) and use standard new/delete elsewhere. I think it is flagSTDNEWDELETE - check PLATFORM_MACOS.


Hello Mirek, thanks for your time.
To prevent the fact Vulkan would use system new/delete instead of U++ one. I did a Custom memory allocator for Vulkan (Vulkan allow you to set up some callback to execute in order to allocate memory instead of using Vulkan default way.

Here is my 2 memory functions :

void* UVkCustomAllocator::DefaultAllocation(size_t size, size_t alignement, VkSystemAllocationScope allocationScope){
	void* ptr;
	if(alignement < size){
		ptr = malloc(size);
		ASSERT_(ptr, "Error during malloc for size of " +  AsString(size));
	}else{
		int rc = posix_memalign(&ptr, alignement, size);
		ASSERT_(rc == 0,"Error code : "+ AsString(rc) +" during posix_memalign for size of " +  AsString(size) + " and alignement of " + AsString(alignement));
	}
    return ptr;
}

void  UVkCustomAllocator::DefaultFree(void* pMemory){
	if(pMemory){
		return free(pMemory);
	}
}

Thoses functions are really simple and is based on malloc and posix_memalign. Right after implementing this (Test with some LOG), all my Vulkan call to allocate some memory use boths functions.
After tracking each malloc and free call, all the memory allocated by vulkan are correctly freed. So I don't understand why I still have some memory leaks when Vulkan don't use U++ allocator ? (or maybe I think he don't use it anymore)

PS: Moreover, when I launch my test program on Windows, (without USEMALLOC flag) I don't have any memory leak (CLANG / MVSC)
PS2: Using USEMALLOC or STD_NEWDELETE work fine even without custom memory allocator

[Updated on: Sun, 22 August 2021 16:55]

Report message to a moderator

Previous Topic: set up ide minimize option when Execute (Ctrl + F5)
Next Topic: Refining IsNull(double)
Goto Forum:
  


Current Time: Thu Mar 28 17:27:28 CET 2024

Total time taken to generate the page: 0.01356 seconds