mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 01:17:34 +00:00
LibVideo: Do not invoke Vector2D's destructor in order to resize it
This leads to all kinds of undefined behavior, especially when callers have a view into the Vector2D.
This commit is contained in:
parent
683c8284cf
commit
8b7c5db186
1 changed files with 12 additions and 6 deletions
|
@ -103,17 +103,14 @@ class Vector2D {
|
||||||
public:
|
public:
|
||||||
~Vector2D()
|
~Vector2D()
|
||||||
{
|
{
|
||||||
if (m_storage)
|
clear_storage();
|
||||||
free(m_storage);
|
|
||||||
m_storage = nullptr;
|
|
||||||
m_width = 0;
|
|
||||||
m_height = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<void> try_resize(u32 height, u32 width)
|
ErrorOr<void> try_resize(u32 height, u32 width)
|
||||||
{
|
{
|
||||||
if (height != m_height && width != m_width) {
|
if (height != m_height && width != m_width) {
|
||||||
this->~Vector2D();
|
clear_storage();
|
||||||
|
|
||||||
size_t size = height * width;
|
size_t size = height * width;
|
||||||
auto* new_storage = static_cast<T*>(malloc(size * sizeof(T)));
|
auto* new_storage = static_cast<T*>(malloc(size * sizeof(T)));
|
||||||
if (!new_storage)
|
if (!new_storage)
|
||||||
|
@ -195,6 +192,15 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void clear_storage()
|
||||||
|
{
|
||||||
|
if (m_storage)
|
||||||
|
free(m_storage);
|
||||||
|
m_storage = nullptr;
|
||||||
|
m_width = 0;
|
||||||
|
m_height = 0;
|
||||||
|
}
|
||||||
|
|
||||||
u32 m_height { 0 };
|
u32 m_height { 0 };
|
||||||
u32 m_width { 0 };
|
u32 m_width { 0 };
|
||||||
T* m_storage { nullptr };
|
T* m_storage { nullptr };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue