Skip to content

Commit

Permalink
内存泄漏检测优化全局变量避免误报(continue)
Browse files Browse the repository at this point in the history
  • Loading branch information
fasiondog committed Apr 5, 2020
1 parent c76edbf commit 59bdac3
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 120
ColumnLimit: 100
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
Expand Down
5 changes: 3 additions & 2 deletions hikyuu_cpp/hikyuu/GlobalInitializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ void GlobalInitializer::init() {
}

void GlobalInitializer::clean() {
DataDriverFactory::release();
StockManager::quit();

H5close();
DataDriverFactory::release();

//H5close();

spdlog::drop_all();

Expand Down
12 changes: 10 additions & 2 deletions hikyuu_cpp/hikyuu/data_driver/DataDriverFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "kdata/hdf5/H5KDataDriver.h"
#include "kdata/mysql/MySQLKDataDriver.h"
#include "kdata/tdx/TdxKDataDriver.h"
#include "kdata/hdf5/H5KDataDriver.h"
#include "DataDriverFactory.h"
#include "KDataDriver.h"

Expand Down Expand Up @@ -131,6 +130,15 @@ void DataDriverFactory::removeKDataDriver(const string& name) {

KDataDriverPtr DataDriverFactory::getKDataDriver(const Parameter& params) {
KDataDriverPtr result;
string name = params.get<string>("type");
to_upper(name);
auto iter = m_kdataDrivers->find(name);
if (iter != m_kdataDrivers->end()) {
result = iter->second;
result->init(params);
}
return result;
/*KDataDriverPtr result;
auto param_iter = m_param_kdataDrivers->find(params);
if (param_iter != m_param_kdataDrivers->end()) {
result = param_iter->second;
Expand All @@ -152,7 +160,7 @@ KDataDriverPtr DataDriverFactory::getKDataDriver(const Parameter& params) {
(*m_param_kdataDrivers)[params] = result;
}
return result;
return result;*/
}

} /* namespace hku */
28 changes: 15 additions & 13 deletions hikyuu_cpp/hikyuu/data_driver/KDataDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ HKU_API std::ostream& operator<<(std::ostream& os, const KDataDriverPtr& driver)
return os;
}

KDataDriver::KDataDriver() : m_name("") {}
KDataDriver::KDataDriver() : m_name(""), m_inited(false) {}

KDataDriver::KDataDriver(const string& name) : m_name(name) {
KDataDriver::KDataDriver(const string& name) : m_name(name), m_inited(false) {
to_upper(m_name);
}

Expand All @@ -52,6 +52,10 @@ bool KDataDriver::checkType() {
}

bool KDataDriver::init(const Parameter& params) {
if (m_inited) {
return m_inited;
}

if (m_params == params) {
return true;
}
Expand All @@ -61,34 +65,32 @@ bool KDataDriver::init(const Parameter& params) {
return false;
}

return _init();
m_inited = _init();
return m_inited;
}

void KDataDriver::loadKData(const string& market, const string& code, KQuery::KType kType,
size_t start_ix, size_t end_ix, KRecordListPtr out_buffer) {}
void KDataDriver::loadKData(const string& market, const string& code, KQuery::KType kType, size_t start_ix,
size_t end_ix, KRecordListPtr out_buffer) {}

size_t KDataDriver::getCount(const string& market, const string& code, KQuery::KType kType) {
return 0;
}

bool KDataDriver::getIndexRangeByDate(const string& market, const string& code, const KQuery& query,
size_t& out_start, size_t& out_end) {
bool KDataDriver::getIndexRangeByDate(const string& market, const string& code, const KQuery& query, size_t& out_start,
size_t& out_end) {
return false;
}

KRecord KDataDriver::getKRecord(const string& market, const string& code, size_t pos,
KQuery::KType kType) {
KRecord KDataDriver::getKRecord(const string& market, const string& code, size_t pos, KQuery::KType kType) {
return Null<KRecord>();
}

TimeLineList KDataDriver ::getTimeLineList(const string& market, const string& code,
const KQuery& query) {
TimeLineList KDataDriver ::getTimeLineList(const string& market, const string& code, const KQuery& query) {
HKU_INFO("This driver not support time-line!");
return TimeLineList();
}

TransList KDataDriver ::getTransList(const string& market, const string& code,
const KQuery& query) {
TransList KDataDriver ::getTransList(const string& market, const string& code, const KQuery& query) {
HKU_INFO("This driver not support transaction record!");
return TransList();
}
Expand Down
15 changes: 7 additions & 8 deletions hikyuu_cpp/hikyuu/data_driver/KDataDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class HKU_API KDataDriver {
* @param end_ix 欲加载的结束位置,不包含自身
* @param out_buffer [out] 缓存指针
*/
virtual void loadKData(const string& market, const string& code, KQuery::KType kType,
size_t start_ix, size_t end_ix, KRecordListPtr out_buffer);
virtual void loadKData(const string& market, const string& code, KQuery::KType kType, size_t start_ix,
size_t end_ix, KRecordListPtr out_buffer);

/**
* 获取指定类型的K线数据量
Expand All @@ -69,8 +69,8 @@ class HKU_API KDataDriver {
* @param out_end [out] 对应的K线记录位置
* @return
*/
virtual bool getIndexRangeByDate(const string& market, const string& code, const KQuery& query,
size_t& out_start, size_t& out_end);
virtual bool getIndexRangeByDate(const string& market, const string& code, const KQuery& query, size_t& out_start,
size_t& out_end);

/**
* 获取指定的K线记录
Expand All @@ -80,8 +80,7 @@ class HKU_API KDataDriver {
* @param kType K线类型
* @return
*/
virtual KRecord getKRecord(const string& market, const string& code, size_t pos,
KQuery::KType kType);
virtual KRecord getKRecord(const string& market, const string& code, size_t pos, KQuery::KType kType);

/**
* 获取分时线
Expand All @@ -90,8 +89,7 @@ class HKU_API KDataDriver {
* @param query 查询条件
* @return
*/
virtual TimeLineList getTimeLineList(const string& market, const string& code,
const KQuery& query);
virtual TimeLineList getTimeLineList(const string& market, const string& code, const KQuery& query);

/**
* 获取历史分笔数据
Expand All @@ -107,6 +105,7 @@ class HKU_API KDataDriver {

private:
string m_name;
bool m_inited; //是否已初始化
};

typedef shared_ptr<KDataDriver> KDataDriverPtr;
Expand Down
42 changes: 17 additions & 25 deletions hikyuu_cpp/hikyuu/data_driver/kdata/hdf5/H5KDataDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,54 +20,46 @@ class HKU_API H5KDataDriver : public KDataDriver {
virtual ~H5KDataDriver();

virtual bool _init() override;
virtual void loadKData(const string& market, const string& code, KQuery::KType kType,
size_t start_ix, size_t end_ix, KRecordListPtr out_buffer) override;
virtual void loadKData(const string& market, const string& code, KQuery::KType kType, size_t start_ix,
size_t end_ix, KRecordListPtr out_buffer) override;
virtual size_t getCount(const string& market, const string& code, KQuery::KType kType) override;
virtual bool getIndexRangeByDate(const string& market, const string& code, const KQuery& query,
size_t& out_start, size_t& out_end) override;
virtual KRecord getKRecord(const string& market, const string& code, size_t pos,
KQuery::KType kType) override;
virtual TimeLineList getTimeLineList(const string& market, const string& code,
const KQuery& query) override;
virtual TransList getTransList(const string& market, const string& code,
const KQuery& query) override;
virtual bool getIndexRangeByDate(const string& market, const string& code, const KQuery& query, size_t& out_start,
size_t& out_end) override;
virtual KRecord getKRecord(const string& market, const string& code, size_t pos, KQuery::KType kType) override;
virtual TimeLineList getTimeLineList(const string& market, const string& code, const KQuery& query) override;
virtual TransList getTransList(const string& market, const string& code, const KQuery& query) override;

private:
void H5ReadRecords(H5::DataSet&, hsize_t, hsize_t, void*);
void H5ReadIndexRecords(H5::DataSet&, hsize_t, hsize_t, void*);
void H5ReadTimeLineRecords(H5::DataSet&, hsize_t, hsize_t, void*);
void H5ReadTransRecords(H5::DataSet&, hsize_t, hsize_t, void*);

bool _getH5FileAndGroup(const string& market, const string& code, KQuery::KType kType,
H5FilePtr& out_file, H5::Group& out_group);
bool _getH5FileAndGroup(const string& market, const string& code, KQuery::KType kType, H5FilePtr& out_file,
H5::Group& out_group);

KRecord _getBaseRecord(const string&, const string&, size_t, KQuery::KType);
KRecord _getOtherRecord(const string&, const string&, size_t, KQuery::KType);

void _loadBaseData(const string& market, const string& code, KQuery::KType kType,
size_t start_ix, size_t end_ix, KRecordListPtr out_buffer);
void _loadIndexData(const string& market, const string& code, KQuery::KType kType,
size_t start_ix, size_t end_ix, KRecordListPtr out_buffer);
void _loadBaseData(const string& market, const string& code, KQuery::KType kType, size_t start_ix, size_t end_ix,
KRecordListPtr out_buffer);
void _loadIndexData(const string& market, const string& code, KQuery::KType kType, size_t start_ix, size_t end_ix,
KRecordListPtr out_buffer);

bool _getBaseIndexRangeByDate(const string&, const string&, const KQuery&, size_t& out_start,
size_t& out_end);
bool _getOtherIndexRangeByDate(const string&, const string&, const KQuery&, size_t& out_start,
size_t& out_end);
bool _getBaseIndexRangeByDate(const string&, const string&, const KQuery&, size_t& out_start, size_t& out_end);
bool _getOtherIndexRangeByDate(const string&, const string&, const KQuery&, size_t& out_start, size_t& out_end);

TimeLineList _getTimeLine(const string& market, const string& code, int64 start, int64 end);
TimeLineList _getTimeLine(const string& market, const string& code, const Datetime& start,
const Datetime& end);
TimeLineList _getTimeLine(const string& market, const string& code, const Datetime& start, const Datetime& end);

TransList _getTransList(const string& market, const string& code, int64 start, int64 end);
TransList _getTransList(const string& market, const string& code, const Datetime& start,
const Datetime& end);
TransList _getTransList(const string& market, const string& code, const Datetime& start, const Datetime& end);

private:
H5::CompType m_h5DataType;
H5::CompType m_h5IndexType;
H5::CompType m_h5TimeLineType;
H5::CompType m_h5TransType;
H5FilePtr m_h5file;
map<string, H5FilePtr> m_h5file_map; // key: market+code
};

Expand Down
4 changes: 2 additions & 2 deletions hikyuu_cpp/unit_test/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ target("small-test")
end

-- add files
--add_files("./hikyuu/hikyuu/**.cpp");
add_files("./hikyuu/hikyuu/test_StockManager.cpp");
add_files("./hikyuu/hikyuu/**.cpp");
-- add_files("./hikyuu/hikyuu/test_StockManager.cpp");
add_files("./hikyuu/test_main.cpp")

target_end()

0 comments on commit 59bdac3

Please sign in to comment.