Q: Using Visual Studio's memory leaked detection system with OpenFrameworks?

Hi All!

Here’s the memory leak detection system I would like to use with Visual Studio:
https://msdn.microsoft.com/en-us/library/x98tx3cf.aspx
Example code:

int main()
{


	// ... other OF windows setup code here ...


	//  Game is type: class Game : public ofBaseApp 
	gGame = new Game();
	ofRunApp(gGame);


	// *** Never reaches this point in the code on a normal ESC key press exit from app

	delete gGame;

	_CrtDumpMemoryLeaks(); // VS leaked reporting method

	return 0;
}

Due to the OpenFrameworks event handling, the process exits using a std::exit(0) and ofRunApp never returns for gGame to be explicitly deleted and thus _CrtDumpMemoryLeaks() is also never called.

So what I’m asking (though I’m open to other workarounds) is: How do I get ofRunApp() to return upon process shutdown so it exits normally through the end of main()?

Thanks!

ofRunApp should be returning. What version of openFrameworks are you using?

It returns the exit code so you can use:

int main(){
...
return ofRunApp(ofApp);

you can also try to explicitly create an ofMainLoop as in:

int main(){
    auto app = make_shared<ofApp>();
    ofGLFWWindowSettings settings;
    auto window = ofCreateWindow(settings);
    ofRunApp(window, app);
    return  ofRunMainLoop();
}

but it shouldn’t be necesary

Hi Arturo! Thank you for your reply. Much appreciated. I’m using 0.9.8.

Here is the method that is eventually terminating my process when ESC is pressed. I’m not doing any special handling of ESC. It’s the default OF handling of it that calls the std::exit(0);

Here:

void ofAppGlutWindow::close(){
	events().notifyExit();
	events().disable();
#ifdef TARGET_LINUX
	glutLeaveMainLoop();
#else
	std::exit(0); // <<<<<<<<<<<<<<<<<<<<<<<<<< exits here 
#endif
}

My issue is that I need _CrtDumpMemoryLeaks() to be called after all dynamically allocated objects have been freed or in my case, the last line of main() before the return 0. As shown in my example code in my first post.

I guess I can do my own handling of the ESC key press and shut it down manually. Maybe that’s the best work around.

mmh yeah if you are using the glut window then the loop never finishes but that’s window is deprecated. Are you using it for some specific reason? otherwise take a look at any of the examples to see how to change it with a glfw one or just use the code i posted above

I switched over the windows creation method with th initialization using ofGLFWWindowSettings and it now ofRunApp returns upon my call of ofExit()!

Thank you! Problem solved! :slight_smile: