Usage of createElement()

Hi,

I’m a bit confused about how to use the createElement() - methods in the DOM.

Please correct me, if I’m wrong, but I thought, you can create an element via this method, manipulate it, and then place it somewhere else (maybe even in another document).

Anyhow, this code won’t work:

...
domShader_GLSL* newShaderGLSL = daeSafeCast<domShader_GLSL >( intoPass_GLSL->createElement(COLLADA_ELEMENT_SHADER) );

if (!newShaderGLSL->isAttributeSet("stage"))
...

This code crashes in the isAttributeSet()-check … it seems like some members of the newly created daeElement are not correctly initialized and assertion fails while accessing _validAttributeArray[i]; in the method daeElement::isAttributeSet( daeString attrName ).

If I create the element with createAndPlace I don’t get those troubles.

Is the createElement() element intended to be used like this at all?

That is highly odd. Both daeElement::createElement(daeString) and daeElement::createAndPlace(daeString) start off with this line of code

daeElementRef elem = _meta->create(className);

Then the only difference is createAndPlace calls the placeElement function before returning and create just returns elem.

Are you sure that newShaderGLSL is not NULL.
OHHHHHHHHH I figured it out. Its ref counting. you are storing the return value from createElement (a daeElementRef) as a *. When you createElement your ref count is 1. But since you don’t keep the ref it gets deleted at the end of that line of code. But createAndPlace will place the element which increased the ref count to 2 or more (there may be another ref in the daeDocument or _contents array).

That is the problem. Try this code instead

daeSmartRef<domShader_GLSL> newShaderGLSL = daeSafeCast<domShader_GLSL >( intoPass_GLSL->createElement(COLLADA_ELEMENT_SHADER) );

if (!newShaderGLSL->isAttributeSet("stage")) 

-Andy

that was it, thanks a lot!