Skip to content

Commit

Permalink
more fix memmememememememe leak
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr.Abc committed Aug 19, 2023
1 parent 42ce439 commit b87250b
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 30 deletions.
2 changes: 2 additions & 0 deletions asexthook.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<ClCompile Include="src\source\angelscript.cpp" />
<ClCompile Include="src\source\CASBaseObject.cpp" />
<ClCompile Include="src\source\CASBinaryStringBuilder.cpp" />
<ClCompile Include="src\source\CASSQLGrid.cpp" />
<ClCompile Include="src\source\CASSQLite.cpp" />
<ClCompile Include="src\source\CASSQLItem.cpp" />
<ClCompile Include="src\source\dllapi.cpp" />
Expand All @@ -110,6 +111,7 @@
<ClInclude Include="src\header\angelscript.h" />
<ClInclude Include="src\header\CASBaseObject.h" />
<ClInclude Include="src\header\CASBinaryStringBuilder.h" />
<ClInclude Include="src\header\CASSQLGrid.h" />
<ClInclude Include="src\header\CASSQLite.h" />
<ClInclude Include="src\header\CASSQLItem.h" />
<ClInclude Include="src\header\dlldef.h" />
Expand Down
6 changes: 6 additions & 0 deletions asexthook.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
<ClCompile Include="src\source\CASSQLItem.cpp">
<Filter>source</Filter>
</ClCompile>
<ClCompile Include="src\source\CASSQLGrid.cpp">
<Filter>source</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="header">
Expand Down Expand Up @@ -104,5 +107,8 @@
<ClInclude Include="src\header\CASSQLItem.h">
<Filter>header</Filter>
</ClInclude>
<ClInclude Include="src\header\CASSQLGrid.h">
<Filter>header</Filter>
</ClInclude>
</ItemGroup>
</Project>
23 changes: 23 additions & 0 deletions src/header/CASSQLGrid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once
#include "CASBaseObject.h"
#include "CASSQLItem.h"
#include <vector>

class CBinaryStringBuilder;
class CASSQLGrid : public CASBaseGCObject{
public:
//Only for c++ not expose to as
CASSQLGrid();
~CASSQLGrid();
static CASSQLGrid* Factory(size_t row, size_t column);
void Set(size_t row, size_t column, CASSQLItem* pItem);
//Angel Scripts
CASSQLItem* Get(size_t row, size_t column);
CASSQLItem* opIndex(size_t row, size_t column);
size_t Rows();
size_t Columns();
private:
std::vector <std::vector<CASSQLItem*>> m_aryData;
size_t m_iRow;
size_t m_iColumn;
};
1 change: 0 additions & 1 deletion src/header/CASSQLItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class CASSQLItem : public CASBaseGCObject{
private:
void SetData(char* str);

asITypeInfo* m_pStrInfo;
asITypeInfo* m_pBlobInfo;
std::string m_szData;

Expand Down
6 changes: 1 addition & 5 deletions src/header/CASSQLite.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CASSQLite : public CASBaseGCObject
static void CloseSQLite3DLL();

int Open();
int ExecSync(CString* sql, void* arrayOut, CString* errMsg);
int ExecSync(CString* sql, CASSQLGrid** gridOut, CString* errMsg);
int Exec(CString* sql, CString* errMsg);
int ExecWithCallBack(CString* sql, aslScriptFunction* pfnCallBack, void* any, CString* errMsg);
void Close();
Expand All @@ -29,9 +29,5 @@ class CASSQLite : public CASBaseGCObject
std::string m_szStoredPath;
int m_iMode = 0;

asITypeInfo* m_pGripInfo;
asIScriptFunction* m_pGripInsertFunction;
asITypeInfo* m_pArrayInfo;
asIScriptFunction* m_pArrayInsertFunction;
asITypeInfo* m_pItemInfo;
};
50 changes: 50 additions & 0 deletions src/source/CASSQLGrid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "CASBinaryStringBuilder.h"
#include "CASSQLGrid.h"

CASSQLGrid::CASSQLGrid(){

}

CASSQLGrid::~CASSQLGrid(){
for (auto iter = m_aryData.begin(); iter != m_aryData.end(); iter++) {
for (auto iter2 = (*iter).begin(); iter2 != (*iter).end(); iter2++) {
(*iter2)->Release();
}
(*iter).clear();
}
m_aryData.clear();
}

CASSQLGrid* CASSQLGrid::Factory(size_t row, size_t column){
CASSQLGrid* obj = new CASSQLGrid();
obj->m_aryData.resize(row);
for (auto iter = obj->m_aryData.begin(); iter != obj->m_aryData.end(); iter++) {
(*iter).resize(column);
}
obj->m_iRow = row;
obj->m_iColumn = column;
CASServerManager* manager = ASEXT_GetServerManager();
asIScriptEngine* engine = manager->scriptEngine;
asITypeInfo* type = engine->GetTypeInfoByName("CSQLGrid");
engine->NotifyGarbageCollectorOfNewObject(obj, type);
return obj;
}

void CASSQLGrid::Set(size_t row, size_t column, CASSQLItem* pItem){
m_aryData[row][column] = pItem;
}

CASSQLItem* CASSQLGrid::Get(size_t row, size_t column){
if (row >= m_iRow || column >= m_iColumn)
return nullptr;
return m_aryData[row][column];
}
CASSQLItem* CASSQLGrid::opIndex(size_t row, size_t column){
return Get(row, column);
}
size_t CASSQLGrid::Rows(){
return m_iRow;
}
size_t CASSQLGrid::Columns(){
return m_iColumn;
}
1 change: 0 additions & 1 deletion src/source/CASSQLItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
CASSQLItem::CASSQLItem(){
CASServerManager* manager = ASEXT_GetServerManager();
asIScriptEngine* engine = manager->scriptEngine;
m_pStrInfo = engine->GetTypeInfoByName("string");
m_pBlobInfo = engine->GetTypeInfoByDecl("CBinaryStringBuilder");
m_bIsNull = true;
}
Expand Down
28 changes: 8 additions & 20 deletions src/source/CASSQLite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

#include "CASBinaryStringBuilder.h"
#include "CASSQLItem.h"
#include "CASSQLGrid.h"
#include "CASSQLite.h"


typedef int(*fnSQLite3Open)(const char* filename, sqlite3** ppDb, int flags, const char* zVfs);
fnSQLite3Open SQLite3_Open;

Expand Down Expand Up @@ -57,10 +59,6 @@ CASSQLite::CASSQLite(CString* szPath, int iMode){

CASServerManager* manager = ASEXT_GetServerManager();
asIScriptEngine* engine = manager->scriptEngine;
m_pGripInfo = engine->GetTypeInfoByDecl("array<array<CSQLItem@>>");
m_pGripInsertFunction = m_pGripInfo->GetMethodByName("insertLast");
m_pArrayInfo = engine->GetTypeInfoByDecl("array<CSQLItem@>");
m_pArrayInsertFunction = m_pArrayInfo->GetMethodByName("insertLast");
m_pItemInfo = engine->GetTypeInfoByName("CSQLite");
}
CASSQLite* CASSQLite::Factory(CString* szPath, int iMode){
Expand Down Expand Up @@ -93,7 +91,7 @@ int CASSQLite::Open(){
m_bClosed = false;
return SQLite3_Open(m_szStoredPath.c_str(), &m_pDatabase, m_iMode, nullptr);
}
int CASSQLite::ExecSync(CString* sql, void* arrayOut, CString* errMsg){
int CASSQLite::ExecSync(CString* sql, CASSQLGrid** gridOut, CString* errMsg){
if (m_bClosed)
return 999;
else if (!m_bAviliable)
Expand All @@ -106,28 +104,16 @@ int CASSQLite::ExecSync(CString* sql, void* arrayOut, CString* errMsg){
iReturn = SQLite3_GetTable(m_pDatabase, sql->c_str(), &pResult, &nRow, &nColumn, &zErrMsg);
if (iReturn != 0)
return iReturn;
CASServerManager* manager = ASEXT_GetServerManager();
asIScriptEngine* engine = manager->scriptEngine;
asIScriptContext* ctx = engine->RequestContext();
int iIndex = 0;
CASSQLGrid* grid = CASSQLGrid::Factory(nRow + 1, nColumn);
for (int i = 0; i <= nRow; i++){
void* ary = engine->CreateScriptObject(m_pArrayInfo);
for (int j = 0; j < nColumn; j++){
ctx->Prepare(m_pArrayInsertFunction);
ctx->SetObject(ary);
char* res = pResult[i * nColumn + j];
//thee shall no be null in value, but could be in sqlite
CASSQLItem* val = res == nullptr ? CASSQLItem::Factory() : CASSQLItem::ParamFactory(res);
ctx->SetArgObject(0, &val);
ctx->Execute();
iIndex++;
grid->Set(i, j, val);
}
ctx->Prepare(m_pGripInsertFunction);
ctx->SetObject(arrayOut);
ctx->SetArgObject(0, ary);
ctx->Execute();
}

*gridOut = grid;
if (zErrMsg) {
errMsg->assign(zErrMsg, strlen(zErrMsg));
SQLite3_Free(zErrMsg);
Expand Down Expand Up @@ -180,6 +166,8 @@ int CASSQLite::Sqlite3Callback(void* param[], int column_size, char* column_valu
aslScriptFunction* pfnASCallBack = static_cast<aslScriptFunction*>(param[0]);
CASFunction* m_callback = ASEXT_CreateCASFunction(pfnASCallBack, ASEXT_GetServerManager()->curModule, 1);
(*ASEXT_CallCASBaseCallable)(m_callback, 0, param[1], column_size, aryVal, aryName);
engine->ReleaseScriptObject(aryVal, aryInfo);
engine->ReleaseScriptObject(aryName, aryInfo);
return 0;
}

Expand Down
12 changes: 9 additions & 3 deletions src/source/angelscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <meta_api.h>
#include "CASBinaryStringBuilder.h"
#include "CASSQLItem.h"
#include "CASSQLGrid.h"
#include "CASSQLite.h"

angelhook_t g_AngelHook;
Expand Down Expand Up @@ -153,8 +154,6 @@ void RegisterAngelScriptMethods(){
ASEXT_RegisterEnumValue(pASDoc, "Extended result codes", "SQLiteMode", "SQLITE_OPEN_EXRESCODE", 0x02000000);
//Class
ASEXT_RegisterObjectType(pASDoc, "SQL Item", "CSQLItem", 0, asOBJ_REF | asOBJ_GC);
//reg = asFUNCTION(CASSQLItem::Factory);
//ASEXT_RegisterObjectBehaviourEx(pASDoc, "Factory", "CSQLItem", asBEHAVE_FACTORY, "CSQLItem@ CSQLItem()", &reg, asCALL_CDECL);
RegisteGCObject<CASSQLItem>(pASDoc, "CSQLItem");
REGISTE_OBJMETHODEX(reg, pASDoc, "Get string", "CSQLItem", "void Get(string&out buffer)", CASSQLItem, Get, asCALL_THISCALL);
REGISTE_OBJMETHODEX(reg, pASDoc, "Get int64", "CSQLItem", "int64 GetLong()", CASSQLItem, GetInt64, asCALL_THISCALL);
Expand All @@ -165,14 +164,21 @@ void RegisterAngelScriptMethods(){
REGISTE_OBJMETHODEX(reg, pASDoc, "Get blob", "CSQLItem", "CBinaryStringBuilder@ GetBlob()", CASSQLItem, GetBlob, asCALL_THISCALL);
REGISTE_OBJMETHODEX(reg, pASDoc, "Is null", "CSQLItem", "bool IsNull()", CASSQLItem, IsNull, asCALL_THISCALL);

ASEXT_RegisterObjectType(pASDoc, "SQL Grid", "CSQLGrid", 0, asOBJ_REF | asOBJ_GC);
RegisteGCObject<CASSQLGrid>(pASDoc, "CSQLGrid");
REGISTE_OBJMETHODEX(reg, pASDoc, "Get CSQLItem", "CSQLGrid", "CSQLItem@ Get(uint row, uint column)", CASSQLGrid, Get, asCALL_THISCALL);
REGISTE_OBJMETHODEX(reg, pASDoc, "Get CSQLItem", "CSQLGrid", "CSQLItem@ opIndex(uint row, uint column)", CASSQLGrid, Get, asCALL_THISCALL);
REGISTE_OBJMETHODEX(reg, pASDoc, "Get Rows", "CSQLGrid", "uint Rows()", CASSQLGrid, Rows, asCALL_THISCALL);
REGISTE_OBJMETHODEX(reg, pASDoc, "Get Columns", "CSQLGrid", "uint Columns()", CASSQLGrid, Columns, asCALL_THISCALL);

ASEXT_RegisterFuncDef(pASDoc, "SQLite Callback", "void fnSQLiteCallback(any@ pParam, int iColumnSize, array<CSQLItem@>@ aryColumnValue, array<CSQLItem@>@ aryColumnName)");

ASEXT_RegisterObjectType(pASDoc, "SQLite", "CSQLite", 0, asOBJ_REF | asOBJ_GC);
reg = asFUNCTION(CASSQLite::Factory);
ASEXT_RegisterObjectBehaviourEx(pASDoc, "Factory", "CSQLite", asBEHAVE_FACTORY, "CSQLite@ CSQLite(string&in path, SQLiteMode iMode)", &reg, asCALL_CDECL);
RegisteGCObject<CASSQLite>(pASDoc, "CSQLite");
REGISTE_OBJMETHODEX(reg, pASDoc, "Excute SQL", "CSQLite", "SQLiteResult Exec(string&in sql, string&out errMsg)", CASSQLite, Exec, asCALL_THISCALL);
REGISTE_OBJMETHODEX(reg, pASDoc, "Excute SQL In Sync", "CSQLite", "SQLiteResult Exec(string&in sql, array<array<CSQLItem@>>&out aryResult, string&out errMsg)", CASSQLite, ExecSync, asCALL_THISCALL);
REGISTE_OBJMETHODEX(reg, pASDoc, "Excute SQL In Sync", "CSQLite", "SQLiteResult Exec(string&in sql, CSQLGrid@&out aryResult, string&out errMsg)", CASSQLite, ExecSync, asCALL_THISCALL);
REGISTE_OBJMETHODEX(reg, pASDoc, "Excute SQL", "CSQLite", "SQLiteResult Exec(string&in sql, fnSQLiteCallback@ pCallback, any@ pCallBackparam, string&out errMsg)", CASSQLite, ExecWithCallBack, asCALL_THISCALL);
REGISTE_OBJMETHODEX(reg, pASDoc, "Close SQL", "CSQLite", "void Close()", CASSQLite, Close, asCALL_THISCALL);

Expand Down

0 comments on commit b87250b

Please sign in to comment.