problem about the memory!

Official forum for the Chipmunk2D Physics Library.
Post Reply
yangzychina
Posts: 3
Joined: Mon Oct 31, 2011 5:15 am
Contact:

problem about the memory!

Post by yangzychina »

Hi
Thanks to your great work.
I am a new chipmunk worker,and I am not good at English.
The Chipmunk is amaze,but I found that when the hardware's memory is not enough,and you use malloc funtion to get some memory,there is not any way to judge.
For Example:
In the cpSpaceNew(),when you use cpSpaceAlloc() to get memory,but It return NULL,this mean that you can not do anyting, but the cpSpaceInit() will be excuted. I am puzzled about this because I do not want to change an code of yours.
I want konw that is there any way to avoid the programe crash when you can not malloc the memory.
Thanks for your reading,and I am respected your response.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: problem about the memory!

Post by slembcke »

Yes, Chipmunk assumes that memory allocations always succeed. It uses very very little memory so I didn't really expect it would ever be a problem. You can always replace the allocation functions with something that does more error checking and handles the error using an exception or longjmp.

From chipmunk.h

Code: Select all

#ifndef cpcalloc
	#define cpcalloc calloc
#endif

#ifndef cprealloc
	#define cprealloc realloc
#endif

#ifndef cpfree
	#define cpfree free
#endif
What hardware are you running it on that it's running out of RAM?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
yangzychina
Posts: 3
Joined: Mon Oct 31, 2011 5:15 am
Contact:

Re: problem about the memory!

Post by yangzychina »

Thanks for your reading and responsed.
I use the chipmunk in a feature phone that called mediatek(MTK),the hardware is limited by it's memroy. As I know,the memory in some version is about 700K, I think the 700K is the smallest. In some version It have more,about 1M or more.
So, I think I should to consider the worst condition,about 700K.
In the #ifndef cpcalloc
#define cpcalloc callocFun
#endif
if I define a new function to malloc the memory, It 's like that I can not do anyting if the memory malloc function return a NULL value.When the value is NULL, It also maybe a crash.
All I think the I can do is that ,I can check the memory in our hardware befor run the chipmunk engine.In this case,I can avoid the not enough memoy problem.
In normal condition with not many elements in the space ,how much memory the hardware should be? Is 700K size OK?
Thanks for your great work.
Best wish!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: problem about the memory!

Post by slembcke »

You can throw a C++ exception or use longjmp() to handle the error. It wouldn't be simple, but it would work. I'm not sure what Chipmunk should do other than crash or assert() if a memory allocation fails.

I think the most memory I've ever used with Chipmunk was 3MB for a large simulation. You can definitely work just fine on less. You'll want to adjust CP_BUFFER_BYTES to something smaller, maybe 8KB? There might be a lot of wasted memory for a small simulation otherwise. I chose 32KB because it's the page size for most virtual memory systems.

From chipmunk.h

Code: Select all

// Allocated size for various Chipmunk buffers
#ifndef CP_BUFFER_BYTES
	#define CP_BUFFER_BYTES (32*1024)
#endif
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
yangzychina
Posts: 3
Joined: Mon Oct 31, 2011 5:15 am
Contact:

Re: problem about the memory!

Post by yangzychina »

Thank you.
I have change the value of CP_BUFFER_BYTES to 8*1024, and In my project it also work well.And Save a lot of memory.
All my games will not be large.And you say that you once use 3M because of large simulate ,this is a good news for me.I am sure in my game the chipmunk can work well for me,because I haven't large simulate.
I have not met the memory problem because of our's bad hardware,but someone mention me that I should notice the memory.
Best wishes!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: problem about the memory!

Post by slembcke »

Awesome. Good luck! :)
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 37 guests