Jump to content

  • Log In with Google      Sign In   
  • Create Account


CableGuy

Member Since 24 Oct 2005
Offline Last Active Yesterday, 11:57 PM

Topics I've Started

Memory management of resources

15 April 2014 - 12:17 PM

Hi,

 

I know this question had probably been asked a few times and I did search the forums but couldn't find what I'm after.

 

My question concerns memory allocation (of resources in a game) and how to avoid having to manage them manually.

 

At first, my little game framework had (almost) everything wrapped in std::shared_ptr.  I think this has impacted performance (AFAIR I did profile it) and also leads to bad design. Recently I removed every use of std::shared_ptr in favor of raw pointers and started to actually think about ownership of memory to define who has the responsibility to free the resources. I have a class called ResourcePool which given a type of a resource, it allocated it and is the owner of the resource and therefore responsible to free its memory. But what if for instance, I would like to clone a resource (because I'm going to change it and don't want everything which uses this resource to be affected).  The cloned resource wasn't created using the ResourcePool (The resource pool can create resource only from files) and therefore won't be freed by it which leaves the responsibility to the user. I can think of three solutions to the problem:

1. Use shared_ptr (or probably my own intrusive reference counting) but that's what I was try to avoid in the first place.

2. Leave it to the user, but in the long run is cumbersome and would cause memory leaks if not carefully taken care of

3. Add the resource to the ResourcePool (or other class) which will become the owner of the resource and therefore will free it.

 

I hope I was clear enough and would like to hear what other designs have you guys used to avoid having to manually manage memory in your game

 

Thanks


Scene graphs and shared_ptr

05 June 2011 - 02:05 PM

Hello,

I've created a simple scene graph implementation and have a problem with cyclic referencing. In my implementation you never hold a node or reference to a node but rather a NodePtr which is just a typedef for shared_ptr<Node>. Also each node stores internally a list of all it's children(a vector of NodePtr) and a parent which is also a NodePtr. As you can see this leads to a cyclic referencing of nodes and their parents.
I tried to come up with ideas that might solve this but still retain the same interface( using shared pointers) but the only solution I could come up with is having some kind of a SceneGraph class that hold a NodePtr to the root and when this object is destroyed iterate over the graph and break all the connections(lets assume that every NodePtr can only be a member of a single SceneGraph). I must say I'm not too fond of this solution so if anyone has got a better idea please share.

Thanks.

[C++] const and reference for primitive paramters

26 March 2011 - 02:50 AM

Hello,

I've read that passing(read only) primitive types by reference is useless, which makes sense because they are probably implemented using pointers which (in the case of an int) doesn't save space and only add one level of indirection. My question is, assuming the above statement is correct, is there any reason to declare primitive parameters as const?

Thanks.

GDNet Firefox extension

18 July 2009 - 09:55 PM

Just gave the GDNet firefox extension a version bump. Seems to be working for me. Clicky.

Redirecting console output

17 April 2009 - 11:49 PM

Hello,
As the title says I'm trying to redirect console output under Windows. I've embedded Lua in my application and now I want to redirect output from the lua script to a TextBox. I couldn't find much on the internet except someone mentioning creating a pipe and then using SetStdHandle to redirect output to this pipe and then using ReadFile on this pipe to get the output but that doesn't seem to work. The text still gets sent to the console and causing ReadFile to block my application:
HANDLE rd,wr;
if (!CreatePipe(&rd,&wr,NULL,0))
	cout << "Error creating pipe.\n";
HANDLE old=GetStdHandle(STD_OUTPUT_HANDLE);
if (!SetStdHandle(STD_OUTPUT_HANDLE,wr))
	cout << "Error setting std handle.\n";
std::cout << "yo!";	
char buf[100];
while (true)
{
	DWORD readBytes=0;
	if (!ReadFile(rd,buf,1,&readBytes,0))
		break;
	if (readBytes>0)
	{			
		string str;
		for (DWORD i=0;i<readBytes;i++)
			str+=buf[i];
		MessageBoxA(0,str.c_str(),"",0);
	}
	else
		break;
}
SetStdHandle(STD_OUTPUT_HANDLE,old);


Any suggestions/snippets of code/links are welcomed.

PARTNERS