Skip to content

Commit

Permalink
solve memory leak when returning error in python and csharp lib
Browse files Browse the repository at this point in the history
  • Loading branch information
felipejfc committed Mar 6, 2019
1 parent 334aa1f commit 18d3fcd
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 28 deletions.
Expand Up @@ -16,7 +16,7 @@ set(CMAKE_IMPORT_FILE_VERSION 1)
set(_targetsDefined)
set(_targetsNotDefined)
set(_expectedTargets)
foreach(_expectedTarget cpprestsdk::cpprest cpprestsdk::cpprestsdk_boost_internal cpprestsdk::cpprestsdk_zlib_internal cpprestsdk::cpprestsdk_openssl_internal cpprestsdk::cpprestsdk_websocketpp_internal)
foreach(_expectedTarget cpprestsdk::cpprest cpprestsdk::cpprestsdk_boost_internal cpprestsdk::cpprestsdk_zlib_internal cpprestsdk::cpprestsdk_openssl_internal)
list(APPEND _expectedTargets ${_expectedTarget})
if(NOT TARGET ${_expectedTarget})
list(APPEND _targetsNotDefined ${_expectedTarget})
Expand Down Expand Up @@ -53,10 +53,9 @@ endif()
add_library(cpprestsdk::cpprest STATIC IMPORTED)

set_target_properties(cpprestsdk::cpprest PROPERTIES
INTERFACE_COMPILE_DEFINITIONS "CPPREST_FORCE_HTTP_CLIENT_ASIO;CPPREST_FORCE_HTTP_LISTENER_ASIO"
INTERFACE_COMPILE_OPTIONS "-fPIC"
INTERFACE_COMPILE_DEFINITIONS "CPPREST_EXCLUDE_WEBSOCKETS=1;CPPREST_FORCE_HTTP_CLIENT_ASIO;CPPREST_FORCE_HTTP_LISTENER_ASIO"
INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
INTERFACE_LINK_LIBRARIES "-ldl;-lpthread;\$<LINK_ONLY:-ldl>;\$<LINK_ONLY:cpprestsdk::cpprestsdk_websocketpp_internal>;\$<LINK_ONLY:dl>;\$<LINK_ONLY:cpprestsdk::cpprestsdk_zlib_internal>;cpprestsdk::cpprestsdk_boost_internal;cpprestsdk::cpprestsdk_openssl_internal;cpprestsdk::cpprestsdk_boost_internal;cpprestsdk::cpprestsdk_openssl_internal"
INTERFACE_LINK_LIBRARIES "-lpthread;\$<LINK_ONLY:cpprestsdk::cpprestsdk_zlib_internal>;cpprestsdk::cpprestsdk_boost_internal;cpprestsdk::cpprestsdk_openssl_internal;cpprestsdk::cpprestsdk_boost_internal;cpprestsdk::cpprestsdk_openssl_internal"
)

# Create imported target cpprestsdk::cpprestsdk_boost_internal
Expand All @@ -73,17 +72,9 @@ set_target_properties(cpprestsdk::cpprestsdk_zlib_internal PROPERTIES
add_library(cpprestsdk::cpprestsdk_openssl_internal INTERFACE IMPORTED)

set_target_properties(cpprestsdk::cpprestsdk_openssl_internal PROPERTIES
INTERFACE_COMPILE_DEFINITIONS "CPPREST_NO_SSL_LEAK_SUPPRESS"
INTERFACE_LINK_LIBRARIES "OpenSSL::SSL"
)

# Create imported target cpprestsdk::cpprestsdk_websocketpp_internal
add_library(cpprestsdk::cpprestsdk_websocketpp_internal INTERFACE IMPORTED)

set_target_properties(cpprestsdk::cpprestsdk_websocketpp_internal PROPERTIES
INTERFACE_LINK_LIBRARIES "cpprestsdk::cpprestsdk_boost_internal;cpprestsdk::cpprestsdk_openssl_internal"
)

if(CMAKE_VERSION VERSION_LESS 3.0.0)
message(FATAL_ERROR "This file relies on consumers using CMake 3.0.0 or greater.")
endif()
Expand Down
Expand Up @@ -56,7 +56,7 @@ set_target_properties(cpprestsdk::cpprest PROPERTIES
INTERFACE_COMPILE_DEFINITIONS "CPPREST_FORCE_HTTP_CLIENT_ASIO;CPPREST_FORCE_HTTP_LISTENER_ASIO"
INTERFACE_COMPILE_OPTIONS "-fPIC"
INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
INTERFACE_LINK_LIBRARIES "-ldl;\$<LINK_ONLY:-ldl>;\$<LINK_ONLY:cpprestsdk::cpprestsdk_websocketpp_internal>;\$<LINK_ONLY:dl>;\$<LINK_ONLY:cpprestsdk::cpprestsdk_zlib_internal>;/Library/Frameworks/CoreFoundation.framework;/Library/Frameworks/Security.framework;cpprestsdk::cpprestsdk_boost_internal;cpprestsdk::cpprestsdk_openssl_internal;cpprestsdk::cpprestsdk_boost_internal;cpprestsdk::cpprestsdk_openssl_internal"
INTERFACE_LINK_LIBRARIES "-ldl;\$<LINK_ONLY:-ldl>;\$<LINK_ONLY:dl>;\$<LINK_ONLY:cpprestsdk::cpprestsdk_zlib_internal>;cpprestsdk::cpprestsdk_boost_internal;cpprestsdk::cpprestsdk_openssl_internal;cpprestsdk::cpprestsdk_boost_internal;cpprestsdk::cpprestsdk_openssl_internal"
)

# Create imported target cpprestsdk::cpprestsdk_boost_internal
Expand Down
4 changes: 2 additions & 2 deletions cpp-lib/src/pitaya/c_wrapper.cpp
Expand Up @@ -56,7 +56,7 @@ FreePitayaError(CPitayaError* err)
{
free(err->code);
free(err->msg);
free(err);
//free(err);
}

static void
Expand All @@ -66,7 +66,7 @@ FreeServer(CServer* sv)
free(sv->type);
free(sv->metadata);
free(sv->hostname);
free(sv);
//free(sv);
}

pitaya::etcdv3_service_discovery::Config
Expand Down
2 changes: 1 addition & 1 deletion csharp-example/csharp-example/Program.cs
Expand Up @@ -96,4 +96,4 @@ static void Main(string[] args)
// }
}
}
}
}
2 changes: 1 addition & 1 deletion csharp-example/csharp-example/csharp-example.csproj
Expand Up @@ -31,7 +31,7 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="Google.Protobuf">
<HintPath>..\..\csharp-lib\packages\protobuf.net35.3.5.1\lib\net35\Google.Protobuf.dll</HintPath>
<HintPath>..\..\csharp-lib\packages\TFG.Protobuff35.3.6.1\lib\net35\Google.Protobuf.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
Expand Down
10 changes: 5 additions & 5 deletions csharp-lib/cluster-lib/PitayaCluster.cs
Expand Up @@ -211,9 +211,9 @@ public static unsafe T Rpc<T>(string serverId, Route route, IMessage msg)

if (!ok) // error
{
// FreePitayaErrorInternal(retError);

throw new PitayaException($"RPC call failed: ({retError.code}: {retError.msg})");
string code = string.Copy(retError.code);
string errMsg = string.Copy(retError.msg);
throw new PitayaException($"RPC call failed: ({code}: {errMsg})");
}

var protoRet = GetProtoMessageFromMemoryBuffer<T>(*memBufPtr);
Expand Down Expand Up @@ -248,7 +248,7 @@ public static T Rpc<T>(Route route, IMessage msg)
[DllImport("libpitaya_cluster", CallingConvention = CallingConvention.Cdecl, EntryPoint = "tfg_pitc_OnSignal")]
private static extern void OnSignalInternal(OnSignalFunc ptr);

// [DllImport("libpitaya_cluster", CallingConvention = CallingConvention.Cdecl, EntryPoint = "tfg_pitc_FreePitayaError")]
// private static extern unsafe void FreePitayaErrorInternal(/**/Pitaya.Error *ptr);
[DllImport("libpitaya_cluster", CallingConvention = CallingConvention.Cdecl, EntryPoint = "tfg_pitc_FreePitayaError")]
private static extern unsafe void FreePitayaErrorInternal(ref Error err);
}
}
Binary file modified precompiled/libpitaya_cluster.dylib
Binary file not shown.
Binary file modified precompiled/libpitaya_cluster.so
Binary file not shown.
12 changes: 7 additions & 5 deletions python-lib/pitayaserver/pitayaserver.py
@@ -1,6 +1,6 @@
from .route import Route
from google.protobuf import message
from ctypes import POINTER, byref, c_char, c_void_p, addressof, memmove, sizeof
from ctypes import POINTER, byref, c_char, c_char_p, c_void_p, addressof, memmove, sizeof
from .gen.response_pb2 import Response
from .remote import BaseRemote
from .c_interop import SdConfig, NatsConfig, Server, Native, FREECB, MemoryBuffer, RPCReq, RPCCB, PitayaError
Expand Down Expand Up @@ -29,11 +29,12 @@ def initialize_pitaya(

def get_server_by_id(server_id: str):
""" gets a server by its id """
# TODO needs free
# TODO needs free -- leak here!
sv = Server()
success = LIB.tfg_pitc_GetServerById(server_id.encode('utf-8'), sv)
if success is False:
raise Exception('failed to get server {}'.format(server_id))
# LIB.tfg_pitc_FreeServer(byref(sv))
return sv


Expand Down Expand Up @@ -120,7 +121,7 @@ def _rpc_cb(req: POINTER(RPCReq)) -> c_void_p:
return _get_error_response_c_void_p("PIT-500", err_str)


def send_rpc(route: str, in_msg: message.Message, res_class: message.Message, server_id: str = '') -> message.Message:
def send_rpc(route: str, in_msg: message.Message, res_class: message.Message, server_id: str='') -> message.Message:
""" sends a rpc to other pitaya server """
if not issubclass(type(in_msg), message.Message) or not issubclass(res_class, message.Message):
raise TypeError
Expand All @@ -132,8 +133,9 @@ def send_rpc(route: str, in_msg: message.Message, res_class: message.Message, se
res = LIB.tfg_pitc_RPC(server_id.encode(
'utf-8'), route.encode('utf-8'), addressof(c_bytes), msg_len, byref(ret_ptr), byref(err))
if not res:
# TODO: needs free
raise Exception("code: {} msg: {}".format(err.code, err.msg))
exception_msg = "code: {} msg: {}".format(err.code, err.msg)
LIB.tfg_pitc_FreePitayaError(err)
raise Exception(exception_msg)
ret_bytes = (
c_char * ret_ptr.contents.size).from_address(ret_ptr.contents.data)
response = Response()
Expand Down
2 changes: 1 addition & 1 deletion python-lib/setup.py
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='pitayaserver',
version='0.2.0',
version='0.2.1',
author="TFG Co",
author_email="backend@tfgco.com",
description="A library for creating pitaya backend servers using python",
Expand Down

0 comments on commit 18d3fcd

Please sign in to comment.