Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Memory leaks in C++

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
zile
Advanced Cheater
Reputation: 0

Joined: 11 Jul 2009
Posts: 75

PostPosted: Wed Jun 22, 2011 5:18 am    Post subject: Memory leaks in C++ Reply with quote

Its my first time using classes, and its something like this

Code:
class Stream {
private:
   unsigned char* data;
   int position;
   int len;
public:
   Stream(unsigned char* packet,int length){
      len = length;
      if (len > MAX_LENGTH || len <= 0)
         return;
      position = 0;
      this->data = packet;
   }
};


i wanna ask if c++ has garbage cleaner thing like java? will the new class created destroy itself if it isnt used anymore?

or do i HAVE to make a destructor/ or just delete the class?

mind giving me a proper way to clean this up please?
Back to top
View user's profile Send private message
Jesper
Grandmaster Cheater Supreme
Reputation: 9

Joined: 21 Feb 2007
Posts: 1156

PostPosted: Wed Jun 22, 2011 8:41 am    Post subject: Reply with quote

I think in C++ you have to do the cleaning all by yourself.
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 198

Joined: 25 Jan 2006
Posts: 8516
Location: 127.0.0.1

PostPosted: Wed Jun 22, 2011 12:53 pm    Post subject: Reply with quote

Look into smart pointers if you want things to start cleaning themselves. Boost has a lot of useful things for this kind of stuff.
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
zile
Advanced Cheater
Reputation: 0

Joined: 11 Jul 2009
Posts: 75

PostPosted: Thu Jun 23, 2011 8:02 am    Post subject: Reply with quote

so for the example above,
i tried
Code:

Stream data = new Stream(...);
delete &data;


but i got a heap error, any idea whats wrong?
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Thu Jun 23, 2011 8:07 am    Post subject: Reply with quote

Stream* data = new Stream(...);
delete data;
Back to top
View user's profile Send private message
zile
Advanced Cheater
Reputation: 0

Joined: 11 Jul 2009
Posts: 75

PostPosted: Thu Jun 23, 2011 9:01 am    Post subject: Reply with quote

edit: thanks above, it works!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites