bytedeco/javacpp
View on GitHubThe same function pointer set more than 10 times will always pass in a NULL pointer to C/C++
Open
#611 opened on Sep 29, 2022
enhancementhelp wantedquestion
Repository metrics
- Stars
- (4,279 stars)
- PR merge metrics
- (No merged PRs in 30d)
Description
Hi @saudet ,
As the subject mentioned, I suspect this is a bug. In C, the code is like:
#include <iostream>
#include "RpcImpl.h"
using namespace std;
namespace client {
rpc_handler_t handler;
void ClientSetMessageHandler(rpc_handler_t userHandler) {
RPC_INFO("ClientSetMessageHandler userHandler:%p", userHandler);
handler = userHandler;
}
}
In Java/Kotlin we got:
@RunWith(AndroidJUnit4::class)
class TestShotSync {
@Test
fun testFunctionPointer() {
for (i in 1..20) {
val handler = ShotSync.rpc_handler_t()
println("ClientSetMessageHandler No. $i $handler")
ShotSync.ClientSetMessageHandler(handler)
}
}
}
And after we run the code, the output is like:
2022-09-29 10:03:55.010 9271-9294 I/System.out: ClientSetMessageHandler No. 1 com.senter.shotsync.ShotSync$rpc_handler_t[address=0xe4433170,position=0,limit=1,capacity=1,deallocator=org.bytedeco.javacpp.Pointer$NativeDeallocator[ownerAddress=0xe4433170,deallocatorAddress=0xced1f8d1]]
2022-09-29 10:03:55.011 9271-9294 I/[STC-RPC]: ClientSetMessageHandler userHandler:0xced1ecdd
2022-09-29 10:03:55.011 9271-9294 I/System.out: ClientSetMessageHandler No. 2 com.senter.shotsync.ShotSync$rpc_handler_t[address=0xe4433180,position=0,limit=1,capacity=1,deallocator=org.bytedeco.javacpp.Pointer$NativeDeallocator[ownerAddress=0xe4433180,deallocatorAddress=0xced1f8d1]]
2022-09-29 10:03:55.011 9271-9294 I/[STC-RPC]: ClientSetMessageHandler userHandler:0xced1f015
2022-09-29 10:03:55.012 9271-9294 I/System.out: ClientSetMessageHandler No. 3 com.senter.shotsync.ShotSync$rpc_handler_t[address=0xe4433188,position=0,limit=1,capacity=1,deallocator=org.bytedeco.javacpp.Pointer$NativeDeallocator[ownerAddress=0xe4433188,deallocatorAddress=0xced1f8d1]]
2022-09-29 10:03:55.012 9271-9294 I/[STC-RPC]: ClientSetMessageHandler userHandler:0xced1f035
2022-09-29 10:03:55.013 9271-9294 I/System.out: ClientSetMessageHandler No. 4 com.senter.shotsync.ShotSync$rpc_handler_t[address=0xe4433190,position=0,limit=1,capacity=1,deallocator=org.bytedeco.javacpp.Pointer$NativeDeallocator[ownerAddress=0xe4433190,deallocatorAddress=0xced1f8d1]]
2022-09-29 10:03:55.013 9271-9294 I/[STC-RPC]: ClientSetMessageHandler userHandler:0xced1f055
2022-09-29 10:03:55.013 9271-9294 I/System.out: ClientSetMessageHandler No. 5 com.senter.shotsync.ShotSync$rpc_handler_t[address=0xe4433198,position=0,limit=1,capacity=1,deallocator=org.bytedeco.javacpp.Pointer$NativeDeallocator[ownerAddress=0xe4433198,deallocatorAddress=0xced1f8d1]]
2022-09-29 10:03:55.013 9271-9294 I/[STC-RPC]: ClientSetMessageHandler userHandler:0xced1f075
2022-09-29 10:03:55.014 9271-9294 I/System.out: ClientSetMessageHandler No. 6 com.senter.shotsync.ShotSync$rpc_handler_t[address=0xe44331a0,position=0,limit=1,capacity=1,deallocator=org.bytedeco.javacpp.Pointer$NativeDeallocator[ownerAddress=0xe44331a0,deallocatorAddress=0xced1f8d1]]
2022-09-29 10:03:55.014 9271-9294 I/[STC-RPC]: ClientSetMessageHandler userHandler:0xced1f095
2022-09-29 10:03:55.015 9271-9294 I/System.out: ClientSetMessageHandler No. 7 com.senter.shotsync.ShotSync$rpc_handler_t[address=0xe44331a8,position=0,limit=1,capacity=1,deallocator=org.bytedeco.javacpp.Pointer$NativeDeallocator[ownerAddress=0xe44331a8,deallocatorAddress=0xced1f8d1]]
2022-09-29 10:03:55.015 9271-9294 I/[STC-RPC]: ClientSetMessageHandler userHandler:0xced1f0b5
2022-09-29 10:03:55.015 9271-9294 I/System.out: ClientSetMessageHandler No. 8 com.senter.shotsync.ShotSync$rpc_handler_t[address=0xe44331b0,position=0,limit=1,capacity=1,deallocator=org.bytedeco.javacpp.Pointer$NativeDeallocator[ownerAddress=0xe44331b0,deallocatorAddress=0xced1f8d1]]
2022-09-29 10:03:55.015 9271-9294 I/[STC-RPC]: ClientSetMessageHandler userHandler:0xced1f0d5
2022-09-29 10:03:55.016 9271-9294 I/System.out: ClientSetMessageHandler No. 9 com.senter.shotsync.ShotSync$rpc_handler_t[address=0xe44331b8,position=0,limit=1,capacity=1,deallocator=org.bytedeco.javacpp.Pointer$NativeDeallocator[ownerAddress=0xe44331b8,deallocatorAddress=0xced1f8d1]]
2022-09-29 10:03:55.016 9271-9294 I/[STC-RPC]: ClientSetMessageHandler userHandler:0xced1f0f5
2022-09-29 10:03:55.017 9271-9294 I/System.out: ClientSetMessageHandler No. 10 com.senter.shotsync.ShotSync$rpc_handler_t[address=0xe44331c0,position=0,limit=1,capacity=1,deallocator=org.bytedeco.javacpp.Pointer$NativeDeallocator[ownerAddress=0xe44331c0,deallocatorAddress=0xced1f8d1]]
2022-09-29 10:03:55.017 9271-9294 I/[STC-RPC]: ClientSetMessageHandler userHandler:0xced1f115
2022-09-29 10:03:55.018 9271-9294 I/System.out: ClientSetMessageHandler No. 11 com.senter.shotsync.ShotSync$rpc_handler_t[address=0xe44331c8,position=0,limit=1,capacity=1,deallocator=org.bytedeco.javacpp.Pointer$NativeDeallocator[ownerAddress=0xe44331c8,deallocatorAddress=0xced1f8d1]]
2022-09-29 10:03:55.018 9271-9294 I/[STC-RPC]: ClientSetMessageHandler userHandler:0x0
2022-09-29 10:03:55.018 9271-9294 I/System.out: ClientSetMessageHandler No. 12 com.senter.shotsync.ShotSync$rpc_handler_t[address=0xe44331d0,position=0,limit=1,capacity=1,deallocator=org.bytedeco.javacpp.Pointer$NativeDeallocator[ownerAddress=0xe44331d0,deallocatorAddress=0xced1f8d1]]
2022-09-29 10:03:55.018 9271-9294 I/[STC-RPC]: ClientSetMessageHandler userHandler:0x0
2022-09-29 10:03:55.020 9271-9294 I/System.out: ClientSetMessageHandler No. 13 com.senter.shotsync.ShotSync$rpc_handler_t[address=0xe44331d8,position=0,limit=1,capacity=1,deallocator=org.bytedeco.javacpp.Pointer$NativeDeallocator[ownerAddress=0xe44331d8,deallocatorAddress=0xced1f8d1]]
2022-09-29 10:03:55.020 9271-9294 I/[STC-RPC]: ClientSetMessageHandler userHandler:0x0
2022-09-29 10:03:55.020 9271-9294 I/System.out: ClientSetMessageHandler No. 14 com.senter.shotsync.ShotSync$rpc_handler_t[address=0xe44331e0,position=0,limit=1,capacity=1,deallocator=org.bytedeco.javacpp.Pointer$NativeDeallocator[ownerAddress=0xe44331e0,deallocatorAddress=0xced1f8d1]]
2022-09-29 10:03:55.020 9271-9294 I/[STC-RPC]: ClientSetMessageHandler userHandler:0x0
2022-09-29 10:03:55.021 9271-9294 I/System.out: ClientSetMessageHandler No. 15 com.senter.shotsync.ShotSync$rpc_handler_t[address=0xe44331e8,position=0,limit=1,capacity=1,deallocator=org.bytedeco.javacpp.Pointer$NativeDeallocator[ownerAddress=0xe44331e8,deallocatorAddress=0xced1f8d1]]
2022-09-29 10:03:55.021 9271-9294 I/[STC-RPC]: ClientSetMessageHandler userHandler:0x0
2022-09-29 10:03:55.022 9271-9294 I/System.out: ClientSetMessageHandler No. 16 com.senter.shotsync.ShotSync$rpc_handler_t[address=0xe44331f0,position=0,limit=1,capacity=1,deallocator=org.bytedeco.javacpp.Pointer$NativeDeallocator[ownerAddress=0xe44331f0,deallocatorAddress=0xced1f8d1]]
2022-09-29 10:03:55.022 9271-9294 I/[STC-RPC]: ClientSetMessageHandler userHandler:0x0
2022-09-29 10:03:55.023 9271-9294 I/System.out: ClientSetMessageHandler No. 17 com.senter.shotsync.ShotSync$rpc_handler_t[address=0xe44331f8,position=0,limit=1,capacity=1,deallocator=org.bytedeco.javacpp.Pointer$NativeDeallocator[ownerAddress=0xe44331f8,deallocatorAddress=0xced1f8d1]]
2022-09-29 10:03:55.023 9271-9294 I/[STC-RPC]: ClientSetMessageHandler userHandler:0x0
2022-09-29 10:03:55.024 9271-9294 I/System.out: ClientSetMessageHandler No. 18 com.senter.shotsync.ShotSync$rpc_handler_t[address=0xe4433200,position=0,limit=1,capacity=1,deallocator=org.bytedeco.javacpp.Pointer$NativeDeallocator[ownerAddress=0xe4433200,deallocatorAddress=0xced1f8d1]]
2022-09-29 10:03:55.024 9271-9294 I/[STC-RPC]: ClientSetMessageHandler userHandler:0x0
2022-09-29 10:03:55.025 9271-9294 I/System.out: ClientSetMessageHandler No. 19 com.senter.shotsync.ShotSync$rpc_handler_t[address=0xe4433208,position=0,limit=1,capacity=1,deallocator=org.bytedeco.javacpp.Pointer$NativeDeallocator[ownerAddress=0xe4433208,deallocatorAddress=0xced1f8d1]]
2022-09-29 10:03:55.025 9271-9294 I/[STC-RPC]: ClientSetMessageHandler userHandler:0x0
2022-09-29 10:03:55.026 9271-9294 I/System.out: ClientSetMessageHandler No. 20 com.senter.shotsync.ShotSync$rpc_handler_t[address=0xe4433210,position=0,limit=1,capacity=1,deallocator=org.bytedeco.javacpp.Pointer$NativeDeallocator[ownerAddress=0xe4433210,deallocatorAddress=0xced1f8d1]]
2022-09-29 10:03:55.026 9271-9294 I/[STC-RPC]: ClientSetMessageHandler userHandler:0x0
As we can see, after 10 times of setting the function pointer, we always got a NULL one in C.
The patch to reproduce it is here.
Is this because we have only 10 instances of the function pointer in JNI?