bytedeco/javacpp

return std::vector<float> as FloatBuffer error

Open

#786 opened on Nov 11, 2024

View on GitHub
 (0 comments) (0 reactions) (0 assignees)Java (620 forks)batch import
bughelp wanted

Repository metrics

Stars
 (4,279 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

I have a c++ function like this:std::vector test(); when i use FloatBuffer to get the return of cpp function,it's get exception

here is the source code : <NativeLibrary.h>

#include <vector>

std::vector<float> test(std::vector<short>& input){
    std::vector<float> result(input.size());
    for(int i = 0; i< input.size() ; ++i){
    result[i] = input[i];
    }

    return std::move(result);
}

NativeLibraryConfig.java

package org.example;

import org.bytedeco.javacpp.annotation.Platform;
import org.bytedeco.javacpp.annotation.Properties;
import org.bytedeco.javacpp.tools.InfoMap;
import org.bytedeco.javacpp.tools.InfoMapper;

@Properties(
        value = {
                @Platform(include={"NativeLibrary.h"}
        }
        ,target = "org.example.NativeLibrary"
)


public class NativeLibraryConfig implements InfoMapper{
    @Override
    public void map(InfoMap infoMap) {

    }
}

main.java

package org.example;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

public class Main {
  public static void main(String[] args) throws InterruptedException {
    FloatBuffer output= NativeLibrary.test(ShortBuffer.allocate(256));
    System.out.printf("cap:%d\n", output.capacity()); // Expected capacity is 256, actual is 1024.
    output.array();
    System.out.println(output.getClass().getName());
  }
}

STDOUT:

cap:1024
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000077940422, pid=21564, tid=0x00000000000028e4
#
# JRE version: OpenJDK Runtime Environment (8.0_432-b06) (build 1.8.0_432-b06)
# Java VM: OpenJDK 64-Bit Server VM (25.432-b06 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# V  [jvm.dll+0x1e0422]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# E:\test\javacpp_thread\hs_err_pid21564.log
#
# If you would like to submit a bug report, please visit:
#   https://github.com/adoptium/adoptium-support/issues
#

Contributor guide