Regular Contributor
Posts: 58
Accepted Solution

Entity pointer after rollback



Here is yet another API_BEGIN/END question:


I know I can conditionally revert unwanted model changes with any of the following ways:


1. API_BEGIN/API_END with bb_immediate_close



So the model will be in the original state after I rollbacked the changes. The question is: what happens with pointers to participating entities?




BODY *b1 = ..., *b2 = ...;  // assigned some bodies


option_header *immed_close = find_option("bb_immediate_close");
immed_close->push(TRUE);  // make sure that rollback happens immediately



  result = api_unite( otherBody, baseBody, pOpts );


  ... here I somehow decided that I don't like the union, so I want to revert everything...


  result = outcome(API_FAILED);  // To rollback the union changes







It seems I still can use the b1, b2 pointers after that, so the original bodies are still located at the same memory as before. I checked it, ran tests on differemt platforms - this statement looks correct. But I'm still not sure. So my questions are:


1. Is it always true that pointers b1, b2 in the example above will remain valid pointers to the original bodies?

2. If it is not always true (so b1, b2 can become stale pointers), how can I get new valid pointers to the bodies? I know I can use SPA_COLLECTION to track the pointers' validity, but how can I find new pointers when I know old pointers are not valid anymore?


Thank you!


Posts: 22

Re: Entity pointer after rollback

1. No, it's not true that the bodies will always remain valid, or even restored to their previous state. It depends on what code you write in the "here I somehow decided" part of the code.


That's one of the reasons why Spatial's solution is bad and hard to maintain.


There are MUCH better ways to deal with transaction (commit/rollback) semantics in C++ including strong exception-safety guarantee and thread-safety. Also, there should not be any need to write all this boilerplate code or any macros at all for such a simple task. But so far Spatial has been reluctant to improve their API, which is messy and full of traps.


2. I have no idea, but this shouldn't even be necessary if the rollback was always guaranteed.


In our company we had to isolate these macros into a few reusable algorithms where we could implement our own workarounds. Contact me in private if you want me to send you these functions.

Regular Contributor
Posts: 30

Re: Entity pointer after rollback

If you are always reverting after the API blocks, the pointers b1, b2 would continue to remain valid.