Problem creating nested group via the C api?


#1

I am trying to create a sub-group in a group and have tried the following but it crashes on SUModelRelease(&model); Am I doing something wrong?

//Initialise model
SUInitialize();
SUModelRef model = SU_INVALID;
SUResult res = SUModelCreate(&model);

//Get model entities
SUEntitiesRef entities = SU_INVALID;
SUModelGetEntities(model, &entities);

//Main group
SUGroupRef groupRef = SU_INVALID;
SUGroupCreate(&groupRef);
SUGroupSetName(groupRef, "Test");

//Get Main group entities
SUEntitiesRef groupEntities = SU_INVALID;
SUGroupGetEntities(groupRef, &groupEntities);

//Sub group
SUGroupRef groupRef2 = SU_INVALID;
SUGroupCreate(&groupRef2);
SUGroupSetName(groupRef2, "Test2");

//Get Sub group entities
SUEntitiesRef groupEntities2 = SU_INVALID;
SUGroupGetEntities(groupRef2, &groupEntities2);

//Add sub group to main group entities
SUEntitiesAddGroup(groupEntities, groupRef2);

//Add main group to model entities
SUEntitiesAddGroup(entities, groupRef);

//Save and cleanup
SUModelSaveToFile(model, Utilities::ToString(filename));
SUModelRelease(&model); //Crash with Attempt to read or write protected memory.
SUTerminate();

#2

Ref in the Meta category: How to properly quote code in the forums


#3

@DanRathbun Noted thank you.


#4

Hi there. I didn’t try to run your code but order of operations will matter in this case. Try adding the group to its parent right after creating it and before populating the group itself. Let me know if I’m not being clear and I’ll add some code.
Thanks!


#5

Thanks for your reply @bugra.
With your suggestion I have got it working by immediately adding the main group to the model entities, and the same with the Sub group. Final code for the group part:

//Main group
SUGroupRef groupRef = SU_INVALID;
SUGroupCreate(&groupRef);

//Add main group to model entities
SUEntitiesAddGroup(entities, groupRef);

//Sub group
SUGroupRef groupRef2 = SU_INVALID;
SUGroupCreate(&groupRef2);

//Get Main group entities
SUEntitiesRef groupEntities = SU_INVALID;
SUGroupGetEntities(groupRef, &groupEntities);

//Add sub group to main group entities
SUEntitiesAddGroup(groupEntities, groupRef2);


//Get Sub group entities
SUEntitiesRef groupEntities2 = SU_INVALID;
SUGroupGetEntities(groupRef2, &groupEntities2);


SUGroupSetName(groupRef2, "Test2");

SUGroupSetName(groupRef, "Test");

#6

I’m glad it worked. This is just a limitation because of how the code works internally and we know we need to fix it.

Btw, you can set the group names right after creating them, they shouldn’t have to be at the end.

Thanks!
Bugra


#7

Thanks Bugra for the clarification.
This should be made more clear in the docs. I just spent hours trying to understand where the crash came from.

For now it just says “The created group must be subsequently added to the Entities of a model, component definition or a group.”
The word “immediately” should be in there somewhere.