Problem using JNA for Tellstick Duo on Win 7 64-bit

Moderator: Telldus

hazzem
Posts: 13
Joined: Fri Mar 17, 2023 9:45 am

Problem using JNA for Tellstick Duo on Win 7 64-bit

Post by hazzem »

Hello Telldus and friends!

Thank you for a brilliant product! I have used it for about 6 months and like it a lot! :D
I also would like to thank the author of the great pkj.no.tellstick package! :D

Now, I am using the Tellstick Duo batch 1 which I have upgraded to latest firmware 5 and also installed latest version 2.1.1 of Telldus Center. I have installed Java 1.6.0_30 (64-bit version).

Originally I was using Windows XP and had no problems writing a Java program communicating using JNA and the pkj.no.tellstick implementation. But when installing on a Windows 7 64-bit version an error occurs when executing (as an application from within Eclipse) on the following lines:

Code: Select all

String name = JNA.CLibrary.INSTANCE.tdGetName(tellstickId); // tellstickId = 1
JNA.CLibrary.INSTANCE.tdReleaseString(name);  // name = "Example device"   <= This line causes appcrash!
An error stating that the Java(TM) platform SE binary has stopped working is shown with the following details (in Swedish but still):
Problemsignatur:
Problemhändelsens namn: APPCRASH
Programnamn: javaw.exe
Programversion: 6.0.300.12
Programtidsstämpel: 4ebba002
Namn på felmodul: StackHash_9eed
Modulens version: 6.1.7601.17725
Tidsstämpel för felmodul: 4ec4aa8e
Undantagskod: c0000374
Undantagsförskjutning: 00000000000c40f2
OS-version: 6.1.7601.2.1.0.256.48
Språkvariant-ID: 1053
Ytterligare information 1: 9eed
Ytterligare information 2: 9eed24ceb325766412db0589833a550b
Ytterligare information 3: a9f5
Ytterligare information 4: a9f52ea99ed5df50062274a5d9ad7905
After the upgrade to latest version of Tellstick Duo firmware and Telldus Center it still runs fine on my Windows XP computer so there must be something I am missing on my Windows 7 64-bit version. :?

If anyone could help me out with what has gone wrong, hint me in the right direction, or perhaps even improve the JNA package or what ever it takes to make the Java <--> Tellstick communication work will make me incredibly happy! :D :D

Thank you all!
/Hazze
micke.prag
Site Admin
Posts: 2243
Joined: Fri Mar 17, 2023 9:45 am
Location: Lund
Contact:

Re: Problem using JNA for Tellstick Duo on Win 7 64-bit

Post by micke.prag »

How do you load the dll? Could you provide the code for that?

Have you the possibilities to try it out on Win 7 32-bit?
Micke Prag
Software
Telldus Technologies
hazzem
Posts: 13
Joined: Fri Mar 17, 2023 9:45 am

Re: Problem using JNA for Tellstick Duo on Win 7 64-bit

Post by hazzem »

Thanks for your reply.

The following code loads the TelldusCore.dll which is placed in the lib folder of the Java application:

Code: Select all

CLibrary INSTANCE = (CLibrary) Native.loadLibrary("TelldusCore", CLibrary.class);
CLibrary is the interface placed in the JNA class and the code is found here:
http://code.google.com/p/javatellstick/ ... k/JNA.java

Unfortunately I don't have the possibility to try it on a 32-bit Win7.
micke.prag
Site Admin
Posts: 2243
Joined: Fri Mar 17, 2023 9:45 am
Location: Lund
Contact:

Re: Problem using JNA for Tellstick Duo on Win 7 64-bit

Post by micke.prag »

When installing the software on a 64-bit machine actually two versions of TelldusCore.dll is installed. One 64-bit and one 32-bit.

What happens if you delete the TelldusCode.dll in the lib-folder? JNA should be able to load it from the system folder (and also load the correct one automatically).
Micke Prag
Software
Telldus Technologies
hazzem
Posts: 13
Joined: Fri Mar 17, 2023 9:45 am

Re: Problem using JNA for Tellstick Duo on Win 7 64-bit

Post by hazzem »

Thanks for your reply.

I have now tried some more combinations including
- removing the TelldusCore.dll from my lib, it did not make any difference as I get exactly the same error message (as in my first post) again.
- copying the TelldusCore.dll from the System32 folder to my lib folder, still get the same error message.
- copying the TelldusCore.dll from the SysWOW64 folder to my lib folder, and get the same error message once more.

So, I guess I am still on square one.
micke.prag
Site Admin
Posts: 2243
Joined: Fri Mar 17, 2023 9:45 am
Location: Lund
Contact:

Re: Problem using JNA for Tellstick Duo on Win 7 64-bit

Post by micke.prag »

NexaHome is written in Java, maybe hekj kan help out how he is using the functions?
Micke Prag
Software
Telldus Technologies
hekj
Posts: 992
Joined: Fri Mar 17, 2023 9:45 am
Location: Stockholm
Contact:

Re: Problem using JNA for Tellstick Duo on Win 7 64-bit

Post by hekj »

hazzem wrote: Now, I am using the Tellstick Duo batch 1 which I have upgraded to latest firmware 5 and also installed latest version 2.1.1 of Telldus Center. I have installed Java 1.6.0_30 (64-bit version).

Originally I was using Windows XP and had no problems writing a Java program communicating using JNA and the pkj.no.tellstick implementation. But when installing on a Windows 7 64-bit version an error occurs when executing (as an application from within Eclipse) on the following lines:

Code: Select all

String name = JNA.CLibrary.INSTANCE.tdGetName(tellstickId); // tellstickId = 1
JNA.CLibrary.INSTANCE.tdReleaseString(name);  // name = "Example device"   <= This line causes appcrash!
...

If anyone could help me out with what has gone wrong, hint me in the right direction, or perhaps even improve the JNA package or what ever it takes to make the Java <--> Tellstick communication work will make me incredibly happy! :D :D
I have both Java versions installed on my Win7, but I normally start NexaHome using the 32-bit version.

I have now tested NexaHome with the 64-bit versions of Java/dll and get an appcrash at the same method (tdReleaseString), I have previously seen the "same" problem in Linux.

Temporary fix is to skip the tdReleaseString method when you are using 64-bit Java.
hazzem
Posts: 13
Joined: Fri Mar 17, 2023 9:45 am

Re: Problem using JNA for Tellstick Duo on Win 7 64-bit

Post by hazzem »

Ah, it is not just me then! :D
@hekj: Thanks for confirming that on your Windows 7 64-bit, Henrik!

Yes, I suppose I can remove the release calls temporarily. But temporary fixes are obviously never long term solutions. :?

What is the purpose of the release string functionality in this case? I have not looked in to how JNI handles this and what is good practice when doing this sort of calls. If it is needed to avoid memory leakage then something must clearly be fixed in the 64-bit dll. If there is no problems with memory leakage then it is no longer necessary to release any of the strings even in 32-bit Windows dll?

@micke.prag: When do you think this relase string problem can be solved in the TelldusCore.dll for 64-bit Windows 7? :)
micke.prag
Site Admin
Posts: 2243
Joined: Fri Mar 17, 2023 9:45 am
Location: Lund
Contact:

Re: Problem using JNA for Tellstick Duo on Win 7 64-bit

Post by micke.prag »

hazzem wrote:@micke.prag: When do you think this relase string problem can be solved in the TelldusCore.dll for 64-bit Windows 7?
When?
As soon as we figure out the issue, of course.
Micke Prag
Software
Telldus Technologies
hazzem
Posts: 13
Joined: Fri Mar 17, 2023 9:45 am

Re: Problem using JNA for Tellstick Duo on Win 7 64-bit

Post by hazzem »

Thanks Micke!

I really wanted to make sure that this is something that is being worked on, as I guess you have a lot of development stuff to prioritize... 8)

Do let me know if there is anything I can help you out with (e.g. testing a new dll) to resolve this issue. :)

BR
/Hazze
hekj
Posts: 992
Joined: Fri Mar 17, 2023 9:45 am
Location: Stockholm
Contact:

Re: Problem using JNA for Tellstick Duo on Win 7 64-bit

Post by hekj »

micke.prag wrote:
hazzem wrote:@micke.prag: When do you think this relase string problem can be solved in the TelldusCore.dll for 64-bit Windows 7?
When?
As soon as we figure out the issue, of course.
To make it possible to use NexaHome with all supported operating systems, the usage of release string command was removed in NexaHome v2.7.4.
hazzem
Posts: 13
Joined: Fri Mar 17, 2023 9:45 am

Re: Problem using JNA for Tellstick Duo on Win 7 64-bit

Post by hazzem »

I was just wondering, is there a ticket created for this issue? I'm concerned that the application will eventually eat all the memory if the strings are not released properly...

BR
/Hazze
micke.prag
Site Admin
Posts: 2243
Joined: Fri Mar 17, 2023 9:45 am
Location: Lund
Contact:

Re: Problem using JNA for Tellstick Duo on Win 7 64-bit

Post by micke.prag »

Micke Prag
Software
Telldus Technologies
Mnemonic
Posts: 38
Joined: Fri Mar 17, 2023 9:45 am

Re: Problem using JNA for Tellstick Duo on Win 7 64-bit

Post by Mnemonic »

I can bring some light to this case since its not a bug in the Telldus API rather a missunderstanding in the management regarding manual garbage collecting in C also called de-allocation in terms of C. For the record I am using the latest TelldusCenter on Windows 7 x64 with no libs in my project folder eg. i am loading the shared library.

Though im no C/C++ expert however we start with the functions for this issue:

From the Telldus-Core we find:

Code: Select all

char *WINAPI 	tdGetName (int intDeviceId)
void WINAPI 	tdReleaseString (char *thestring)
In JAVA JNA this translates into:

Code: Select all

Pointer tdGetName( int deviceId );
void tdReleaseString( Pointer value );
The first function tdGetName returns a pointer and the second function want the same pointer in return. Simple explanation: pointers are a variable holder for memory, so to clean up memory we need the pointer. Example:

Code: Select all

Pointer pointerToName = libTelldus.tdGetName( id );	//Retrives the name of the device by unique id.
String name = pointerToName.getString(0);                        //Retrive the string from the pointer.
System.out.println( name );						//Prints the name.
libTelldus.tdReleaseString( pointerToName );			//Release the resource in the pointer.
My example shows that we save the value of the pointer in pointerToName and to get the String from that we use getString(0); to have JAVA fetch the actual string from the memory the pointer is pointing at. Thirdly we print it out and last we release the memory held by the pointer in tdReleaseString.

From http://jna.java.net/javadoc/overview-summary.html we find that char in C is byte in JAVA, thus char *variable is a pointer in C and Pointer in JAVA. I have come across some missunderstanding with const char* in C is String in JAVA so i think you guys have been using these statements in you JNA inteface:

Code: Select all

String tdGetName (int intDeviceId)
void tdReleaseString (String value)
Since when I test with these function declerations i can reproduce the problem reported but using the proper way to remove memory allocations which is stated in official JNA documentation that memory de-allocation is not done automatically by JNA the problem is gone.

/Per
hazzem
Posts: 13
Joined: Fri Mar 17, 2023 9:45 am

Re: Problem using JNA for Tellstick Duo on Win 7 64-bit

Post by hazzem »

Hello Per!

Thank you very much for pointing me in the right direction (no pun intended)! :D
The problem is now solved! No more app crashes for my 64-bit win 7 server! Yay!!

I had a feeling that something was bad, I just could not figure it out. As you said it turns out that the JNA.java was a bit dodgy. Now that you explained how to use it properly I got it all working beautifully. You have already explained how to use the release string bit, so I'll just add the changes I also made to the JNA.java file:

Code: Select all

		// TELLSTICK_API void WINAPI tdReleaseString(char *string);
		public void tdReleaseString(Pointer pointer);

		// TELLSTICK_API char * WINAPI tdGetErrorString(int intErrorNo);
		public Pointer tdGetErrorString(int intErrorNo);

		// TELLSTICK_API char * WINAPI tdGetName(int intDeviceId);
		public Pointer tdGetName(int intDeviceId);

		// TELLSTICK_API char * WINAPI tdGetProtocol(int intDeviceId);
		public Pointer tdGetProtocol(int intDeviceId);

		// TELLSTICK_API char * WINAPI tdGetModel(int intDeviceId);
		public Pointer tdGetModel(int intDeviceId);

		// TELLSTICK_API char * WINAPI tdGetDeviceParameter(int intDeviceId, const char *strName, const char *defaultValue);
		public Pointer tdGetDeviceParameter(int intDeviceId, String strName, String defaultValue);

Thanks again, Per! No more memory leaks or crashes! :D

@micke.prag: You can now close the ticket http://developer.telldus.com/ticket/239.

BR
/Hazze
Post Reply