Skip to content
This repository was archived by the owner on Oct 23, 2022. It is now read-only.

Commit ed67f7f

Browse files
author
Sharkbyteprojects
committed
REMOVE MEMORY LEAK
1 parent 92ee2cb commit ed67f7f

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM alpine:3.14
2+
3+
WORKDIR /app
4+
5+
RUN apk add --no-cache cmake build-base make
6+
7+
COPY libuv libuv
8+
COPY libUvTest libUvTest
9+
COPY CMakeLists.txt .
10+
11+
RUN mkdir /server
12+
RUN cmake -S . -B /server -G "Unix Makefiles"
13+
RUN cmake --build /server
14+
15+
WORKDIR /server/libUvTest
16+
17+
RUN rm -rf /app
18+
RUN apk del cmake build-base make
19+
20+
EXPOSE 8000
21+
22+
CMD ["./libUvTest"]

libUvTest/libUvTest.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ static void on_close(uv_handle_t* handle) {
1515

1616
static void after_write(uv_write_t* req, int status) {
1717
write_req_t* wr = (write_req_t*)req;
18-
1918
if (wr->buf.base != NULL)
2019
free(wr->buf.base);
2120
free(wr);
@@ -57,7 +56,8 @@ static void after_read(uv_stream_t* handle,
5756

5857
if (nread < 0) {
5958
/*assert(nread == UV_EOF);*/
60-
fprintf(stderr, "err: %s\n", uv_strerror(nread));
59+
if (nread != UV_EOF)
60+
fprintf(stderr, "err: %s\n", uv_strerror(nread));
6161

6262
req = (uv_shutdown_t*)malloc(sizeof(*req));
6363
assert(req != NULL);
@@ -79,6 +79,7 @@ static void after_read(uv_stream_t* handle,
7979
send[nread - 1 - s] = buf->base[s];
8080
}
8181
send[nread] = 0;
82+
free(buf->base);
8283
}
8384
//END OF REVERSE
8485

tcpTest.node.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const NumberOfSockets = 2000;
2+
// Include Nodejs' net module.
3+
const { Socket } = require('dgram');
4+
const Net = require('net');
5+
// The port number and hostname of the server.
6+
const port = 8000;
7+
const host = 'localhost';
8+
9+
for(let x = 0;x < NumberOfSockets;x++){
10+
// Create a new TCP client.
11+
const client = new Net.Socket();
12+
// Send a connection request to the server.
13+
client.connect({ port: port, host: host }, () => {
14+
// If there is no error, the server has accepted the request and created a new
15+
// socket dedicated to us.
16+
console.log('TCP connection established with the server.');
17+
18+
// The client can now send data to the server by writing to its socket.
19+
let ax = 0;
20+
let i = setInterval(()=>{
21+
if(ax % 2 == 0)
22+
client.write('Hello, server');
23+
else
24+
client.write(`|Random Number: ${Math.round(Math.random() * 100)}|`.split("").reverse().join(""));
25+
// Request an end to the connection after the data has been received.
26+
if(++ax > 20) {
27+
clearInterval(i);
28+
client.end();
29+
}
30+
}, 100);
31+
});
32+
33+
// The client can also receive data from the server by reading from its socket.
34+
client.on('data', function(chunk) {
35+
console.log(`Data received from the server: "${chunk.toString()}"`);
36+
37+
38+
});
39+
40+
client.on('end', function() {
41+
console.log('Requested an end to the TCP connection');
42+
});
43+
}

0 commit comments

Comments
 (0)