-
Notifications
You must be signed in to change notification settings - Fork 128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DOC] Singletons section #1540
Merged
Merged
[DOC] Singletons section #1540
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
89006bf
singleton section added
pczekaj99 b5e4262
singletons documentation section added
pczekaj99 d687266
Merge branch 'master' into singleton_doc
pczekaj99 f12b2fe
singletons.md added to mkdocs.yml
pczekaj99 356491a
warning fixed
pczekaj99 638f0ff
formating fix
pczekaj99 35ceba8
singletons description addded
pczekaj99 d7d22af
Merge branch 'master' into singleton_doc
pczekaj99 e58eeab
[doc] description of singletons added
pczekaj99 eaf3ae7
Update singletons.md
MFransen69 fc1f285
Merge branch 'master' into singleton_doc
MFransen69 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
## Using and disposing singletons | ||
|
||
When creating and using singletons, there are a few things to bear in mind. If they are handled incorrectly, problems with memory, the creation of overmapped copies of them and also the incorrect order of their destruction can occur. | ||
|
||
Singletons that use sockets should connect to them before they are registered in the singletons list. This is therefore best done in their constructor. If the connection occurs too late, the singleton may not be cleaned up before the Resource Monitor destructor is called. | ||
Here is an example of making sure a socket connection is opened in the OpenCDMAccessor singleton constructor. | ||
```cpp | ||
OpenCDMAccessor(const TCHAR domainName[]) | ||
: _refCount(1) | ||
, _domain(domainName) | ||
, _engine(Core::ProxyType<RPC::InvokeServerType<1, 0, 4>>::Create()) | ||
, _client() | ||
, _remote(nullptr) | ||
, _adminLock() | ||
, _signal(false, true) | ||
, _interested(0) | ||
, _sessionKeys() | ||
{ | ||
TRACE_L1("Trying to open an OCDM connection @ %s\n", domainName); | ||
Reconnect(); // make sure ResourceMonitor singleton is created before OpenCDMAccessor so the destruction order is correct | ||
} | ||
``` | ||
|
||
When a singleton uses itself (via the instance method) or any other singleton during destruction be very careful to make sure it does not create a new singleton by accident. | ||
|
||
A client library dispose method should not call `Core::Singleton::Dispose()` but only clean up its own internal singletons. | ||
!!!warning | ||
`Core::Singleton::Dispose()` dispose of all singletons in the singletons list. | ||
|
||
Calling this method on a particular singleton will dispose of them all. Instead, we should use `Core::SingletonType<>::Dispose()` with the appropriate type of singleton we want to dispose of specified, or write our own `Dispose()` which will not dispose of all remaining singletons. | ||
|
||
Below is an example of how you SHOULD NOT dispose of a singleton! | ||
```cpp | ||
void opencdm_dispose() { | ||
Core::Singleton::Dispose(); | ||
} | ||
``` | ||
And here an example of the RIGHT way to dispose of a singleton. | ||
```cpp | ||
void opencdm_dispose() { | ||
Core::SingletonType<OpenCDMAccessor>::Dispose(); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think it would be good to (very briefly) also describe the use of singletons in Thunder (what they are) and as section on the code in Core we have to help write singletons (e.g. SingletonType, the role of Core::Singleton::Dispose() etc.) and perhaps some small code example. If you have question on this I think Sebastion will be able to provide some details)