Skip to content

Commit

Permalink
c_str() の結果を明示的にアロケーションしないと Unity Editor がクラッシュする
Browse files Browse the repository at this point in the history
  • Loading branch information
enm10k committed Feb 8, 2024
1 parent 75665ae commit 4844029
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/unity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,9 @@ void sora_audio_output_helper_set_handsfree(void* p, unity_bool_t enabled) {
UNITY_INTERFACE_EXPORT const char* get_track_id(ptrid_t track_id,
unity_bool_t* found) {
void* p = sora_unity_sdk::IdPointer::Instance().Lookup(track_id);

*found = p != nullptr ? 1 : 0;

// return *found ? ((sora_unity_sdk::UnityRenderer::Sink*)p)->GetTrackId().c_str()
return *found ? ((sora_unity_sdk::UnityRenderer::Sink*)p)->GetTrackIdC()
: nullptr;
}
Expand Down
9 changes: 3 additions & 6 deletions src/unity_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ namespace sora_unity_sdk {

// UnityRenderer::Sink

UnityRenderer::Sink::Sink(webrtc::VideoTrackInterface* track) : track_(track) {
UnityRenderer::Sink::Sink(webrtc::VideoTrackInterface* track)
: track_(track), track_id_(track_->id()) {
RTC_LOG(LS_INFO) << "[" << (void*)this << "] Sink::Sink";
deleting_ = false;
updating_ = false;
ptrid_ = IdPointer::Instance().Register(this);
track_->AddOrUpdateSink(this, rtc::VideoSinkWants());
track_id_ = track->id();

// track_id_c_ = track_id_.c_str();
size_t size = strlen(track_id_.c_str());
track_id_c_ = new char[size + 1];
strcpy(track_id_c_, track_id_.c_str());
Expand All @@ -33,15 +32,13 @@ UnityRenderer::Sink::~Sink() {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}

// track_id_c_ を開放すると Unity Editor がクラッシュする
// delete[] track_id_c_;
track_->RemoveSink(this);
IdPointer::Instance().Unregister(ptrid_);
}
ptrid_t UnityRenderer::Sink::GetSinkID() const {
return ptrid_;
}
std::string UnityRenderer::Sink::GetTrackId() const {
const std::string& UnityRenderer::Sink::GetTrackId() const {
return track_id_;
}
char* UnityRenderer::Sink::GetTrackIdC() const {
Expand Down
2 changes: 1 addition & 1 deletion src/unity_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class UnityRenderer {
Sink(webrtc::VideoTrackInterface* track);
~Sink();
ptrid_t GetSinkID() const;
std::string GetTrackId() const;
const std::string& GetTrackId() const;
char* GetTrackIdC() const;
void SetTrack(webrtc::VideoTrackInterface* track);

Expand Down

0 comments on commit 4844029

Please sign in to comment.