On 10/11/2014 at 15:34, xxxxxxxx wrote:
Hi NiklasR,
Thanks for the clarification. I made it sound stricter because in the specific case given, reordering the code eliminated the crash, and it seemed that inserting the object meant direct modifications could not be done past that point.
As for what ScottA states in his replies:
Please consider there is a lot of false information on the Internet, and some of it may be technically true but poorly explained, leading to misunderstandings.
We have to solve some communication issues here that are making it hard to help dispel some misconceptions. I figured out you're using unconventional terminology for related software development and 3D editor concepts.
For instance, I believe you mean some of the data that represents the 3D elements must be properly created and setup before adding them to the 3D world in Cinema 4D. The 3D world being the data you visualize and manipulate in C4D, as viewed in its viewports, rendered, etc., so all the polygonal shapes, fog effects, etc.
So you state 'in memory' when you mean 'outside of the 3D world', and when you state 'physically create', you mean 'add to the 3D world'. Up to this point, I'd believe we only have a terminology issue, but the concepts are generally correct.
Conceptual issues start with the fact that nothing is 'physically written' when adding objects to the 3D world, rather you are associating your data to the 3D world (doc->InsertObject()), all in the computer's volatile memory space (virtual memory is not a factor at this conceptual level). When you explicitly write your data to a file to commit it to non-volatile memory, you can argue you created a 'physically written' form of your data, as when you turn your computer off, it's still 'represented on a physical area' on your hard disk.
In terms of opening / closing files, you should open the file for as long as you need to access it (read and/or write), and then close it once you know you're done, for efficiency purposes. However, opening / closing a file repeatedly shouldn't crash unless you're not releasing the Windows file handle each time. That will cause a file handle resource leak pile up, eventually causing a crash if the first indication of resource exhaustion is not handled by your code. You could instead leave a file open and write data to it in chunks, closing it at the end, rather than build up the file contents in RAM and then write all in one shot.
More info from Microsoft:
Mark's Blog
Therefore, please consider the concepts relating to resources managed by the OS, in addition to the rules about threading in Cinema 4D, instead of thinking it's always about timing: defective code does not become correct when slowed down or sped up, it just may lead to correct results because the defect is masked by randomly avoiding the conditions that reveal it. You still risk exposing the defect(s) when conditions change (new code, performance variations, etc.). Correct code, on the other hand, should work properly at any speed.
I hope that helps!
Joey Gaspe
SDK Support Engineer