Clean up memory in examples.
This commit is contained in:
@@ -76,6 +76,8 @@ main()
|
|||||||
// ...do more stuff...
|
// ...do more stuff...
|
||||||
}
|
}
|
||||||
...
|
...
|
||||||
|
delete ws; // alternatively, use unique_ptr<> if you have C++11
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -5,20 +5,23 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
using easywsclient::WebSocket;
|
using easywsclient::WebSocket;
|
||||||
WebSocket::pointer ws = WebSocket::from_url("ws://localhost:8126/foo");
|
std::unique_ptr<WebSocket> ws(WebSocket::from_url("ws://localhost:8126/foo"));
|
||||||
assert(ws);
|
assert(ws);
|
||||||
ws->send("goodbye");
|
ws->send("goodbye");
|
||||||
ws->send("hello");
|
ws->send("hello");
|
||||||
while (ws->getReadyState() != WebSocket::CLOSED) {
|
while (ws->getReadyState() != WebSocket::CLOSED) {
|
||||||
|
WebSocket::pointer wsp = &*ws; // <-- because a unique_ptr cannot be copied into a lambda
|
||||||
ws->poll();
|
ws->poll();
|
||||||
ws->dispatch([ws](const std::string & message) {
|
ws->dispatch([wsp](const std::string & message) {
|
||||||
printf(">>> %s\n", message.c_str());
|
printf(">>> %s\n", message.c_str());
|
||||||
if (message == "world") { ws->close(); }
|
if (message == "world") { wsp->close(); }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
// N.B. - unique_ptr will free the WebSocket instance upon return:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,5 +23,6 @@ int main()
|
|||||||
ws->poll();
|
ws->poll();
|
||||||
ws->dispatch(handle_message);
|
ws->dispatch(handle_message);
|
||||||
}
|
}
|
||||||
|
delete ws;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user