Skip to content

Commit

Permalink
address memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshGandhi-AWS committed Jul 3, 2023
1 parent 4638d2d commit 6610e14
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
3 changes: 2 additions & 1 deletion source/SharedCrtResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,8 @@ aws_event_loop *SharedCrtResourceManager::getNextEventLoop()
return nullptr;
}

return aws_event_loop_group_get_next_loop(eventLoopGroup->GetUnderlyingHandle());
eventLoop = aws_event_loop_group_get_next_loop(eventLoopGroup->GetUnderlyingHandle());
return eventLoop;
}

aws_allocator *SharedCrtResourceManager::getAllocator()
Expand Down
6 changes: 4 additions & 2 deletions source/SharedCrtResourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ namespace Aws
std::unique_ptr<Aws::Crt::Io::DefaultHostResolver> defaultHostResolver;
std::unique_ptr<Aws::Crt::Io::ClientBootstrap> clientBootstrap;
std::unique_ptr<Aws::Iot::MqttClient> mqttClient;
std::shared_ptr<Crt::Mqtt::MqttConnection> connection;
aws_allocator *allocator{nullptr};

aws_mem_trace_level memTraceLevel{AWS_MEMTRACE_NONE};
std::shared_ptr<Util::FeatureRegistry> features;

Expand All @@ -55,6 +54,9 @@ namespace Aws
/**
* inheritable for testing
*/
std::shared_ptr<Crt::Mqtt::MqttConnection> connection;
aws_allocator *allocator{nullptr};
aws_event_loop *eventLoop;
bool locateCredentials(const PlainConfig &config) const;

public:
Expand Down
18 changes: 6 additions & 12 deletions test/sensor-publish/TestSensorPublishFeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class FakeSharedCrtResourceManager : public SharedCrtResourceManager
public:
FakeSharedCrtResourceManager()
{
allocator = aws_default_allocator();
// eventLoop = aws_event_loop_new_default(allocator, aws_high_res_clock_get_ticks);
// allocator = aws_default_allocator();
eventLoop = aws_event_loop_new_default(allocator, aws_high_res_clock_get_ticks);
}

// ~FakeSharedCrtResourceManager() { aws_event_loop_destroy(eventLoop); }
Expand All @@ -38,10 +38,10 @@ class FakeSharedCrtResourceManager : public SharedCrtResourceManager

aws_allocator *getAllocator() override { return allocator; }

std::unique_ptr<Aws::Crt::ApiHandle> apiHandle;
aws_allocator *allocator;
std::shared_ptr<Aws::Crt::Mqtt::MqttConnection> connection; // No connection.
aws_event_loop *eventLoop;
// std::unique_ptr<Aws::Crt::ApiHandle> apiHandle;
// aws_allocator *allocator;
// std::shared_ptr<Aws::Crt::Mqtt::MqttConnection> connection; // No connection.
// aws_event_loop *eventLoop;
};

class FakeNotifier : public ClientBaseNotifier
Expand Down Expand Up @@ -176,8 +176,6 @@ class MockSensorPublishFeature : public SensorPublishFeature

TEST_F(SensorPublishFeatureTest, InitSensorSuccess)
{
manager->eventLoop = aws_event_loop_new_default(manager->allocator, aws_high_res_clock_get_ticks);

// Feature initializes one Sensor per entry in config.
MockSensorPublishFeature feature;

Expand All @@ -188,7 +186,6 @@ TEST_F(SensorPublishFeatureTest, InitSensorSuccess)

TEST_F(SensorPublishFeatureTest, InitSensorDisabled)
{
manager->eventLoop = aws_event_loop_new_default(manager->allocator, aws_high_res_clock_get_ticks);
// When a Sensor entry is disabled in config,
// then the entry is not added to the list of sensors.
config.sensorPublish.settings[1].enabled = false;
Expand All @@ -215,7 +212,6 @@ class MockSensorPublishFeatureCreateSensorThrows : public SensorPublishFeature

TEST_F(SensorPublishFeatureTest, InitSensorThrows)
{
manager->eventLoop = aws_event_loop_new_default(manager->allocator, aws_high_res_clock_get_ticks);
// When a Sensor constructor throws an exception,
// then the entry is not added to the list of sensors.
config.sensorPublish.settings[1].enabled = false;
Expand All @@ -229,7 +225,6 @@ TEST_F(SensorPublishFeatureTest, InitSensorThrows)

TEST_F(SensorPublishFeatureTest, StartSensorSuccess)
{
manager->eventLoop = aws_event_loop_new_default(manager->allocator, aws_high_res_clock_get_ticks);
// After calling start, feature start notification is sent.
MockSensorPublishFeature feature;

Expand All @@ -244,7 +239,6 @@ TEST_F(SensorPublishFeatureTest, StartSensorSuccess)

TEST_F(SensorPublishFeatureTest, StopSensorSuccess)
{
manager->eventLoop = aws_event_loop_new_default(manager->allocator, aws_high_res_clock_get_ticks);
// After calling stop, feature stop notification is sent.
MockSensorPublishFeature feature;

Expand Down

0 comments on commit 6610e14

Please sign in to comment.