{"id":2149,"date":"2011-07-23T12:38:10","date_gmt":"2011-07-23T02:38:10","guid":{"rendered":"http:\/\/www.vectorstorm.org\/?p=2149"},"modified":"2011-07-23T12:38:10","modified_gmt":"2011-07-23T02:38:10","slug":"on-the-pleasures-of-memory-management","status":"publish","type":"post","link":"https:\/\/www.vectorstorm.com.au\/2011\/07\/23\/on-the-pleasures-of-memory-management\/","title":{"rendered":"On the pleasures of memory management"},"content":{"rendered":"
Error:\u00a0 Allocation from MMO_GroundClutter.cpp line 42 was allocated using new []\u00a0<\/strong>but was freed using delete;\u00a0 should have been delete []!<\/strong><\/p>\n I can’t say how proud I was to have had my memory management system be smart enough to print out such a cogent and useful error message. \u00a0I’m less certain about how bright an idea it was for it to do the correct thing after displaying the error, instead of crashing immediately. \u00a0This error message, for example, has been generated by the game for three days now, and I only noticed it today. \u00a0If it had crashed immediately, I would have fixed it at once.<\/p>\n For those who aren’t familiar with the pleasures of C++, a large part of working with the language has to do with requesting and returning blocks of memory (by which we traditionally mean RAM). \u00a0We use those blocks of memory to store data temporarily. \u00a0Textures, models, various bits of gameplay, etc. \u00a0In C, there was only one way to get a block of memory: \u00a0you called “malloc()”, passing in the size of the data block you wanted to receive. \u00a0And when you were done with that data block, you called “free()”, the same way.<\/p>\n In C++, for various reasons, two more methods were added: \u00a0“new” (for creating an instance of a C++ class), and “new []” (for creating a lot of instances of a C++ class in a big array). \u00a0Because these different blocks of memory contain different data and are allocated in different ways, they need to be freed in different ways. \u00a0For “new”, you must call “delete” to release the object. \u00a0For “new[]”, you must call “delete[]”. \u00a0In many memory management implementations, calling the wrong one will cause your program to crash, so it’s important to get this right.<\/p>\n VectorStorm’s memory management system isn’t a fast one. \u00a0I should say that up front. \u00a0When a game asks for memory, the memory management system does a pretty dumb linear search through its preallocated heap space looking for a spare area that it can hand out. \u00a0It also isn’t very memory-efficient; \u00a0it allocates extra space before and after each memory block that it hands out, storing information about how the block was allocated, by which file and which line of code, as well as boundary guards for detecting whether something has accidentally written past the end of the block.<\/p>\n These things all make it possible to easily detect issues like mismatched allocation\/deallocations, leaks, and other places where programs have accessed memory incorrectly. \u00a0So every time I make a mistake about these things (which thankfully isn’t very often), it’s there to poke me with a stick.<\/p>\n It’s worth also noting that VectorStorm’s memory management also isn’t friendly to devices which want to use virtual memory (i.e. almost every modern computer or phone), since it handles all its own allocations inside of its own huge data block. \u00a0From the OS’s point of view, a VectorStorm game uses only a single memory block, which is several megabytes in size. \u00a0(In the case of MMORPG Tycoon 2, it’s currently about 600 megabytes in size, and will eventually be even larger. \u00a0Most of my other games are in the 6-30 megabyte range).<\/p>\n Between it’s lack of speed and lack of friendliness toward virtual memory support (or fragmented memory in general on the target device, as is often the case on, for example, the iPhone), I expect that for any major release I’d swap over to using the regular OS-provided allocation\/deallocation functions instead of VectorStorm’s built-in ones. \u00a0But for the purpose of development, it’s great having a system like this there to catch errors early. \u00a0I know from long experience that it can be a real bear to track these things down otherwise.<\/p>\n","protected":false},"excerpt":{"rendered":" Error:\u00a0 Allocation from MMO_GroundClutter.cpp line 42 was allocated using new []\u00a0but was freed using delete;\u00a0 should have been delete []! I can’t say how proud I was to have had my memory management system be smart enough to print out such a cogent and useful error message. \u00a0I’m less certain about how bright an idea…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[33],"tags":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/po9WK-yF","_links":{"self":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts\/2149"}],"collection":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/comments?post=2149"}],"version-history":[{"count":0,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts\/2149\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/media?parent=2149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/categories?post=2149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/tags?post=2149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}