[c++]can not run ray-template and put() bad_alloc

I’m new to using ray. Follow the documentation tutorial, i install ray and run test:

pip install -U ray[cpp]
# Create a Ray C++ project template to start with.
ray cpp --generate-bazel-project-template-to ray-template 
cd $somepath/ray-template && bash run.sh

printf:
Fetching @rules_cc; fetching 30s
lding failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
ERROR: An error occurred during the fetch of repository ‘rules_cc’:

i do not know how to fix it, so i decide to build a demo with gcc:

g++ ray_test.cpp -lray_api -g -std=c++17

int main(int argc, char **argv) {
  /// initialization
  ray::Init();

  /// put and get object
  auto object = ray::Put(100);
  auto put_get_result = *(ray::Get(object));
}

when i run ray_test, printf:
logging.cc:97: Unhandled exception: St9bad_alloc. what(): std::bad_alloc
logging.cc:104: Stack trace:
libray_api.so(+0xd6fe6a) [0x7f1cef526e6a] ray::operator<<()
,
,
[0x408100] ray::CopyAndAddReference()
[0x40dab2] ray::ObjectRef<>::ObjectRef()

What did I do wrong?

Looks like the Put function returned a null value:

auto id = ray::internal::GetRayRuntime()->Put(buffer);
// id = "";

@GuyangSong could you help check this?

Seems like a bazel issue. What’s your bazel version? Can you try 4.2.1?

About this, maybe you can try to set the ABI to 0, we already set it in the template https://github.com/ray-project/ray/blob/master/cpp/example/.bazelrc

thanks, It’s working right. now I can link ray_api.so to learn how to use ray with c++.
about bazel, may be just network permission issues. I’m going to forget bazel.

hi, here’s a new problem. use the following compilation command and code:

g++ ray_test.cpp -lray_api -g -std=c++17 -D_GLIBCXX_USE_CXX11_ABI=0 -Wl,-rpath

int main(int argc, char **argv) {
  /// initialization
  ray::Init();

  /// put and get object
  auto object = ray::Put(100);
  auto put_get_result = *(ray::Get(object));
  // object  = [string] = 0x6a2a58 ""  and size not show
  // printf put_get_result  = 100;

  /// common task
  auto task_object = ray::Task(Plus).Remote(6, 2);
  auto task_result = *task_object.Get();
  // task_object = [string] = 0xd7ac28 "\337\317YD˴\336L", '\377' <repeats 12 times>, "\016"
  /*
    Aborted in Get() function.
    0x0000000000441ff6  callq  0x44efbc <ray::ObjectRef<int>::ID() const> 
    0x0000000000441ffb  mov    %rax,%rdx 
    0x0000000000441ffe  lea    -0x30(%rbp),%rax 
    0x0000000000442002  mov    %rbx,%rsi 
    0x0000000000442005  mov    %rax,%rdi 
    0x0000000000442008  callq  *%r12 
    0x000000000044200b  lea    -0x20(%rbp),%rax 
  */
}

what else do I need to do before using task and actor?

Can you learn this the bazel template project first? ray/cpp/example/BUILD.bazel at master · ray-project/ray · GitHub

I guess the issue is that we need a dynamic library, beside the binary.

Thank you again. It’s been successfully executed.
I’m still wondering why need to compile a dynamic library using the same source code to use ray.

As a new man, It’s hard for me to know that from the code that Docs “Ray Core–User Guides–Tasks–example” presents.

Yep, the C++ API documentation is bad :rofl:. We need to improve it. Sorry for the bad experience.