KhronosGroup / Vulkan-Hpp

Open-Source Vulkan C++ API
Apache License 2.0
3.13k stars 307 forks source link

Under MSVC the debug allocation macros conflict with the methods free()... #1200

Closed davidbien closed 2 years ago

davidbien commented 2 years ago

The debug heap macros under MSVC are, unfortunately, named the same as the normal c methods.

So, free is defined as:

#define free(p) _free_dbg(p, _NORMAL_BLOCK) It is really nice to be able to use the debug heap to find memory leaks.

Luckily, there is an easy fix that I have applied locally, for each instance enclose the name free in parenthesis and this prevents looking up the preprocessor macro. i.e.:

  template <typename Dispatch>
  VULKAN_HPP_INLINE void Device::free( VULKAN_HPP_NAMESPACE::CommandPool           commandPool,

becomes:

  template <typename Dispatch>
  VULKAN_HPP_INLINE void (Device::free)( VULKAN_HPP_NAMESPACE::CommandPool           commandPool,

The other solution is to change the methods to a name that doesn't clash with the macro, i.e. vkfree() for instance.

Repro:

#ifdef WIN32
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#ifndef NDEBUG
    #define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#else
    #define DBG_NEW new
#endif
#else //!WIN32
#define DBG_NEW new
#endif //!WIN32

#define __NDEBUG_THROW
#define TRACESENABLED 0

#include <iostream>
#include <stdexcept>
#include <cstdlib>

#define GLFW_INCLUDE_VULKAN
#include <glfw3.h>
#include <vulkan/vulkan_raii.hpp>

Now compile anything...

asuessenbach commented 2 years ago

Thanks a lot for reporting this issue. And especially for providing this nice little workaround. Will add that right now.

davidbien commented 2 years ago

Wow – that was fast. Thanks. I reviewed just for completeness and it seems that the changes to the source correspond to my local changes.

Sent from Mailhttps://go.microsoft.com/fwlink/?LinkId=550986 for Windows

From: Andreas @.> Sent: Thursday, February 10, 2022 4:59 AM To: @.> Cc: David L. @.>; @.> Subject: Re: [KhronosGroup/Vulkan-Hpp] Under MSVC the debug allocation macros conflict with the methods free()... (Issue #1200)

Closed #1200https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FKhronosGroup%2FVulkan-Hpp%2Fissues%2F1200&data=04%7C01%7C%7Ca9ed6f25781543f41a0e08d9ec8ccffc%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637800911794748461%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=PaoeUZc%2Fdj2z5pVfeuLUPTGgnm%2BcACw0zFjm81SGsQk%3D&reserved=0 via #1201https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FKhronosGroup%2FVulkan-Hpp%2Fpull%2F1201&data=04%7C01%7C%7Ca9ed6f25781543f41a0e08d9ec8ccffc%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637800911794748461%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=EXCgFtIvJW9M5mfbACkFO%2FND2vP9wGsjHKvergckdvo%3D&reserved=0.

— Reply to this email directly, view it on GitHubhttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FKhronosGroup%2FVulkan-Hpp%2Fissues%2F1200%23event-6041579456&data=04%7C01%7C%7Ca9ed6f25781543f41a0e08d9ec8ccffc%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637800911794748461%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=eLnW0NrW0%2BiQXbgkH7iepZ8KskDiNckLCg97cbb%2BQFQ%3D&reserved=0, or unsubscribehttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAKSVVVA3B44OLOJFMT2WA4TU2OSCTANCNFSM5N7RR25A&data=04%7C01%7C%7Ca9ed6f25781543f41a0e08d9ec8ccffc%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637800911794748461%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=umXmR7dukTqkZRAWE%2BMVcRtK0h53qCgZXPL%2Fd%2F2gmgw%3D&reserved=0. You are receiving this because you authored the thread.Message ID: @.***>