memory leak fix, now uses less than 5MB of memory

This commit is contained in:
Anonymous275
2020-07-04 15:58:53 +03:00
parent 69362b2dfd
commit 83c095ba17
3 changed files with 17 additions and 16 deletions

View File

@@ -13,8 +13,11 @@ void STCPSend(Client*c,std::any Data,size_t Size){
if(std::string(Data.type().name()).find("string") != std::string::npos){ if(std::string(Data.type().name()).find("string") != std::string::npos){
auto data = std::any_cast<std::string>(Data); auto data = std::any_cast<std::string>(Data);
BytesSent = send(c->GetTCPSock(), data.c_str(), data.size(), 0); BytesSent = send(c->GetTCPSock(), data.c_str(), data.size(), 0);
data.clear();
}else{ }else{
BytesSent = send(c->GetTCPSock(), std::any_cast<char*>(Data), Size, 0); char* D = std::any_cast<char*>(Data);
BytesSent = send(c->GetTCPSock(), D, Size, 0);
delete[] D;
} }
if (BytesSent == 0){ if (BytesSent == 0){
std::cout << "(STCPS) Connection closing..." << std::endl; std::cout << "(STCPS) Connection closing..." << std::endl;
@@ -38,28 +41,25 @@ void SendFile(Client*c,const std::string&Name){
f.seekg(0, std::ios_base::end); f.seekg(0, std::ios_base::end);
std::streampos fileSize = f.tellg(); std::streampos fileSize = f.tellg();
size_t Size = fileSize,Sent = 0,Diff; size_t Size = fileSize,Sent = 0,Diff;
char* Data = new char[Size]; char* Data;
f.seekg(0, std::ios_base::beg);
f.read(Data, fileSize);
f.close();
char* Chunk;
int Split = 64000; int Split = 64000;
while(c->GetStatus() > -1 && Sent < Size){ while(c->GetStatus() > -1 && Sent < Size){
Diff = Size - Sent; Diff = Size - Sent;
if(Diff > Split){ if(Diff > Split){
Chunk = new char[Split]; Data = new char[Split];
memcpy_s(Chunk,Split,Data+Sent,Split); f.seekg(Sent, std::ios_base::beg);
STCPSend(c,Chunk,Split); f.read(Data, Split);
STCPSend(c,Data,Split);
Sent += Split; Sent += Split;
}else{ }else{
Chunk = new char[Diff]; Data = new char[Diff];
memcpy_s(Chunk,Diff,Data+Sent,Diff); f.seekg(Sent, std::ios_base::beg);
STCPSend(c,Chunk,Diff); f.read(Data, Diff);
STCPSend(c,Data,Diff);
Sent += Diff; Sent += Diff;
} }
} }
delete[] Data; f.close();
delete[] Chunk;
} }
void Parse(Client*c,char*data){ void Parse(Client*c,char*data){
@@ -103,6 +103,7 @@ bool STCPRecv(Client*c){
memcpy_s(Ret,BytesRcv,buf,BytesRcv); memcpy_s(Ret,BytesRcv,buf,BytesRcv);
ZeroMemory(buf, len); ZeroMemory(buf, len);
Parse(c,Ret); Parse(c,Ret);
delete[] Ret;
return true; return true;
} }
void SyncResources(Client*c){ void SyncResources(Client*c){

View File

@@ -60,7 +60,7 @@ void Check(Sequence* S){
int Max(){ int Max(){
int M = MaxPlayers; int M = MaxPlayers;
for(Client*c : Clients){ for(Client*c : Clients){
if(c->GetRole() == "MDEV")M--; if(c->GetRole() == "MDEV")M++;
} }
return M; return M;
} }

View File

@@ -15,7 +15,7 @@ void ParseConfig();
void addToLog(const std::string& Data); void addToLog(const std::string& Data);
//void ServerMain(int Port, int MaxClients); //void ServerMain(int Port, int MaxClients);
void HeartbeatInit(); void HeartbeatInit();
std::string ServerVersion = "0.46"; std::string ServerVersion = "0.47";
std::string ClientVersion = "1.46"; std::string ClientVersion = "1.46";
std::string CustomIP; std::string CustomIP;
void HandleResources(std::string path); void HandleResources(std::string path);