PCAN Basic API Windows Handle Leak

The free CAN Software API (Application Programming Interface) for Windows®
Locked
hglee
Posts: 4
Joined: Mon 22. Dec 2025, 02:31

PCAN Basic API Windows Handle Leak

Post by hglee » Mon 22. Dec 2025, 09:22

I am posting this issue because I have a problem while using the PCAN Basic API.

Even in this simple example, the program's opened handles keep increasing.

This will be a problem in programs that run for long time.

This is my enviroment and example source.
OS: Windows 11 25H2
Device: PCAN-USB
Driver: 5.0.0.2
PCAN Basic API: 5.0.0.1115

Code: Select all

int main()
{
	while (1)
	{
		TPCANStatus status = CAN_Initialize(PCAN_USBBUS1, PCAN_BAUD_500K, 0, 0, 0);
		if (status != PCAN_ERROR_OK)
		{
			printf("Error on CAN_Initialize: 0x%X", status);

			exit(1);
		}

		status = CAN_Uninitialize(PCAN_USBBUS1);
		if (status != PCAN_ERROR_OK)
		{
			printf("Error on CAN_Uninitialize: 0x%X", status);

			exit(2);
		}

		// According to manual, need about 500ms to uninitialize TX queue
		Sleep(1000);
	}

	return (0);
}
In WinDbg, handles for the Token keep increasing like this:

Code: Select all

246 Handles
Type           	Count
None           	7
Event          	17
Section        	1
File           	7
Directory      	2
Mutant         	3
Semaphore      	2
Key            	9
Token          	180
Thread         	2
IoCompletion   	3
TpWorkerFactory	3
ALPC Port      	2
WaitCompletionPacket	8
Would you fix this issue?

Regards,

G.Lang
Support
Support
Posts: 192
Joined: Wed 22. Sep 2010, 14:58

Re: PCAN Basic API Windows Handle Leak

Post by G.Lang » Thu 25. Dec 2025, 12:04

Hello,

as a simple test please check if the same error also appears with the PCANBasic API version 4.x.

hglee
Posts: 4
Joined: Mon 22. Dec 2025, 02:31

Re: PCAN Basic API Windows Handle Leak

Post by hglee » Mon 29. Dec 2025, 00:01

Hi,

I uninstalled the v5 PEAK-Drivers and removed v5 PCAN Basic API.

And I tested with v4 PEAK-Drivers and v4 PCAN Basic API.

As a result, v4 has same handle leak issue.

This is my enviroment for v4.
OS: Windows 11 25H2
Device: PCAN-USB
Driver: v4.6.4.16846
PCAN Basic API: v4.10.1.968
In WinDbg,

Code: Select all

240 Handles
Type           	Count
None           	7
Event          	18
Section        	1
File           	7
Directory      	2
Mutant         	3
Semaphore      	2
Key            	9
Token          	172
Thread         	3
IoCompletion   	3
TpWorkerFactory	3
ALPC Port      	2
WaitCompletionPacket	8
Regards,

hglee
Posts: 4
Joined: Mon 22. Dec 2025, 02:31

Re: PCAN Basic API Windows Handle Leak

Post by hglee » Mon 29. Dec 2025, 01:05

Hi,

I checked PCAN Basic API v4, v5 DLLs with the Ghidra.

I found code that did not close handle.

For v4, in the FUN_18000bd60 has problem and in the incoming call tree, it has reference from CAN_Initialize, CAN_Uninitialize and many functions.

PCAN Basic v4 decompiled
PCAN Basic v4 decompiled
v4_decompile.PNG (37.87 KiB) Viewed 1451 times

For v5, in the FUN_18000e160 has problem and the function has same contents to the above function.

PCAN Basic v5 decompiled
PCAN Basic v5 decompiled
v5_decompile.PNG (42.27 KiB) Viewed 1451 times

Regards,

K.Wagner
Software Development
Software Development
Posts: 1084
Joined: Wed 22. Sep 2010, 13:36

Re: PCAN Basic API Windows Handle Leak

Post by K.Wagner » Wed 7. Jan 2026, 10:43

Hello,

as I understood, the problem is the same no matter wich DLL you are using. If the problems were only in the v5, this could be an indication of problem in refactoring or new implemented code. The v4 was well tested again memory leaks.

Regarding your test:

Code: Select all

int main()
{
	while (1)
	{
		TPCANStatus status = CAN_Initialize(PCAN_USBBUS1, PCAN_BAUD_500K, 0, 0, 0);
		if (status != PCAN_ERROR_OK)
		{
			printf("Error on CAN_Initialize: 0x%X", status);

			exit(1);
		}

		status = CAN_Uninitialize(PCAN_USBBUS1);
		if (status != PCAN_ERROR_OK)
		{
			printf("Error on CAN_Uninitialize: 0x%X", status);

			exit(2);
		}

		// According to manual, need about 500ms to uninitialize TX queue
		Sleep(1000);
	}

	return (0);
}
Please note:
you are having a Sleep(1000) in your loop. The 500ms needed in the uninitialize-function "are handled" in there - you do not need (and should not) wait by your own. Doing this, you are preventing the API to spend the 500 milliseconds directly but after your delay, so that the function is actually waiting 1500 ms before continuing.


Please remove the delay from your test code and repeat your tests. We will check nevertheless the API for memory leaks.
Best regards,
Keneth

hglee
Posts: 4
Joined: Mon 22. Dec 2025, 02:31

Re: PCAN Basic API Windows Handle Leak

Post by hglee » Wed 7. Jan 2026, 11:46

Hi,

The problem is actually a Windows handle leak (type of Token), not a memory leak.

As you can see above the captured images, decompiled by the Ghidra for the PCAN Basic API DLLs, the handle obtained from OpenProcessToken() did not closed in there.

I retried the test as you metioned with PCAN Basic API v4. But the result was same as before.

You can see the total number of handles in the task manager keep increasing. Or you can see the used handle types in the WinDbg using !handle command.

This is my enviroment and modified source.
OS: Windows 11 25H2
Device: PCAN-USB
Driver: v4.6.4.16846
PCAN Basic API: v4.10.1.968

Code: Select all

int main()
{
	while (1)
	{
		TPCANStatus status = CAN_Initialize(PCAN_USBBUS1, PCAN_BAUD_500K);
		if (status != PCAN_ERROR_OK)
		{
			printf("Error on CAN_Initialize: 0x%X", status);

			exit(1);
		}

		status = CAN_Uninitialize(PCAN_USBBUS1);
		if (status != PCAN_ERROR_OK)
		{
			printf("Error on CAN_Uninitialize: 0x%X", status);

			exit(2);
		}
	}

	return (0);
}
20260107_task_manager.gif
20260107_task_manager.gif (982.27 KiB) Viewed 1291 times
20260107_windbg.png
20260107_windbg.png (4.93 KiB) Viewed 1291 times
Regards,

K.Wagner
Software Development
Software Development
Posts: 1084
Joined: Wed 22. Sep 2010, 13:36

Re: PCAN Basic API Windows Handle Leak

Post by K.Wagner » Wed 7. Jan 2026, 13:35

Hello,

now I see. Yes, this seems to be a (very old) bug. Indeed a CloseHandle is failing. This will be fixed by the next release.

Thank you for bringing this to our attention.
Best regards,
Keneth

Locked