Skip to content

fix WSclient_t initialization in server #581

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

Merged
merged 4 commits into from
Nov 26, 2020

Conversation

d-a-v
Copy link
Contributor

@d-a-v d-a-v commented Nov 22, 2020

Reference: discussion with @mcspr

(opened to discussion or any other solution)

@d-a-v
Copy link
Contributor Author

d-a-v commented Nov 22, 2020

If the N WSclient_t instances from the array do not need to be re-initialized, then in-place destructor and constructor are not needed. WSclient_t members are always initialized to their default values (0 for scalar, respective contructor for other members including the Strings - this initialization was later destroyed by the former memset()).
Then a WSclient_t::init(num, interval/count...) is sufficient, and would come after the constructors are called (which happens at the time of the array allocation).

@mcspr
Copy link

mcspr commented Nov 22, 2020

Then a WSclient_t::init(num, interval/count...) is sufficient, and would come after the constructors are called (which happens at the time of the array allocation).

Basically, comes down to this.

Note that classname() = default; does not always guarantee that it is called :) e.g.

Object obj;

vs.

Object obj = Object();

Which is exactly the issue the default values in the class body always help to avoid (and I really like that option tbh). And not to mention different ctors, which need to manually call the default one.

Just to repeat myself - explicit dtor + ctor through placement new could be replaced with either copy-assignment or move-assignment, depending on what we want with the resulting objects.
e.g. like to preserve existing buffers of Strings, which what is currently happening after String="", since it just sets length to 0 and leaves the existing buffer.
Might also be less scary to look at.

@mcspr
Copy link

mcspr commented Nov 22, 2020

Small code example re. "And not to mention different ctors, which need to manually call the default one."
https://godbolt.org/z/rrY9jP vs. https://godbolt.org/z/GqbdhT (adding default body)

@d-a-v
Copy link
Contributor Author

d-a-v commented Nov 22, 2020

edited2

Well OK, thanks!
I just proved myself through another example that scalars are not initialized by default constructor in a placement new
but members with constructors are (so scalars have no implicit constructors).

So basically a new is a calloc() followed by (calling constructors) = (in-place new).
Calling an in-place constructor will not call calloc again.
I was wrongly thinking new was a malloc, then (memset + calling constructors) = (in-place new).

@d-a-v
Copy link
Contributor Author

d-a-v commented Nov 22, 2020

@mcspr Do you agree with the last commit ?

(I wouldn't mind if you came with your own PR/solution - the memset() needs to be fixed)

@mcspr
Copy link

mcspr commented Nov 22, 2020

(ref. gitter + 9d9f892)
I think this covers it.

init() alternative is also doing it like https://godbolt.org/z/zaTjvT
ping intervals are dynamic though, but it seems to be handled via enableHeartbeat

@d-a-v d-a-v changed the title use native contructor and destructor to initialize WSclient_t fix WSclient_t initialization in server Nov 22, 2020
@d-a-v
Copy link
Contributor Author

d-a-v commented Nov 22, 2020

@Links2004 This PR is ready for reviewing, sorry for the noise ;)

@Links2004
Copy link
Owner

no worry, the git commends are for discussing.
and I am happy to learn new stuff and getting the lib better in the process.

@Links2004 Links2004 merged commit ebb87cd into Links2004:master Nov 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants