Skip to content
This repository was archived by the owner on Jul 16, 2019. It is now read-only.

Commit 3c4a214

Browse files
committed
Memory leak fix
1 parent 00b6122 commit 3c4a214

File tree

2 files changed

+66
-65
lines changed

2 files changed

+66
-65
lines changed

StbSharp/StbImage.Generated.cs

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,14 @@ public struct stbi__result_info
3232
public int channel_order;
3333
}
3434

35-
[StructLayout(LayoutKind.Sequential)]
36-
public struct stbi__huffman
35+
public class stbi__huffman
3736
{
38-
public fixed byte fast[1 << 9];
39-
public fixed ushort code[256];
40-
public fixed byte values[256];
41-
public fixed byte size[257];
42-
public fixed uint maxcode[18];
43-
public fixed int delta[17];
37+
public byte[] fast = new byte[1 << 9];
38+
public ushort[] code = new ushort[256];
39+
public byte[] values = new byte[256];
40+
public byte[] size = new byte[257];
41+
public uint[] maxcode = new uint[18];
42+
public int[] delta = new int[17];
4443
}
4544

4645
[StructLayout(LayoutKind.Sequential)]
@@ -840,7 +839,7 @@ public static ushort stbi__compute_y_16(int r, int g, int b)
840839
return good;
841840
}
842841

843-
public static int stbi__build_huffman(stbi__huffman* h, int* count)
842+
public static int stbi__build_huffman(stbi__huffman h, int* count)
844843
{
845844
int i;
846845
int j;
@@ -850,63 +849,66 @@ public static int stbi__build_huffman(stbi__huffman* h, int* count)
850849
{
851850
for (j = (int)(0); (j) < (count[i]); ++j)
852851
{
853-
h->size[k++] = ((byte)(i + 1));
852+
h.size[k++] = ((byte)(i + 1));
854853
}
855854
}
856855

857-
h->size[k] = (byte)(0);
856+
h.size[k] = (byte)(0);
858857
code = (int)(0);
859858
k = (int)(0);
860859
for (j = (int)(1); j <= 16; ++j)
861860
{
862-
h->delta[j] = (int)(k - code);
863-
if ((h->size[k]) == (j))
861+
h.delta[j] = (int)(k - code);
862+
if ((h.size[k]) == (j))
864863
{
865-
while ((h->size[k]) == (j))
864+
while ((h.size[k]) == (j))
866865
{
867-
h->code[k++] = ((ushort)(code++));
866+
h.code[k++] = ((ushort)(code++));
868867
}
869868

870869
if ((code - 1) >= (1 << j))
871870
return (int)(stbi__err("bad code lengths"));
872871
}
873872

874-
h->maxcode[j] = (uint)(code << (16 - j));
873+
h.maxcode[j] = (uint)(code << (16 - j));
875874
code <<= 1;
876875
}
877876

878-
h->maxcode[j] = (uint)(0xffffffff);
879-
CRuntime.memset(h->fast, (int)(255), (ulong)(1 << 9));
877+
h.maxcode[j] = (uint)(0xffffffff);
878+
for (i = 0; i < h.fast.Length; ++i)
879+
{
880+
h.fast[i] = 255;
881+
}
880882
for (i = (int)(0); (i) < (k); ++i)
881883
{
882-
int s = (int)(h->size[i]);
884+
int s = (int)(h.size[i]);
883885
if (s <= 9)
884886
{
885-
int c = (int)(h->code[i] << (9 - s));
887+
int c = (int)(h.code[i] << (9 - s));
886888
int m = (int)(1 << (9 - s));
887889
for (j = (int)(0); (j) < (m); ++j)
888890
{
889-
h->fast[c + j] = ((byte)(i));
891+
h.fast[c + j] = ((byte)(i));
890892
}
891893
}
892894
}
893895

894896
return (int)(1);
895897
}
896898

897-
public static void stbi__build_fast_ac(short* fast_ac, stbi__huffman* h)
899+
public static void stbi__build_fast_ac(short[] fast_ac, stbi__huffman h)
898900
{
899901
int i;
900902
for (i = (int)(0); (i) < (1 << 9); ++i)
901903
{
902-
byte fast = (byte)(h->fast[i]);
904+
byte fast = (byte)(h.fast[i]);
903905
fast_ac[i] = (short)(0);
904906
if ((fast) < (255))
905907
{
906-
int rs = (int)(h->values[fast]);
908+
int rs = (int)(h.values[fast]);
907909
int run = (int)((rs >> 4) & 15);
908910
int magbits = (int)(rs & 15);
909-
int len = (int)(h->size[fast]);
911+
int len = (int)(h.size[fast]);
910912
if (((magbits) != 0) && (len + magbits <= 9))
911913
{
912914
int k = (int)(((i << len) & ((1 << 9) - 1)) >> (9 - magbits));
@@ -946,29 +948,29 @@ public static void stbi__grow_buffer_unsafe(stbi__jpeg j)
946948
} while (j.code_bits <= 24);
947949
}
948950

949-
public static int stbi__jpeg_huff_decode(stbi__jpeg j, stbi__huffman* h)
951+
public static int stbi__jpeg_huff_decode(stbi__jpeg j, stbi__huffman h)
950952
{
951953
uint temp;
952954
int c;
953955
int k;
954956
if ((j.code_bits) < (16))
955957
stbi__grow_buffer_unsafe(j);
956958
c = (int)((j.code_buffer >> (32 - 9)) & ((1 << 9) - 1));
957-
k = (int)(h->fast[c]);
959+
k = (int)(h.fast[c]);
958960
if ((k) < (255))
959961
{
960-
int s = (int)(h->size[k]);
962+
int s = (int)(h.size[k]);
961963
if ((s) > (j.code_bits))
962964
return (int)(-1);
963965
j.code_buffer <<= s;
964966
j.code_bits -= (int)(s);
965-
return (int)(h->values[k]);
967+
return (int)(h.values[k]);
966968
}
967969

968970
temp = (uint)(j.code_buffer >> 16);
969971
for (k = (int)(9 + 1); ; ++k)
970972
{
971-
if ((temp) < (h->maxcode[k]))
973+
if ((temp) < (h.maxcode[k]))
972974
break;
973975
}
974976

@@ -980,10 +982,10 @@ public static int stbi__jpeg_huff_decode(stbi__jpeg j, stbi__huffman* h)
980982

981983
if ((k) > (j.code_bits))
982984
return (int)(-1);
983-
c = (int)(((j.code_buffer >> (32 - k)) & stbi__bmask[k]) + h->delta[k]);
985+
c = (int)(((j.code_buffer >> (32 - k)) & stbi__bmask[k]) + h.delta[k]);
984986
j.code_bits -= (int)(k);
985987
j.code_buffer <<= k;
986-
return (int)(h->values[c]);
988+
return (int)(h.values[c]);
987989
}
988990

989991
public static int stbi__extend_receive(stbi__jpeg j, int n)
@@ -1023,8 +1025,8 @@ public static int stbi__jpeg_get_bit(stbi__jpeg j)
10231025
return (int)(k & 0x80000000);
10241026
}
10251027

1026-
public static int stbi__jpeg_decode_block(stbi__jpeg j, short* data, stbi__huffman* hdc, stbi__huffman* hac,
1027-
short* fac, int b, ushort* dequant)
1028+
public static int stbi__jpeg_decode_block(stbi__jpeg j, short* data, stbi__huffman hdc, stbi__huffman hac,
1029+
short[] fac, int b, ushort[] dequant)
10281030
{
10291031
int diff;
10301032
int dc;
@@ -1085,7 +1087,7 @@ public static int stbi__jpeg_decode_block(stbi__jpeg j, short* data, stbi__huffm
10851087
return (int)(1);
10861088
}
10871089

1088-
public static int stbi__jpeg_decode_block_prog_dc(stbi__jpeg j, short* data, stbi__huffman* hdc, int b)
1090+
public static int stbi__jpeg_decode_block_prog_dc(stbi__jpeg j, short* data, stbi__huffman hdc, int b)
10891091
{
10901092
int diff;
10911093
int dc;
@@ -1112,7 +1114,7 @@ public static int stbi__jpeg_decode_block_prog_dc(stbi__jpeg j, short* data, stb
11121114
return (int)(1);
11131115
}
11141116

1115-
public static int stbi__jpeg_decode_block_prog_ac(stbi__jpeg j, short* data, stbi__huffman* hac, short* fac)
1117+
public static int stbi__jpeg_decode_block_prog_ac(stbi__jpeg j, short* data, stbi__huffman hac, short[] fac)
11161118
{
11171119
int k;
11181120
if ((j.spec_start) == (0))
@@ -1476,9 +1478,9 @@ public static int stbi__parse_entropy_coded_data(stbi__jpeg z)
14761478
{
14771479
int ha = (int)(z.img_comp[n].ha);
14781480
if (
1479-
stbi__jpeg_decode_block(z, data, (stbi__huffman*)z.huff_dc + z.img_comp[n].hd,
1480-
(stbi__huffman*)z.huff_ac + ha,
1481-
z.fast_ac[ha], (int)(n), (ushort*)z.dequant[z.img_comp[n].tq]) ==
1481+
stbi__jpeg_decode_block(z, data, z.huff_dc[z.img_comp[n].hd],
1482+
z.huff_ac[ha],
1483+
z.fast_ac[ha], (int)(n), z.dequant[z.img_comp[n].tq]) ==
14821484
0)
14831485
return (int)(0);
14841486
z.idct_block_kernel(z.img_comp[n].data + z.img_comp[n].w2 * j * 8 + i * 8,
@@ -1520,9 +1522,9 @@ public static int stbi__parse_entropy_coded_data(stbi__jpeg z)
15201522
int ha = (int)(z.img_comp[n].ha);
15211523
if (
15221524
stbi__jpeg_decode_block(z, data,
1523-
(stbi__huffman*)z.huff_dc + z.img_comp[n].hd,
1524-
(stbi__huffman*)z.huff_ac + ha, z.fast_ac[ha], (int)(n),
1525-
(ushort*)z.dequant[z.img_comp[n].tq]) == 0)
1525+
z.huff_dc[z.img_comp[n].hd],
1526+
z.huff_ac[ha], z.fast_ac[ha], (int)(n),
1527+
z.dequant[z.img_comp[n].tq]) == 0)
15261528
return (int)(0);
15271529
z.idct_block_kernel(z.img_comp[n].data + z.img_comp[n].w2 * y2 + x2,
15281530
(int)(z.img_comp[n].w2), data);
@@ -1561,14 +1563,13 @@ public static int stbi__parse_entropy_coded_data(stbi__jpeg z)
15611563
if ((z.spec_start) == (0))
15621564
{
15631565
if (stbi__jpeg_decode_block_prog_dc(z, data,
1564-
(stbi__huffman*)z.huff_dc + z.img_comp[n].hd, (int)(n)) == 0)
1566+
z.huff_dc[z.img_comp[n].hd], (int)(n)) == 0)
15651567
return (int)(0);
15661568
}
15671569
else
15681570
{
15691571
int ha = (int)(z.img_comp[n].ha);
1570-
if (stbi__jpeg_decode_block_prog_ac(z, data, (stbi__huffman*)z.huff_ac + ha,
1571-
z.fast_ac[ha]) == 0)
1572+
if (stbi__jpeg_decode_block_prog_ac(z, data, z.huff_ac[ha], z.fast_ac[ha]) == 0)
15721573
return (int)(0);
15731574
}
15741575

@@ -1607,7 +1608,7 @@ public static int stbi__parse_entropy_coded_data(stbi__jpeg z)
16071608
int y2 = (int)(j * z.img_comp[n].v + y);
16081609
short* data = z.img_comp[n].coeff + 64 * (x2 + y2 * z.img_comp[n].coeff_w);
16091610
if (stbi__jpeg_decode_block_prog_dc(z, data,
1610-
(stbi__huffman*)z.huff_dc + z.img_comp[n].hd, (int)(n)) == 0)
1611+
z.huff_dc[z.img_comp[n].hd], (int)(n)) == 0)
16111612
return (int)(0);
16121613
}
16131614
}
@@ -1630,7 +1631,7 @@ public static int stbi__parse_entropy_coded_data(stbi__jpeg z)
16301631

16311632
}
16321633

1633-
public static void stbi__jpeg_dequantize(short* data, ushort* dequant)
1634+
public static void stbi__jpeg_dequantize(short* data, ushort[] dequant)
16341635
{
16351636
int i;
16361637
for (i = (int)(0); (i) < (64); ++i)
@@ -1655,7 +1656,7 @@ public static void stbi__jpeg_finish(stbi__jpeg z)
16551656
for (i = (int)(0); (i) < (w); ++i)
16561657
{
16571658
short* data = z.img_comp[n].coeff + 64 * (i + j * z.img_comp[n].coeff_w);
1658-
stbi__jpeg_dequantize(data, (ushort*)z.dequant[z.img_comp[n].tq]);
1659+
stbi__jpeg_dequantize(data, z.dequant[z.img_comp[n].tq]);
16591660
z.idct_block_kernel(z.img_comp[n].data + z.img_comp[n].w2 * j * 8 + i * 8,
16601661
(int)(z.img_comp[n].w2), data);
16611662
}
@@ -1704,7 +1705,7 @@ public static int stbi__process_marker(stbi__jpeg z, int m)
17041705
L = (int)(stbi__get16be(z.s) - 2);
17051706
while ((L) > (0))
17061707
{
1707-
byte* v;
1708+
byte[] v;
17081709
int* sizes = stackalloc int[16];
17091710
int i;
17101711
int n = (int)(0);
@@ -1722,17 +1723,17 @@ public static int stbi__process_marker(stbi__jpeg z, int m)
17221723
L -= (int)(17);
17231724
if ((tc) == (0))
17241725
{
1725-
if (stbi__build_huffman((stbi__huffman*)z.huff_dc + th, sizes) == 0)
1726+
if (stbi__build_huffman(z.huff_dc[th], sizes) == 0)
17261727
return (int)(0);
1727-
stbi__huffman* h = (stbi__huffman*)z.huff_dc + th;
1728-
v = h->values;
1728+
stbi__huffman h = z.huff_dc[th];
1729+
v = h.values;
17291730
}
17301731
else
17311732
{
1732-
if (stbi__build_huffman((stbi__huffman*)z.huff_ac + th, sizes) == 0)
1733+
if (stbi__build_huffman(z.huff_ac[th], sizes) == 0)
17331734
return (int)(0);
1734-
stbi__huffman* h = (stbi__huffman*)z.huff_ac + th;
1735-
v = h->values;
1735+
stbi__huffman h = z.huff_ac[th];
1736+
v = h.values;
17361737
}
17371738

17381739
for (i = (int)(0); (i) < (n); ++i)
@@ -1741,7 +1742,7 @@ public static int stbi__process_marker(stbi__jpeg z, int m)
17411742
}
17421743

17431744
if (tc != 0)
1744-
stbi__build_fast_ac(z.fast_ac[th], (stbi__huffman*)z.huff_ac + th);
1745+
stbi__build_fast_ac(z.fast_ac[th], z.huff_ac[th]);
17451746
L -= (int)(n);
17461747
}
17471748

StbSharp/StbImage.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ public struct img_comp
5353
public class stbi__jpeg
5454
{
5555
public stbi__context s;
56-
public readonly stbi__huffman* huff_dc = (stbi__huffman*) stbi__malloc(4 * sizeof(stbi__huffman));
57-
public readonly stbi__huffman* huff_ac = (stbi__huffman*) stbi__malloc(4 * sizeof(stbi__huffman));
58-
public readonly ushort*[] dequant;
56+
public readonly stbi__huffman[] huff_dc = new stbi__huffman[4];
57+
public readonly stbi__huffman[] huff_ac = new stbi__huffman[4];
58+
public readonly ushort[][] dequant;
5959

60-
public readonly short*[] fast_ac;
60+
public readonly short[][] fast_ac;
6161

6262
// sizes for components, interleaved MCUs
6363
public int img_h_max, img_v_max;
@@ -83,7 +83,7 @@ public class stbi__jpeg
8383
public int rgb;
8484

8585
public int scan_n;
86-
public int* order = (int*) stbi__malloc(4 * sizeof(int));
86+
public int[] order = new int[4];
8787
public int restart_interval, todo;
8888

8989
// kernels
@@ -104,16 +104,16 @@ public stbi__jpeg()
104104
img_comp[i] = new img_comp();
105105
}
106106

107-
fast_ac = new short *[4];
107+
fast_ac = new short[4][];
108108
for (var i = 0; i < fast_ac.Length; ++i)
109109
{
110-
fast_ac[i] = (short*) stbi__malloc((1 << STBI__ZFAST_BITS) * sizeof(short));
110+
fast_ac[i] = new short[1 << STBI__ZFAST_BITS];
111111
}
112112

113-
dequant = new ushort *[4];
113+
dequant = new ushort [4][];
114114
for (var i = 0; i < dequant.Length; ++i)
115115
{
116-
dequant[i] = (ushort*) stbi__malloc(64 * sizeof(ushort));
116+
dequant[i] = new ushort[64];
117117
}
118118
}
119119
};

0 commit comments

Comments
 (0)