1

(I've never asked a question here before so please excuse me if I don't follow proper etiquette)

I'm working on a scene graph for use in a GLWidget, and I'm running into a weird case of bad_alloc. Each node (called a graphNode) in my scene graph has the following fields:

QString* name
graphNode* parent
std::vector<graphNode*> children
myPolygon* polygon

(myPolygon is a custom polygon class I created to store vertex and color data)

My SceneGraph class has the following fields:

graphNode* root
std::vector<graphNode*> allNodes
std::vector<myPolygon> allShapes

the bad_alloc occurs when I'm trying to build allNodes. my root ndoe has 5 children, and if I use std::cout to print the size of the children vector I do get 5. However, when I try to push each child to allNodes I get a std::bad_alloc error. The exact Application ouput in QT is

terminate called after throwing an instance of 'std::bad_alloc'
what():  std::bad_alloc

I'm not sure what I'm doing wrong, especially since the program is recognizing that the children exist (and when I step through using print statements and the debugger I can see that the children are all what I expect them to be. Any help would be greatly appreciated, and if I need to provide my code I would be more than glad to do so.

EDIT: Since someone asked, essentially I have some code that creates a character as follows:

Node Root: name "root" polygon = a rectangle for the torso graphNode* parent = NULL vector of 5 children [Head, Arm1, Arm2, Leg1, Leg2]

Node Head: name "head" polygon = a circle for the head graphNode* parent = root an empty children vector

Node Arm1 name "arm1" polygon = a rectangle for the upper arm graphNode* parent = root vector of 1 child [Forearm1]

Node Arm2 name "arm2" polygon = a rectangle for the upper arm graphNode* parent = root vector of 1 child [Forearm2]

Node Leg1 name "leg1" polygon = a rectangle for the thigh graphNode* parent = root vector of 1 child [Loweleg1]

Node Leg2 name "leg2" polygon = a rectangle for the thigh graphNode* parent = root vector of 1 child [Loweleg2]

Node Forearm1 name "forearm1" polygon = a rectangle for the forearm graphNode* parent = Arm1 an empty children vector

Node Forearm2 name "forearm2" polygon = a rectangle for the forearm graphNode* parent = Arm2 an empty children vector

Node Lowerleg1 name "lowerleg1" polygon = a rectangle for the calf graphNode* parent = Leg1 an empty children vector

Node Lowerleg2 name "lowerleg2" polygon = a rectangle for the calf graphNode* parent = Leg2 an empty children vector

  • `std::bad_alloc` means the program ran out of memory. It may occur during pushing because if the vector size grows beyond its current capacity, the vector has to allocate a bigger buffer – Brian Bi Feb 23 '14 at 04:57
  • How does `root` come into this? And you might fire this through Valgrind just to see what bubbles up. – WhozCraig Feb 23 '14 at 05:01
  • root is the base of the tree. Essentially I'm drawing a really basic character as follows: root node with a rectangle polygon for the torso and a vector of 5 graphNode* children: 2 arm nodes with rectangle myPolygons 2 leg nodes with rectangle myPolygons a head node with a circle myPolygon each arm node has a forearm node as a child each leg node has a lower leg node as a child – IAmTheClayman Feb 23 '14 at 05:26
  • Run it in a debugger and set a catchpoint on throw. – Frank Osterfeld Feb 23 '14 at 10:03
  • 1
    I'd suspect a bug in adding to *allNodes*. See how it grows. I'd verify it doesn't have some infinite loop/recursion, or adding itself to itself. – hyde Feb 23 '14 at 10:23
  • It's almost certainly a bug in your code, as @hyde suggested. If you post the code that copies the children of the root node into allNodes, we can probably tell you what's wrong. – Julian Feb 23 '14 at 11:57

0 Answers0