slembcke / Chipmunk2D

A fast and lightweight 2D game physics library.
http://chipmunk2d.net
MIT License
2.22k stars 352 forks source link

[Documentation] Custom cpcalloc actually does need to zero memory #250

Open nbr0wn opened 4 months ago

nbr0wn commented 4 months ago

In "Memory management the Chipmunk Way", there is a section which reads:

cpSpaceAlloc() – Allocates but does not initialize a cpSpace struct. All allocation functions look more or less like this: return (cpSpace *)cpcalloc(1, sizeof(cpSpace)); You can write your own allocation functions if you want. It is not a requirement that the memory be zeroed.

..turns out if you don't zero the memory, you can fail on an assert in cSpatialIndexInit() - cSpacialIndex.c line 41:

cpAssertHard(!staticIndex->dynamicIndex, "This static index is already associated with a dynamic index.");

... because the dynamicIndex pointer contains whatever garbage was in memory at alloc time.