peter nitsch.net

peternitsch.net

Memory allocation issues with C++ and Alchemy

Not being a seasoned C++ programmer by any stretch of the imagination, I've run into several problems compiling Alchemy projects since it first debuted. Most of the issues were related to my poor understanding of the language, but some point to problems with the compiler. In particular, there seems to be an issue with memory allocation of non-pointer data types, such as the std::vector. I've run into this problem again while trying to port ASCIIpOrtal, and it's causing frequent hair pulling. Hopefully someone much smarter than me can point to a solution.

Here's a simplified example of what I'm trying to do:

CODE:
  1. stringstream mapfile(sfile); //sfile is a std::string
  2. vector<vector<string>> rawmaps;
  3. vector<string> map;
  4.  
  5. while (! mapfile.eof() ) {
  6.      string line;
  7.      getline (mapfile, line, '\n');
  8.      map.push_back(line);
  9. }
  10.  
  11. rawmaps.push_back(map);

This should give me a multidimensional vector (in this case rawmaps[0][0] gives me the first line of the first map), but the values are getting overwritten in 'memory'. That calls for memory allocation, which I assume means using the reserve method or defining the blocks to reserve in the constructor. For example:

CODE:
  1. rawmaps.reserve(1);
  2. map.reserve(24);

CODE:
  1. vector<vector<string>> rawmaps(1);
  2. vector<string> map(24);

But it doesn't work. The methods fire correctly and are stored correctly directly after their assignment operation, but over time the earliest values are being overwritten as if memory is not being reserved.

I had the same problem with my metaball experiment where I was using the following structure for vertex information:

CODE:
  1. struct SVertex
  2. {
  3.      float v[3]; //verts
  4.      float n[3]; //norms
  5.      float t[2]; //uvts
  6. };
  7.  
  8. SVertex *m_pVertices;

Over time the earliest values were getting overwritten. Here is a link to a very early build where the problem can be seen in action (it's super slow because I had papervision drawing the triangle mesh and still had not written any optimizations). In this case, I solved the problem by restructuring the data back to pointer types:

CODE:
  1. float *verts;
  2. float *norms;
  3. float *uvts;
  4.  
  5. verts = new float[MAX_VERTICES*3];
  6. norms = new float[MAX_VERTICES*3];
  7. uvts = new float[MAX_VERTICES*2];

This method works, but there still seems to be major issue with the memory allocation of std::vector, and possibly other data types. I've put together a simplified example of the string vectors I'm working on in an effort to demonstrate the problem. You can download it here. The example loads a text file into Alchemy, and reads/stores the length of each line in a vector, and then stores that vector inside another. You can see the values getting modified in the trace log.

If anyone has insight into this problem, I'd love to hear your feedback. I'm hoping it's just a case of my noobish C++ understanding, but likely it's not, and we'll need to make the Alchemy team aware.

UPDATE: An excellent post on the Adobe Forums confirming a major bug with std::string in Alchemy.

Category: Alchemy, Flash

Tagged: , , , ,

Leave a Reply