stephane / libmodbus

A Modbus library for Linux, Mac OS, FreeBSD and Windows
http://libmodbus.org
GNU Lesser General Public License v2.1
3.33k stars 1.71k forks source link

Memory Leak in _modbus_tcp_flush. #334

Open preetamj84 opened 8 years ago

preetamj84 commented 8 years ago

libmodbus version

all

Operating system

Windows x64

Description of the Modbus network (server, client, links, etc)

client

Expected behavior

Memory Leak should not Happen

Actual behavior

When we use _modbus_tcp_flush function, memory leak is observed. Bytes do not get freed as expected.Output that appears is "" Invalid TID received.. 0 bytes freed. ""

Steps to reproduce the behavior (commands or source code)

Scenario1 : connected a target modbus device with modbus tcp/ip gateway.Gateway device goes out of network while field device is ON. Scenario2: target device goes off, while gateway is ON. Scenario3: continuous timeout due to say wrong slave id or correct slave id but target device is off.

libmodbus output with debug mode enabled

Invalid TID received.. 0 bytes freed. **

stephane commented 8 years ago

In your report, you have indicated all modbus versions but this message (Invalid TID received) doesn't exist in master branch. Which version do you use?

BTW a message with '0 bytes freed' doesn't mean there is a leak in the program.

preetamj84 commented 8 years ago

Lib Modbus Version is 3.0.6

And the statement appears in the following code in modbus_tcp.c int _modbus_tcp_pre_check_confirmation(modbus_t _ctx, const uint8_t req, const uint8_t rsp, int rsplength) { / Check TID */ if (req[0] != rsp[0] || req[1] != rsp[1]) { if (ctx->debug) { fprintf(stderr, "Invalid TID received 0x%X (not 0x%X)\n", (rsp[0] << 8) + rsp[1], (req[0] << 8) + req[1]); } errno = INVALID_DATA; return -1; } else { return 0; } }

This routine is called from the function check_confirmation in modbus.c

To explain correctly when it says Invalid TID received followed by statement, 0 bytes freed,at that point of time, In Task manager, the memory shows as increased after this point. Secondly, when the statement gets printed as Invalid TID Received, say 9 bytes freed, at that time, memory does not increase.

stephane commented 7 years ago

Before further investigation, do you have the same behaviour with libmodbus v3.1.4 (or master) ?

studioraga commented 7 years ago

I am seeing the same error while using ./unit-test-client tcp

Output that appears is "" Invalid TID received.

This is seen with stable version 3.0.6 and master v3.1.4) ...