www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.conv.to!string refuses to convert a char* to string.

reply Venkat <venkatram.akkineni gmail.com> writes:
I am trying out the DJni library 
(https://github.com/Monnoroch/DJni). For some reason 
std.conv.to!string doesn't want to convert a char* to a 
string.The lines below are taken from the log. I see that the 
last frame is at gc_qalloc. I am not sure why it failed there. 
Can anybody elaborate on what is going on here ? Thanks in 
advance.

  91
  92
  93 Stack: [0x00007f749dfd9000,0x00007f749e0da000],  
sp=0x00007f749e0d8600,  free space=1021k
  94 Native frames: (J=compiled Java code, j=interpreted, Vv=VM 
code, C=native code)
  95 C  [libPrompt.so+0x6b0cc]  gc_qalloc+0x2c
  96 C  [libPrompt.so+0x7bf18]  
_D2rt8lifetime12__arrayAllocFNaNbmxC8TypeInfoxQlZS4core6memory8BlkInfo_+0xec
  97 C  [libPrompt.so+0x6c2fa]  _d_newarrayU+0x86
  98 C  [libPrompt.so+0x593a7]  
_D6object__T4_dupTxaTaZQlFNaNbAxaZAa+0x1f
  99 C  [libPrompt.so+0x59384]  
_D6object__T11_trustedDupTxaTaZQtFNaNbNeAxaZAa+0x20
100 C  [libPrompt.so+0x59360]  
_D6object__T3dupTaZQhFNaNbNdNfAxaZAa+0x20
101 C  [libPrompt.so+0x5932a]  
_D3std4conv__T6toImplTAyaTPxaZQqFQhZ9__lambda2MFNaNbZQBf+0x3e
102 C  [libPrompt.so+0x592ea]  
_D3std4conv__T6toImplTAyaTPxaZQqFNaNbQlZQs+0x16
103 C  [libPrompt.so+0x592d1]  
_D3std4conv__T2toTAyaZ__TQlTPxaZQsFNaNbQlZQy+0x9
104 C  [libPrompt.so+0x59289]  Java_Prompt_getLine+0x61
105 j  Prompt.getLine(Ljava/lang/String;)Ljava/lang/String;+0
106 j  Prompt.main([Ljava/lang/String;)V+11
107 v  ~StubRoutines::call_stub
108 V  [libjvm.so+0x690c66]  JavaCalls::call_helper(JavaValue*, 
methodHandle*, JavaCallArguments*, Thread*)+0x1056
109 V  [libjvm.so+0x6d2072]  jni_invoke_static(JNIEnv_*, 
JavaValue*, _jobject*, JNICallType, _jmethodID*, 
JNI_ArgumentPusher*, Thread*)+0x362
110 V  [libjvm.so+0x6ee8da]  jni_CallStaticVoidMethod+0x17a
111 C  [libjli.so+0x7bdf]  JavaMain+0x81f
112 C  [libpthread.so.0+0x8184]  start_thread+0xc4
113
114 Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
115 j  Prompt.getLine(Ljava/lang/String;)Ljava/lang/String;+0
116 j  Prompt.main([Ljava/lang/String;)V+11
Dec 08 2017
parent reply Neia Neutuladh <neia ikeran.org> writes:
On Saturday, 9 December 2017 at 05:55:21 UTC, Venkat wrote:
 I am trying out the DJni library 
 (https://github.com/Monnoroch/DJni). For some reason 
 std.conv.to!string doesn't want to convert a char* to a 
 string.The lines below are taken from the log. I see that the 
 last frame is at gc_qalloc. I am not sure why it failed there. 
 Can anybody elaborate on what is going on here ? Thanks in 
 advance.
I've got no idea, but can you verify that you can print it with printf? Can you allocate other GC memory? Can you try using fromStringz instead of to!string and see what that does? Just shots in the dark...
Dec 08 2017
parent reply Venkat <venkatram.akkineni gmail.com> writes:
Thanks for the quick response. std.string.fromStringz did the 
trick. I am not sure what was the deal with to!string.
Dec 08 2017
parent reply Mike Parker <aldacron gmail.com> writes:
On Saturday, 9 December 2017 at 06:14:36 UTC, Venkat wrote:
 Thanks for the quick response. std.string.fromStringz did the 
 trick. I am not sure what was the deal with to!string.
Be careful with fromStringz. It doesn't allocate a new string, so the returned string can easily become corrupted if the C string it's referencing falls off the stack. As for your problem with to!string, it isn't actually to!string that's the issue. It appears to have something to do with the memory allocated from the GC. I assume this is a shared library. Random thoughts -- Have you initialized DRuntime? Have you registered the calling thread with the GC?
Dec 09 2017
parent Venkat <venkatram.akkineni gmail.com> writes:
Thank you, core.runtime.Runtime.initialize() fixed the issue. I 
am now able to use to!string as well. I found your posts and Ali 
Çehreli's posts on this subject. I think I have some 
understanding now.
Dec 09 2017