The memory doesn't seem to be freed when deleting an object allocated in the heap. What am I missing here? #1846
Replies: 1 comment 1 reply
-
You need to free in .message. AddressSanitizer would have told you
Den ons 12 feb. 2025 21:24duster ***@***.***> skrev:
… Hello. Whenever a new client connects, a new std::string instance in heap
inside the '.message' callback for each client is created and set with a
value sent by the client (the 'message' object). I can see the memory (RSS
tab) keeps increasing with new each client connection (For example 9000
something bytes, then 9004 something bytes, then 9008 something bytes and
so on for each two or three new clients), but it's not decreasing even when
I am deleting the object in the '.close' callback when each of those
clients exists properly clientSocket.close() and the '.close' callback is
fired on the server.
I am using the following commandline to watch the memory usage in real
time in Linux Debian.
I must be doing something very wrong. Many thanks. I am noob in using this
library.
watch -n0.2 'pmap -x | tail -n1'
This is the user data structure:
struct PerSocketData {
std::string* someString {nullptr};
};
.message = [&](uWS::WebSocket<false, true, PerSocketData> *ws, std::string_view message, uWS::OpCode opCode) {
PerSocketData* ud = ws->getUserData();
ud->someString = new std::string(message);
}
In the '.close' callback I am deleting the object as following:
.close = [](uWS::WebSocket<false, true, PerSocketData> *ws, int /*code*/, std::string_view message) {
PerSocketData* ud = ws->getUserData();
ud->someString->clear(); //looks dumb but since the delete wasn't working hence I tried this as well
ud->someString->shrink_to_fit(); //looks dumb but since the delete wasn't working hence I tried this as well
delete ud->someString;
ud->someString = nullptr;
}
—
Reply to this email directly, view it on GitHub
<#1846>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/A2NMOMPLZKF3UMOSBFVCBH32POUZTAVCNFSM6AAAAABXAQNTSSVHI2DSMVQWIX3LMV43ERDJONRXK43TNFXW4OZXHE2TQMRWGY>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
starduxt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello. Whenever a new client connects, a new std::string instance in heap inside the '.message' callback for each client is created and set with a value sent by the client (the 'message' object). I can see the memory (RSS tab) keeps increasing with new each client connection (For example 9000 something bytes, then 9004 something bytes, then 9008 something bytes and so on for each two or three new clients), but it's not decreasing even when I am deleting the object in the '.close' callback when each of those clients exits properly clientSocket.close() in the client side and the '.close' callback is fired on the server. For testing I am sending 1024 bytes from each client.
I am using the following commandline to watch the memory usage in real time in Linux Debian.
I must be doing something very wrong. Many thanks. I am noob in using this library.
This is the user data structure:
In the '.close' callback I am deleting the object as following:
Beta Was this translation helpful? Give feedback.
All reactions