Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 03:43

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Memory leak?

Discussion on Memory leak? within the C/C++ forum part of the Coders Den category.

Closed Thread
 
Old   #1
 
elite*gold: 0
Join Date: Oct 2011
Posts: 814
Received Thanks: 675
Memory leak?

Hi,

kommen wir direkt zur sache.

Ich habe eine Client Struktur und eine Serverklasse,
in der Serverklasse habe ich die Client Struktur deklariert.

Code:
Clients Client[255];
Nun läuft eine Schleife die auf Clients wartet, sollte ein Client akzeptiert werden wird ein Thread aufgerufen, dabei startet es eine Funktion meiner Recv Klasse und übergibt die Client Variable als Parameter.

Code:
CreateThread(NULL, NULL, &TRecv::Data, &this->Client[i], NULL, NULL);
So nun bei meinen Thread habe ich nen Pointer deklariert und anschließend den Parameter gecastet.

Code:
DWORD WINAPI TRecv::Data(LPVOID param)
{
          Clients *my_client = (Clients*)param;

          while(1) 
          {
              //überprüfen ob Daten zum empfangen vorhanden, etc...
          }
}
So nun zu meiner Frage,
muss das my_client wieder freigegeben werden?
Es wurde ja kein Objekt mit new erstellt.
Stehe gerade etwas auf dem Schlauch.

Möchte ungerne ein Memory leak später im Programm haben,
da es etwas größeres werden soll.

Freue mich auf eine Antwort.

mfg Trollface-
Trollface- is offline  
Old 09/08/2012, 13:13   #2


 
MrSm!th's Avatar
 
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,902
Received Thanks: 25,407
Wenn myclient nicht mit new alloziert wurde, kann er auch nicht mit delete freigegeben werden.

malloc -> free
new -> delete
nichts -> nichts

Letzteres ist bei dir der Fall, die Client Variable ist entweder auf dem Stack oder global (es ist nicht ersichtlich, ob du die Variable lokal erstellst oder sie global ist).
Im Falle, dass sie auf dem Stack liegt, musst du aber darauf achten, dass der Stack ein temporärer Speicher ist.
Wenn die Funktion, die das Objekt erstellt hat, returned und dann andere Dinge mit dem Stack geschehen, wird es überschrieben. Deshalb gibt man aus einer Funktion nie Pointer auf lokale Variablen zurück und erst recht nicht an andere Threads.
MrSm!th is offline  
Thanks
1 User
Old 09/08/2012, 13:33   #3
 
elite*gold: 0
Join Date: Oct 2011
Posts: 814
Received Thanks: 675
Quote:
Originally Posted by MrSm!th View Post
Wenn myclient nicht mit new alloziert wurde, kann er auch nicht mit delete freigegeben werden.

malloc -> free
new -> delete
nichts -> nichts

Letzteres ist bei dir der Fall, die Client Variable ist entweder auf dem Stack oder global (es ist nicht ersichtlich, ob du die Variable lokal erstellst oder sie global ist).
Im Falle, dass sie auf dem Stack liegt, musst du aber darauf achten, dass der Stack ein temporärer Speicher ist.
Wenn die Funktion, die das Objekt erstellt hat, returned und dann andere Dinge mit dem Stack geschehen, wird es überschrieben. Deshalb gibt man aus einer Funktion nie Pointer auf lokale Variablen zurück und erst recht nicht an andere Threads.
Super erklärt, vielen dank.
Damit hat es sich hier erledigt und kann geschlossen werden.
Trollface- is offline  
Old 09/08/2012, 15:19   #4


 
MrSm!th's Avatar
 
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,902
Received Thanks: 25,407
#closed
MrSm!th is offline  
Closed Thread


Similar Threads Similar Threads
[eSRO] Memory Leak
08/01/2011 - SRO Private Server - 9 Replies
Since I have been playing eSRO the client has been memory leaking really bad. Does anyone know a fix to this or know the cause of the problem? Any info would be much appreciated.
Memory Leak?
10/22/2010 - CO2 Private Server - 14 Replies
Ok guys...3rd thread about this.... Q1: Does the variables that are declared like int x = new int(); use memory if not set to null after it's no longer used? Q2:is there a way to delete a variable if it's no longer used? Q3:is it of any use to set the variables/arrays/hastables etc to null after not being used anymore? Q4:when does GC.Collect() collects the garbage (resources)? I am using GC.Collect in a thread running every 1 second and I've also set the variables to null after their no...
ZSZC SRO-DB Bot Memory Leak?
06/13/2010 - SRO Private Server - 7 Replies
I'm using the .8 one since the .9 doesn't attack. But I'm just wondering, after say half an hour or so, the games gets extremely laggy. Does anyone have this issue as well and is it just due to a memory leak. Thanks!
5127 Memory Leak
06/08/2009 - Conquer Online 2 - 13 Replies
Has anyone found a solution to the memory leak problem that started after patch 5127? My clients has been experiencing a constant increase in the amount of RAM used by Conquer since patch 5127 have been installed. After looking in CO's official forum, I am pretty sure that it has not been cause by something from my side since many players have also having the same problem.
CO Partner Memory Leak?
06/18/2006 - Conquer Online 2 - 8 Replies
Does it happen to any one else; after 5-10 mins of lvling it just ignores the mobs and random runs around.



All times are GMT +1. The time now is 03:43.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.