question

winwatar avatar image
winwatar asked

Unity SDK : Using statements - AGSSyncable : IDisposable

Hi, junior game dev here. I am just about to start implementing Whispersync for a newly released game. I have a question regarding the management and best practices regarding the consumption of AGSGameDataMap and anything that is a subclass of the AGSSyncable class, as it will influence the implementation/refactoring of the wrapper classes we use for our game data. The base class, AGSSyncable implements the IDisposeable interface and the only code example I can find online uses this pattern if(AGSClient.IsServiceReady()) {     using(AGSGameDataMap dataMap = AGSWhispersyncClient.GetGameData()){         using(AGSSyncableNumber myHighNumber = dataMap.GetHighestNumber("myHighNumber")){             myHighNumber.Set(1000);         }     } } I looked at the examples too, and saw code like this: using(AGSSyncableNumber syncableNumber = GetSyncableNumber(dataMap)) { if(null != syncableNumber) { GetSyncableValue(syncableNumber); } } I am asking is if it really is necessary to instantiate a new reference to the value every time it is accessed? I am guessing that if your reference goes out of scope the java instance on the android side will never be garbage collected? Is it dangerous to not do it this way? Do the javaobjects hold/lock some resource that must be disposed, like a file handle? Will it cause memory leaks? Or is it managed by some manager? There are few ways I could go about the wrapper but two pop into my head: 1. In the c# properties, make the setters and getters of my data classes use, the "using pattern" above. 2. Implement the IDisposable interface in my data classes, create explicit destructors for the classes and call Dispose in them. (So they are run when the instance gets garbage collected.) Option 1 would probably be safer but also possibly a resource hog... marshalling isn't that fast. Option 2 could lead to more severe memory leaks in case of crashes... Thanks in advance for any help, it is greatly appreciated!
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

DougM@Amazon avatar image
DougM@Amazon answered
To answer your question about whether it is required to instantiate a new reference to the value each time it is accessed: You can keep the same reference to the value; behind the scenes we use a "get or create pattern": if the value hasn't been requested before by name, then we create an object and return a reference to that, if it has been requested before, then we just return a reference to that. We should probably update the example to reflect this. As for your second question about the DataMap being in memory or being garbage collected: The entire map should be in memory the whole time ,especially if you keep a reference to the global game data map.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.