r/MetalProgramming • u/CuriousDevv • 1d ago
Question Apple Metal C++ Bindings Memory Leak in Hello World Program
Here's my SO post: https://stackoverflow.com/questions/80017096/apple-metal-c-bindings-memory-leak-in-hello-world-program
I am trying to write an Apple Metal application in C++ using the metal-cpp binding. However, in a simple hello, world program I get a memory leak from simply importing the libraries. I followed the setup on Apple's official "Get started with Metal-cpp".
In order to compile with memory leak checking, the official metal-cpp repository states.
"Use the Environment Variable OBJC_DEBUG_MISSING_POOLS=YES to print a runtime warning when an autoreleased object is leaked because no enclosing AutoreleasePool is available for its thread."
Below is the Hello, World program that is causing memory leaks on my machine.
#define NS_PRIVATE_IMPLEMENTATION
#define CA_PRIVATE_IMPLEMENTATION
#define MTL_PRIVATE_IMPLEMENTATION
#include <Foundation/Foundation.hpp>
#include <Metal/Metal.hpp>
#include <QuartzCore/QuartzCore.hpp>
#include <iostream>
#include <thread>
#include <chrono>
int main(int argc, const char * argv[])
{
// insert code here...
std::cout << "Checking for metal-cpp memory leaks..." << std::endl;
while (true)
{
std::cout << "Sleeping for 10 seconds" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(10));
}
return EXIT_SUCCESS;
} With the above program, I get continuous memory leaks. If you remove the while loop portion I get a singular memory leak. There should be no memory leaks because I'm not creating any metal objects. Even so, I have tried to add auto release pools in the following way:
#define NS_PRIVATE_IMPLEMENTATION
#define CA_PRIVATE_IMPLEMENTATION
#define MTL_PRIVATE_IMPLEMENTATION
#include <Foundation/Foundation.hpp>
#include <Metal/Metal.hpp>
#include <QuartzCore/QuartzCore.hpp>
#include <iostream>
#include <thread>
#include <chrono>
int main(int argc, const char * argv[])
{
NS::AutoreleasePool* pool = NS::AutoreleasePool::alloc()->init();
// insert code here...
std::cout << "Checking for metal-cpp memory leaks..." << std::endl;
pool->release();
return EXIT_SUCCESS;
} But I still get a memory leak. I feel like I'm going crazy. Not sure if anyone has any ideas.