提交 944a125c 编写于 作者: M Megvii Engine Team

fix(mgb): improve exception safety with smart pointers

GitOrigin-RevId: e08421ac2938954cbd0bfca5ba17781f69e3a812
上级 32cd9ca0
...@@ -117,23 +117,21 @@ bool Mat<T>::equals(const Mat<T>& rhs) const { ...@@ -117,23 +117,21 @@ bool Mat<T>::equals(const Mat<T>& rhs) const {
return false; return false;
if (this->m_channels != rhs.m_channels) if (this->m_channels != rhs.m_channels)
return false; return false;
T* row1 = new T[m_cols * m_channels]; std::unique_ptr<T[]> row1(new T[m_cols * m_channels]);
T* row2 = new T[m_cols * m_channels]; std::unique_ptr<T[]> row2(new T[m_cols * m_channels]);
megdnn_assert(row1); megdnn_assert(row1);
megdnn_assert(row2); megdnn_assert(row2);
for (size_t r = 0; r < m_rows; ++r) { for (size_t r = 0; r < m_rows; ++r) {
cuda_check(cudaMemcpy(row1, this->ptr(r), cuda_check(cudaMemcpy(row1.get(), this->ptr(r),
sizeof(T) * m_cols * m_channels, sizeof(T) * m_cols * m_channels,
cudaMemcpyDeviceToHost)); cudaMemcpyDeviceToHost));
cuda_check(cudaMemcpy(row2, rhs.ptr(r), sizeof(T) * m_cols * m_channels, cuda_check(cudaMemcpy(row2.get(), rhs.ptr(r), sizeof(T) * m_cols * m_channels,
cudaMemcpyDeviceToHost)); cudaMemcpyDeviceToHost));
for (size_t i = 0; i < m_cols * m_channels; ++i) { for (size_t i = 0; i < m_cols * m_channels; ++i) {
if (row1[i] != row2[i]) if (row1[i] != row2[i])
return false; return false;
} }
} }
delete[] row1;
delete[] row2;
return true; return true;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册