增加自动释放资源功能

This commit is contained in:
a158
2026-05-07 23:49:19 +08:00
parent db4e2199b4
commit bf65dbb9f7
7 changed files with 86 additions and 2 deletions

View File

@@ -22,6 +22,7 @@ If you have any questions, please contact us: 1585346868@qq.com Or visit our web
#include "module/http/request.h"
#include "module/http/response.h"
#include "net/http_subscribe.h"
#include "module/request_temp_ptr.h"
fastweb::subscribe_manager::subscribe_manager(fastweb::app* app) :Interface(app)
{
}
@@ -62,6 +63,9 @@ bool fastweb::subscribe_manager::callback(network::http::request* request, netwo
bool ok_continue = false;
auto lua = app()->state->get();
std::string exception_string;
// 临时指针管理(待释放资源)
module::request_temp_ptr request_temp_ptr;
try
{
sol::load_result script = lua->state->load_file(app()->config->website.dir + filepath);
@@ -85,7 +89,10 @@ bool fastweb::subscribe_manager::callback(network::http::request* request, netwo
exception_string = e.what();
if (app()->config->website.debug)
LOG_ERROR("[subscribe][" + request->filepath() + "]: " + e.what());
}
}
// 释放临时指针
request_temp_ptr.clear();
// 释放lua资源
lua->state->collect_garbage();
app()->state->push(lua);

View File

@@ -22,8 +22,13 @@ namespace module
/// <summary>
/// 全局变量阶段获取自身指针
/// </summary>
/// <returns></returns>
/// <returns></returns>
virtual void* self() { return this; }
/// <summary>
/// 释放自身(非指针释放非lua引用释放仅释放内部资源)
/// </summary>
virtual void self_free() = 0;
};
}

View File

@@ -29,6 +29,7 @@ namespace module
// 通过 imodule 继承
virtual void regist_global(const char* name, sol::state* lua);
virtual void delete_global() { delete this; }
virtual void self_free() { }
std::mutex m_mutex;
};

View File

@@ -15,6 +15,7 @@ namespace module
static void regist(sol::state* lua);
virtual void regist_global(const char* name, sol::state* lua) override;
virtual void delete_global() { delete this; }
virtual void self_free() {}
private:
ylib::queue<std::string> m_queue;
};

View File

@@ -0,0 +1,47 @@
/*Software License
Copyright(C) 2024[liuyingjie]
License Terms
Usage Rights
Any individual or entity is free to use, copy, and distribute the binary form of this software without modification to the source code, without the need to disclose the source code.
If the source code is modified, the modifications must be open - sourced under the same license.This means that the modifications must be disclosed and accompanied by a copy of this license.
Future Versions Updates
From this version onwards, all future releases will be governed by the terms of the latest version of the license.This license will automatically be nullified and replaced by the new version.
Users must comply with the terms of the new license issued in future releases.
Liability and Disclaimer
This software is provided “as is”, without any express or implied warranties, including but not limited to the warranties of merchantability, fitness for a particular purpose, and non - infringement.In no event shall the author or copyright holder be liable for any claims, damages, or other liabilities, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software.
Contact Information
If you have any questions, please contact us: 1585346868@qq.com Or visit our website fwlua.com.
*/
#include "request_temp_ptr.h"
module::request_temp_ptr::request_temp_ptr()
{
}
module::request_temp_ptr::~request_temp_ptr()
{
}
void module::request_temp_ptr::push(void* value)
{
m_queue.push(value);
}
void module::request_temp_ptr::clear()
{
while (m_queue.size() > 0)
{
auto base = (module::base*)m_queue.front();
m_queue.pop();
base->self_free();
}
}
void module::request_temp_ptr::regist(sol::state* lua)
{
lua->new_usertype<module::request_temp_ptr>("fw_request_temp_ptr",
"new", sol::constructors<module::request_temp_ptr()>(),
"push", &module::request_temp_ptr::push,
"clear", &module::request_temp_ptr::clear
);
}

View File

@@ -0,0 +1,22 @@
#pragma once
#include "sol/sol.hpp"
#include "module/basemodule.h"
#include <queue>
namespace module
{
class request_temp_ptr{
public:
request_temp_ptr();
~request_temp_ptr();
void push(void* value);
void clear();
static void regist(sol::state* lua);
private:
std::queue<void*> m_queue;
};
}

View File

@@ -39,6 +39,7 @@ namespace module
static void regist(sol::state* lua);
virtual void regist_global(const char* name, sol::state* lua) override;
virtual void delete_global() { delete this; }
virtual void self_free() { }
private:
virtual bool run();