<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <p>Hi,<br>
      I still have the feeling that we're looking at several pieces --
      but missing the full picture. We know that:</p>
    <p>* this is a custom C++ library<br>
      * there is a C header which exports some of the C++ symbols with
      `extern "C"`<br>
      * this C++ library seems to depend on the datachannel library<br>
    </p>
    <p>We haven't seen how the Java side does the calling.</p>
    <p>It would be really helpful if there was a self-contained
      repository somewhere we could look at to try and reproduce (and
      better understand) this issue.</p>
    <p>As for this code, `make_unique` creates a new RtcManager object
      -- so maybe something is up with the constructor call -- but hard
      to say what could be (the issue could also be completely
      unrelated).</p>
    <p>If you remove most of the code from `createRtcContext` and just
      leave this</p>
    <p>```<br>
      rtcManager = std::make_unique<RtcManager>()<br>
      ```</p>
    <p>Does it still crash there?<br>
    </p>
    <p>Maurizio<br>
    </p>
    <div class="moz-cite-prefix">On 08/04/2025 01:51, bai li wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:CAFEHhdb4cQfVpLusOTxdbbmqvuc_EVQFCbu3aaV9=dUeMrGRUg@mail.gmail.com">
      
      <div dir="ltr">
        <div>The createRtcContext function is like this:</div>
        <div>
          <div style="background-color:rgb(30,31,34);color:rgb(188,190,196)">
            <pre style="font-family:"JetBrains Mono",monospace;font-size:9.8pt"><span style="color:rgb(207,142,109)">void </span><span style="color:rgb(86,168,245)">createRtcContext</span>(<span style="color:rgb(207,142,109)">const char</span>* localId, <span style="color:rgb(207,142,109)">int </span>logLevel, <span style="color:rgb(181,182,227)">IceConfig</span>* iceConfig, <span style="color:rgb(181,182,227)">DataChannelConfig</span>* dataChannelConfig) {
    <span style="color:rgb(181,182,227)">std</span>::cout <span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(106,171,115)">"1" </span><span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(181,182,227)">std</span>::endl;
    <span style="color:rgb(207,142,109)">int </span>expected = <span style="color:rgb(42,172,184)">0</span>;
    <span style="color:rgb(207,142,109)">bool </span>success = state.compare_exchange_strong(expected, <span style="color:rgb(42,172,184)">1</span>);
    <span style="color:rgb(207,142,109)">if </span>(!success) <span style="color:rgb(207,142,109)">return</span>;
    <span style="color:rgb(207,142,109)">if </span>(localId == <span style="color:rgb(207,142,109)">nullptr</span>) <span style="color:rgb(207,142,109)">return</span>;
    <span style="color:rgb(207,142,109)">if </span>(iceConfig == <span style="color:rgb(207,142,109)">nullptr</span>) <span style="color:rgb(207,142,109)">return</span>;
    <span style="color:rgb(207,142,109)">if </span>(dataChannelConfig == <span style="color:rgb(207,142,109)">nullptr</span>) <span style="color:rgb(207,142,109)">return</span>;
    <span style="color:rgb(181,182,227)">std</span>::cout <span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(106,171,115)">"2" </span><span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(181,182,227)">std</span>::endl;
    <span style="color:rgb(181,182,227)">std</span>::<span style="color:rgb(185,188,209)">string </span>id = <span style="color:rgb(181,182,227)">std</span>::string(localId);
    <span style="color:rgb(181,182,227)">std</span>::cout <span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(106,171,115)">"2 1" </span><span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(106,171,115)">" " </span><span style="color:rgb(95,140,138)"><< </span>id <span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(181,182,227)">std</span>::endl;
    <span style="color:rgb(181,182,227)">std</span>::make_unique<<span style="color:rgb(207,142,109)">int</span>>(<span style="color:rgb(42,172,184)">3</span>);
    <span style="color:rgb(181,182,227)">std</span>::cout <span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(106,171,115)">"2 1 1" </span><span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(181,182,227)">std</span>::endl;
    rtcManager <span style="color:rgb(95,140,138)">= </span><span style="color:rgb(181,182,227)">std</span>::make_unique<<span style="color:rgb(181,182,227)">RtcManager</span>>(); <span style="color:rgb(122,126,133)">// Crash here!
</span><span style="color:rgb(122,126,133)">    </span><span style="color:rgb(181,182,227)">std</span>::cout <span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(106,171,115)">"2 1 2" </span><span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(181,182,227)">std</span>::endl;
    rtcManager<span style="color:rgb(95,140,138)">-></span>setLocalId(id);
    <span style="color:rgb(181,182,227)">std</span>::cout <span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(106,171,115)">"2 2" </span><span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(181,182,227)">std</span>::endl;
    rtcManager<span style="color:rgb(95,140,138)">-></span>initLogger(logLevel);
    <span style="color:rgb(181,182,227)">std</span>::cout <span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(106,171,115)">"2 3" </span><span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(181,182,227)">std</span>::endl;
    <span style="color:rgb(181,182,227)">IceModel </span>iceModel;
    <span style="color:rgb(181,182,227)">std</span>::cout <span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(106,171,115)">"2 4" </span><span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(181,182,227)">std</span>::endl;
    iceModel.setNoStun(iceConfig-><span style="color:rgb(147,115,165)">noStun</span>);
    iceModel.setStunServer(<span style="color:rgb(181,182,227)">std</span>::string(iceConfig-><span style="color:rgb(147,115,165)">stunServer</span>));
    iceModel.setStunPort(iceConfig-><span style="color:rgb(147,115,165)">stunPort</span>);
    iceModel.setUdpMux(iceConfig-><span style="color:rgb(147,115,165)">udpMux</span>);
    iceModel.setWebSocketServer(<span style="color:rgb(181,182,227)">std</span>::string(iceConfig-><span style="color:rgb(147,115,165)">webSocketServer</span>));
    iceModel.setWebSocketPort(iceConfig-><span style="color:rgb(147,115,165)">webSocketPort</span>);
    <span style="color:rgb(181,182,227)">std</span>::cout <span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(106,171,115)">"2 5" </span><span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(181,182,227)">std</span>::endl;
    <span style="color:rgb(207,142,109)">auto </span>dataChannelListener = <span style="color:rgb(181,182,227)">DataChannelListener</span>::Builder()
            .withOnOpen(dataChannelConfig-><span style="color:rgb(147,115,165)">onOpen</span>)
            .withOnClosed(dataChannelConfig-><span style="color:rgb(147,115,165)">onClosed</span>)
            .withOnTextMessage(dataChannelConfig-><span style="color:rgb(147,115,165)">onTextMessage</span>)
            .withOnBinaryMessage(dataChannelConfig-><span style="color:rgb(147,115,165)">onBinaryMessage</span>)
            .build();
    <span style="color:rgb(181,182,227)">std</span>::cout <span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(106,171,115)">"2 6" </span><span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(181,182,227)">std</span>::endl;
    <span style="color:rgb(181,182,227)">std</span>::<span style="color:rgb(181,182,227)">shared_ptr</span><<span style="color:rgb(181,182,227)">DataChannelListener</span>> dcListener = <span style="color:rgb(181,182,227)">std</span>::move(dataChannelListener);
    <span style="color:rgb(181,182,227)">std</span>::cout <span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(106,171,115)">"3" </span><span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(181,182,227)">std</span>::endl;
    rtcManager<span style="color:rgb(95,140,138)">-></span>initDataChannelListener(dcListener);
    <span style="color:rgb(181,182,227)">std</span>::cout <span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(106,171,115)">"4" </span><span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(181,182,227)">std</span>::endl;
    <span style="color:rgb(181,182,227)">std</span>::<span style="color:rgb(181,182,227)">unique_ptr</span><<span style="color:rgb(181,182,227)">WebSocketHelper</span>> webSocketHelper = rtcManager<span style="color:rgb(95,140,138)">-></span>connectToSignalingServer(iceModel);
    <span style="color:rgb(181,182,227)">std</span>::cout <span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(106,171,115)">"5" </span><span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(181,182,227)">std</span>::endl;
    <span style="color:rgb(207,142,109)">while </span>(state.load() == <span style="color:rgb(42,172,184)">1</span>) {
        <span style="color:rgb(181,182,227)">std</span>::<span style="color:rgb(181,182,227)">this_thread</span>::yield();
    }
    <span style="color:rgb(181,182,227)">std</span>::cout <span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(106,171,115)">"6" </span><span style="color:rgb(95,140,138)"><< </span><span style="color:rgb(181,182,227)">std</span>::endl;
}</pre>
          </div>
        </div>
      </div>
      <br>
      <div class="gmail_quote gmail_quote_container">
        <div dir="ltr" class="gmail_attr">On Mon, Mar 31, 2025 at
          5:26 PM bai li <<a href="mailto:k3315368931@gmail.com" moz-do-not-send="true" class="moz-txt-link-freetext">k3315368931@gmail.com</a>>
          wrote:<br>
        </div>
        <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
          <div dir="ltr">Hi Panama team,<br>
            <br>
            I’m using the Java FFM API in JDK 21 to call a C++ dynamic
            library function, `void createRtcContext(const char*
            localId, int logLevel, IceConfig* iceConfig,
            DataChannelConfig* dataChannelConfig)`. The function can be
            invoked, but it crashes when executing
            `std::make_unique<RtcManager>()` inside the function.
            The same C++ code runs without any issues when executed
            independently. The `RtcManager` class is roughly defined as
            follows:<br>
            <br>
            ```cpp<br>
            class RtcManager {<br>
            private:<br>
                std::string localId;<br>
                rtc::Configuration config;<br>
                std::unordered_map<std::string,
            std::shared_ptr<rtc::PeerConnection>>
            peerConnectionMap;<br>
                std::unordered_map<std::string,
            std::shared_ptr<rtc::DataChannel>> dataChannelMap;<br>
                std::shared_ptr<rtc::WebSocket> ws =
            std::make_shared<rtc::WebSocket>();<br>
                std::shared_ptr<DataChannelListener>
            dataChannelListener = nullptr;<br>
            public:<br>
                explicit RtcManager(const std::string& id) :
            localId(id) {}<br>
            };
            <div>
              <div>The C++ code is compiled with MSVC 2022. Here’s the
                crash log from the console:</div>
            </div>
            <div># A fatal error has been detected by the Java Runtime
              Environment:<br>
              #<br>
              #  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at
              pc=0x00007ffb0ca62f58, pid=22096, tid=26348<br>
              #<br>
              # JRE version: Java(TM) SE Runtime Environment (21.0.6+8)
              (build 21.0.6+8-LTS-188)<br>
              # Java VM: Java HotSpot(TM) 64-Bit Server VM
              (21.0.6+8-LTS-188, mixed mode, sharing, tiered, compressed
              oops, compressed class ptrs, g1 gc, windows-amd64)<br>
              # Problematic frame:<br>
              # C  [msvcp140.dll+0x12f58]<br>
              #<br>
              # No core dump will be written. Minidumps are not enabled
              by default on client versions of Windows<br>
              #<br>
              # An error report file with more information is saved as:<br>
              # E:\projects\backend-projects\test\hs_err_pid22096.log<br>
              [0.236s][warning][os] Loading hsdis library failed<br>
              #<br>
              # If you would like to submit a bug report, please visit:<br>
              #   <a href="https://bugreport.java.com/bugreport/crash.jsp" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">https://bugreport.java.com/bugreport/crash.jsp</a><br>
              # The crash happened outside the Java Virtual Machine in
              native code.<br>
              # See problematic frame for where to report the bug.<br>
              #<br>
            </div>
            <div>
              <div>Thanks</div>
            </div>
          </div>
        </blockquote>
      </div>
    </blockquote>
  </body>
</html>