Font->Draw Memory Leak

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
Deis
Posts: 8
Joined: Sun Jun 26, 2005 4:02 pm

Font->Draw Memory Leak

Post by Deis »

This does not seem to be affecting regular Irrlicht. But, Irrlicht.NET I can confirm that it is happening.

Loading a font and then calling Font.Draw() method will cause memory usage to continue to grow around 4k a second depending on how many times you call it per loop.


Is anyone else experiencing this?
Deis
Posts: 8
Joined: Sun Jun 26, 2005 4:02 pm

Post by Deis »

I fixed it. It has to do with the wrapper converting unmanaged data to managed data and the Marshalling you have to do. Niko or someone just forgot to Free the temporary memory allocated during the process. Here is an updated IGUIFont.cpp you can stick in and recompile to fix the problem.

Code: Select all

// Copyright (C) 2002-2005 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h

#include "IGUIFont.h"
#include "NativeConverter.h"

namespace Irrlicht
{
namespace GUI
{

	IGUIFont::IGUIFont(irr::gui::IGUIFont* fnt)
		: Font(fnt)
	{
		Font->grab();
	}


	IGUIFont::~IGUIFont()
	{
		Font->drop();
	}

	void IGUIFont::Draw(System::String* text, Core::Rect position, 
		Video::Color color, bool hcenter, bool vcenter,
		Core::Rect cliprect)
	{
		char* str = (char*)(void*)
			System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(text);

		irr::core::rect<irr::s32> r = irr::NativeConverter::getNativeRect(cliprect);

		Font->draw(irr::core::stringw(str).c_str(), 
			irr::NativeConverter::getNativeRect(position),
			color.color, hcenter, vcenter, 
			&r);
		System::Runtime::InteropServices::Marshal::FreeHGlobal(str);
	}

	void IGUIFont::Draw(System::String* text, Core::Rect position, 
		Video::Color color, bool hcenter, bool vcenter)
	{
		char* str = (char*)(void*)
			System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(text);

		Font->draw(irr::core::stringw(str).c_str(), 
			irr::NativeConverter::getNativeRect(position),
			color.color, hcenter, vcenter, 0);
		System::Runtime::InteropServices::Marshal::FreeHGlobal(str);
	}

	Core::Dimension2D IGUIFont::GetDimension(System::String* text)
	{
		char* str = (char*)(void*)
			System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(text);

		return irr::NativeConverter::getNETDim((Font->getDimension(
			irr::core::stringw(str).c_str())));
		System::Runtime::InteropServices::Marshal::FreeHGlobal(str);
	}

	void IGUIFont::Draw(System::String* text, Core::Position2D position, Video::Color color)
	{
		char* str = (char*)(void*)
			System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(text);

		Font->draw(irr::core::stringw(str).c_str(), 
			irr::core::rect<irr::s32>(irr::NativeConverter::getNativePos(position), 
				irr::core::dimension2d<irr::s32>(0,0)),
			color.color, false, false, 0);
		System::Runtime::InteropServices::Marshal::FreeHGlobal(str);
	}

	irr::gui::IGUIFont* IGUIFont::get_NativeFont()
	{
		return Font;
	}

}
}
niko
Site Admin
Posts: 1759
Joined: Fri Aug 22, 2003 4:44 am
Location: Vienna, Austria
Contact:

Post by niko »

Wow, thanks for posting, really a bad bug.
Post Reply