Skip to content

Commit 5b910f8

Browse files
committed
Remove all delete calls outside of _destroy functions
1 parent ac477cf commit 5b910f8

File tree

6 files changed

+66
-127
lines changed

6 files changed

+66
-127
lines changed

src/CSFML/Audio/Music.cpp

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,22 @@ sfMusic* sfMusic_createFromFile(const char* filename)
3535
{
3636
assert(filename);
3737

38-
auto* music = new sfMusic;
39-
if (!music->This.openFromFile(filename))
40-
{
41-
delete music;
42-
music = nullptr;
43-
}
44-
45-
return music;
38+
sf::Music music;
39+
if (!music.openFromFile(filename))
40+
return nullptr;
41+
42+
return new sfMusic{std::move(music), {}};
4643
}
4744

4845

4946
////////////////////////////////////////////////////////////
5047
sfMusic* sfMusic_createFromMemory(const void* data, size_t sizeInBytes)
5148
{
52-
auto* music = new sfMusic;
53-
if (!music->This.openFromMemory(data, sizeInBytes))
54-
{
55-
delete music;
56-
music = nullptr;
57-
}
58-
59-
return music;
49+
sf::Music music;
50+
if (!music.openFromMemory(data, sizeInBytes))
51+
return nullptr;
52+
53+
return new sfMusic{std::move(music), {}};
6054
}
6155

6256

@@ -65,15 +59,11 @@ sfMusic* sfMusic_createFromStream(sfInputStream* stream)
6559
{
6660
assert(stream);
6761

68-
auto* music = new sfMusic;
69-
music->Stream = CallbackStream(stream);
62+
auto music = std::make_unique<sfMusic>(sfMusic{{}, {stream}});
7063
if (!music->This.openFromStream(music->Stream))
71-
{
72-
delete music;
73-
music = nullptr;
74-
}
64+
return nullptr;
7565

76-
return music;
66+
return music.release();
7767
}
7868

7969

src/CSFML/Audio/MusicStruct.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@
3737
////////////////////////////////////////////////////////////
3838
struct sfMusic
3939
{
40-
CallbackStream Stream;
4140
sf::Music This;
41+
CallbackStream Stream;
4242
};

src/CSFML/Graphics/Font.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,11 @@ sfFont* sfFont_createFromStream(sfInputStream* stream)
6060
{
6161
assert(stream);
6262

63-
auto* font = new sfFont;
64-
font->Stream = CallbackStream(stream);
63+
auto font = std::make_unique<sfFont>(sfFont{{}, {}, {stream}});
6564
if (!font->This.openFromStream(font->Stream))
66-
{
67-
delete font;
68-
font = nullptr;
69-
}
65+
return nullptr;
7066

71-
return font;
67+
return font.release();
7268
}
7369

7470

src/CSFML/Graphics/Texture.cpp

Lines changed: 37 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,17 @@
3434
#include <CSFML/System/ConvertVector2.hpp>
3535
#include <CSFML/Window/WindowStruct.hpp>
3636

37+
#include <memory>
38+
3739

3840
////////////////////////////////////////////////////////////
3941
sfTexture* sfTexture_create(sfVector2u size)
4042
{
41-
auto* texture = new sfTexture;
42-
43+
auto texture = std::make_unique<sfTexture>();
4344
if (!texture->This->resize(convertVector2(size)))
44-
{
45-
delete texture;
46-
texture = nullptr;
47-
}
45+
return nullptr;
4846

49-
return texture;
47+
return texture.release();
5048
}
5149

5250

@@ -55,67 +53,47 @@ sfTexture* sfTexture_createFromFile(const char* filename, const sfIntRect* area)
5553
{
5654
assert(filename);
5755

58-
auto* texture = new sfTexture;
59-
60-
const sf::IntRect rect = area ? convertRect(*area) : sf::IntRect();
61-
56+
const auto rect = area ? convertRect(*area) : sf::IntRect();
57+
auto texture = std::make_unique<sfTexture>();
6258
if (!texture->This->loadFromFile(filename, false, rect))
63-
{
64-
delete texture;
65-
texture = nullptr;
66-
}
59+
return nullptr;
6760

68-
return texture;
61+
return texture.release();
6962
}
7063

7164
////////////////////////////////////////////////////////////
7265
sfTexture* sfTexture_createSrgbFromFile(const char* filename, const sfIntRect* area)
7366
{
7467
assert(filename);
7568

76-
auto* texture = new sfTexture;
77-
78-
const sf::IntRect rect = area ? convertRect(*area) : sf::IntRect();
79-
69+
const auto rect = area ? convertRect(*area) : sf::IntRect();
70+
auto texture = std::make_unique<sfTexture>();
8071
if (!texture->This->loadFromFile(filename, true, rect))
81-
{
82-
delete texture;
83-
texture = nullptr;
84-
}
72+
return nullptr;
8573

86-
return texture;
74+
return texture.release();
8775
}
8876

8977
////////////////////////////////////////////////////////////
9078
sfTexture* sfTexture_createFromMemory(const void* data, size_t sizeInBytes, const sfIntRect* area)
9179
{
92-
auto* texture = new sfTexture;
93-
94-
const sf::IntRect rect = area ? convertRect(*area) : sf::IntRect();
95-
80+
const auto rect = area ? convertRect(*area) : sf::IntRect();
81+
auto texture = std::make_unique<sfTexture>();
9682
if (!texture->This->loadFromMemory(data, sizeInBytes, false, rect))
97-
{
98-
delete texture;
99-
texture = nullptr;
100-
}
83+
return nullptr;
10184

102-
return texture;
85+
return texture.release();
10386
}
10487

10588
////////////////////////////////////////////////////////////
10689
sfTexture* sfTexture_createSrgbFromMemory(const void* data, size_t sizeInBytes, const sfIntRect* area)
10790
{
108-
auto* texture = new sfTexture;
109-
110-
const sf::IntRect rect = area ? convertRect(*area) : sf::IntRect();
111-
91+
const auto rect = area ? convertRect(*area) : sf::IntRect();
92+
auto texture = std::make_unique<sfTexture>();
11293
if (!texture->This->loadFromMemory(data, sizeInBytes, true, rect))
113-
{
114-
delete texture;
115-
texture = nullptr;
116-
}
94+
return nullptr;
11795

118-
return texture;
96+
return texture.release();
11997
}
12098

12199

@@ -124,37 +102,27 @@ sfTexture* sfTexture_createFromStream(sfInputStream* stream, const sfIntRect* ar
124102
{
125103
assert(stream);
126104

127-
auto* texture = new sfTexture;
128-
129-
const sf::IntRect rect = area ? convertRect(*area) : sf::IntRect();
130-
105+
const auto rect = area ? convertRect(*area) : sf::IntRect();
131106
CallbackStream sfmlStream(stream);
107+
auto texture = std::make_unique<sfTexture>();
132108
if (!texture->This->loadFromStream(sfmlStream, false, rect))
133-
{
134-
delete texture;
135-
texture = nullptr;
136-
}
109+
return nullptr;
137110

138-
return texture;
111+
return texture.release();
139112
}
140113

141114
////////////////////////////////////////////////////////////
142115
sfTexture* sfTexture_createSrgbFromStream(sfInputStream* stream, const sfIntRect* area)
143116
{
144117
assert(stream);
145118

146-
auto* texture = new sfTexture;
147-
148-
const sf::IntRect rect = area ? convertRect(*area) : sf::IntRect();
149-
119+
const auto rect = area ? convertRect(*area) : sf::IntRect();
150120
CallbackStream sfmlStream(stream);
121+
auto texture = std::make_unique<sfTexture>();
151122
if (!texture->This->loadFromStream(sfmlStream, true, rect))
152-
{
153-
delete texture;
154-
texture = nullptr;
155-
}
123+
return nullptr;
156124

157-
return texture;
125+
return texture.release();
158126
}
159127

160128

@@ -163,35 +131,25 @@ sfTexture* sfTexture_createFromImage(const sfImage* image, const sfIntRect* area
163131
{
164132
assert(image);
165133

166-
auto* texture = new sfTexture;
167-
168-
const sf::IntRect rect = area ? convertRect(*area) : sf::IntRect();
169-
134+
const auto rect = area ? convertRect(*area) : sf::IntRect();
135+
auto texture = std::make_unique<sfTexture>();
170136
if (!texture->This->loadFromImage(image->This, false, rect))
171-
{
172-
delete texture;
173-
texture = nullptr;
174-
}
137+
return nullptr;
175138

176-
return texture;
139+
return texture.release();
177140
}
178141

179142
////////////////////////////////////////////////////////////
180143
sfTexture* sfTexture_createSrgbFromImage(const sfImage* image, const sfIntRect* area)
181144
{
182145
assert(image);
183146

184-
auto* texture = new sfTexture;
185-
186-
const sf::IntRect rect = area ? convertRect(*area) : sf::IntRect();
187-
147+
const auto rect = area ? convertRect(*area) : sf::IntRect();
148+
auto texture = std::make_unique<sfTexture>();
188149
if (!texture->This->loadFromImage(image->This, true, rect))
189-
{
190-
delete texture;
191-
texture = nullptr;
192-
}
150+
return nullptr;
193151

194-
return texture;
152+
return texture.release();
195153
}
196154

197155
////////////////////////////////////////////////////////////

src/CSFML/Graphics/VertexBuffer.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,14 @@
3232
////////////////////////////////////////////////////////////
3333
sfVertexBuffer* sfVertexBuffer_create(unsigned int vertexCount, sfPrimitiveType type, sfVertexBufferUsage usage)
3434
{
35-
auto* buffer = new sfVertexBuffer;
35+
sf::VertexBuffer buffer;
36+
if (!buffer.create(vertexCount))
37+
return nullptr;
3638

37-
if (!buffer->This.create(vertexCount))
38-
{
39-
delete buffer;
40-
buffer = nullptr;
41-
}
42-
else
43-
{
44-
buffer->This.setPrimitiveType(static_cast<sf::PrimitiveType>(type));
45-
buffer->This.setUsage(static_cast<sf::VertexBuffer::Usage>(usage));
46-
}
39+
buffer.setPrimitiveType(static_cast<sf::PrimitiveType>(type));
40+
buffer.setUsage(static_cast<sf::VertexBuffer::Usage>(usage));
4741

48-
return buffer;
42+
return new sfVertexBuffer{std::move(buffer)};
4943
}
5044

5145

src/CSFML/Network/TcpListener.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include <CSFML/Network/TcpListenerStruct.hpp>
3030
#include <CSFML/Network/TcpSocketStruct.hpp>
3131

32+
#include <memory>
33+
3234

3335
////////////////////////////////////////////////////////////
3436
sfTcpListener* sfTcpListener_create()
@@ -90,14 +92,13 @@ sfSocketStatus sfTcpListener_accept(sfTcpListener* listener, sfTcpSocket** conne
9092
assert(listener);
9193
assert(connected);
9294

93-
*connected = new sfTcpSocket;
94-
auto status = static_cast<sfSocketStatus>(listener->This.accept((*connected)->This));
95+
auto socket = std::make_unique<sfTcpSocket>();
96+
auto status = static_cast<sfSocketStatus>(listener->This.accept(socket->This));
9597

9698
if (status != sfSocketDone)
97-
{
98-
delete *connected;
9999
*connected = nullptr;
100-
}
100+
else
101+
*connected = socket.release();
101102

102103
return status;
103104
}

0 commit comments

Comments
 (0)