Bug fix: memory leak of marker cluster feature
MODIFY
plugin.xml
plugin.xml
<?xml version='1.0' encoding='utf-8'?>
-<plugin id="cordova-plugin-googlemaps" version="2.0.0-beta3-20170826-0040" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
+<plugin id="cordova-plugin-googlemaps" version="2.0.0-beta3-20170827-1706" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<name>cordova-plugin-googlemaps</name>
<js-module name="BaseClass" src="www/BaseClass.js">
<runs />

MODIFY
src/android/plugin/google/maps/PluginMarkerCluster.java
src/android/plugin/google/maps/PluginMarkerCluster.java
@@ -7,6 +7,7 @@ import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
+import com.google.android.gms.maps.model.Polygon;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
@@ -19,6 +20,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
+import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class PluginMarkerCluster extends PluginMarker {
@@ -50,11 +52,11 @@ public class PluginMarkerCluster extends PluginMarker {
public void onDestroy() {
super.onDestroy();
stopFlag = true;
-
}
@Override
protected void clear() {
+ super.clear();
synchronized (semaphore) {
synchronized (pluginMarkers) {
synchronized (deleteMarkers) {
@@ -68,7 +70,6 @@ public class PluginMarkerCluster extends PluginMarker {
}
}
}
-
}
//---------------------
@@ -146,9 +147,40 @@ public class PluginMarkerCluster extends PluginMarker {
}
}
}
+
+ String cacheKey;
+ Set<String> keySet = pluginMap.objects.keySet();
+ Marker marker;
+ String[] objectIdArray = keySet.toArray(new String[keySet.size()]);
+ for (String objectId : objectIdArray) {
+ if (objectId.contains(clusterId)) {
+ if (objectId.startsWith("marker_icon")) {
+ cacheKey = (String) pluginMap.objects.remove(objectId);
+
+ if (iconCacheKeys.containsKey(cacheKey)) {
+ int count = iconCacheKeys.get(cacheKey);
+ if (count < 1) {
+ iconCacheKeys.remove(cacheKey);
+ AsyncLoadImage.removeBitmapFromMemCahce(cacheKey);
+ } else {
+ iconCacheKeys.put(cacheKey, count - 1);
+ }
+ }
+ } else if (objectId.startsWith("marker_property_") ||
+ objectId.startsWith("marker_imageSize_")) {
+ pluginMap.objects.remove(objectId);
+ } else if (objectId.startsWith("marker_")) {
+ marker = (Marker) pluginMap.objects.remove(objectId);
+ marker.remove();
+ } else {
+ pluginMap.objects.remove(objectId);
+ }
+ }
+ }
synchronized (deleteThreadLock) {
deleteThreadLock.notify();
}
+
callbackContext.success();
}
/**

MODIFY
src/ios/GoogleMaps/PluginMarkerCluster.m
src/ios/GoogleMaps/PluginMarkerCluster.m
@@ -90,7 +90,17 @@ const int GEOCELL_GRID_SIZE = 4;
- (void)pluginUnload
{
self.stopFlag = true;
- [super pluginInitialize];
+ [super pluginUnload];
+
+ @synchronized (self.pluginMarkers) {
+ NSString *key;
+ NSArray *keys = self.pluginMarkers.allKeys;
+ for (int i = 0; i < keys.count; i++) {
+ key = [keys objectAtIndex:i];
+ [self.pluginMarkers setObject:@"DELETED" forKey:key];
+ [self.deleteMarkers addObject:key];
+ }
+ }
}
@@ -113,6 +123,7 @@ const int GEOCELL_GRID_SIZE = 4;
}
}
}
+
dispatch_semaphore_signal(self.deleteThreadLock);

Bug fix: memory leak of marker cluster feature