Skip to content

Commit df3aff1

Browse files
Merge pull request #2 from flitsmeister/feature/fix-memory-leak
Fix memory leak in encode
2 parents ed7ee68 + 13a12ed commit df3aff1

File tree

3 files changed

+7
-15
lines changed

3 files changed

+7
-15
lines changed

Sources/FlitsGeohash/GeoHash.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ public enum Geohash {
4646
if length < 1, length > 22 {
4747
assertionFailure("length must be greater than 0 and less than 23")
4848
}
49-
return string(from: GEOHASH_encode(coordinate.latitude, coordinate.longitude, length))
49+
guard let pointer = GEOHASH_encode(coordinate.latitude, coordinate.longitude, length) else {
50+
fatalError()
51+
}
52+
let hash = string(from: pointer)
53+
free(pointer)
54+
return hash
5055
}
5156

5257
public static func adjacent(hash: String, direction: Direction) -> String {
@@ -57,7 +62,7 @@ public enum Geohash {
5762
fatalError()
5863
}
5964
let adjacent = string(from: pointer)
60-
GEOHASH_free_adjacent(pointer)
65+
free(pointer)
6166
return adjacent
6267
}
6368

Sources/FlitsGeohashC/cgeohash.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ typedef struct {
2323
} GEOHASH_neighbors;
2424

2525
char* GEOHASH_encode(double latitude, double longitude, unsigned int hash_length);
26-
void GEOHASH_free_encode(char* hash);
27-
2826
GEOHASH_neighbors* GEOHASH_get_neighbors(const char *hash);
2927
void GEOHASH_free_neighbors(GEOHASH_neighbors *neighbors);
3028

3129
char* GEOHASH_get_adjacent(const char* hash, GEOHASH_direction dir);
32-
void GEOHASH_free_adjacent(char* hash);

Sources/FlitsGeohashC/cgeohash.m

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,6 @@
8383
return hash;
8484
}
8585

86-
void
87-
GEOHASH_free_encode(char* hash) {
88-
free(hash);
89-
}
90-
9186
GEOHASH_neighbors*
9287
GEOHASH_get_neighbors(const char* hash)
9388
{
@@ -166,8 +161,3 @@
166161
base[len] = BASE32_ENCODE_TABLE[idx];
167162
return base;
168163
}
169-
170-
void
171-
GEOHASH_free_adjacent(char* hash) {
172-
free(hash);
173-
}

0 commit comments

Comments
 (0)