Freelancer Community Network
Reminder: Internet Explorer 6 or below are NOT supported.
HomeHome
ForumForum
WikiWiki
DownloadsDownloads
ForgeForge
Multiplayer Connection Tutorial
Collapse/Expand Random Image
Collapse/Expand Login
Username:

Password:

Remember me



Lost Password?

Register now!
Collapse/Expand Chat
Collapse/Expand Who's Online
60 user(s) are online (32 user(s) are browsing Forum)

Members: 0
Guests: 60

more...
Collapse/Expand Donations
Monthly costs: -30€
Income (ads): +5€
Donations (last month): +165€

Current balance: 105€
(last updated 02/2021)

Please make a donation if you want to help keeping The-Starport online:

Thanks!
Collapse/Expand Links
Collapse/Expand Advertisement
There are currently 87 users playing Freelancer on 41 servers.
February. 26, 2021

Browsing this Thread:   1 Anonymous Users



 Bottom   Previous Topic   Next Topic  Register To Post



n00b's programming question
Home away from home
Joined:
2009/7/30 4:40
From P.R.C
Group:
Registered Users
Senior Members
Posts: 849
Offline
The story is I'm acutally making a FLHook plugin.

In the plugin, I have a group of class (Let's say, derived class) which shared same Base class. And a Manager class used to control and send event one by one to those derived classes with method call.

Code like this:
https://gist.github.com/raincious/6b786a9c7f9e4fa6c0b3

My question is:

1, Can it be done? Notice I each derived class can have different size and method, And the Base class may have unvirtual methods.
2, When I try to delete, will be memory which assigned to derived classes can be completely release (memory size is different to Base class)?

I'm new to C++, so new .... that I can't know if I truly need a shared_ptr or not ....

Please help, Thank you!

Posted on: 2015/1/18 5:32
Sorry for my poor english...
Top
Re: n00b's programming question
Just can't stay away
Joined:
2008/6/16 20:41
Group:
Registered Users
FLServer Admins
Trusted Speciality Developers
Posts: 312
Offline
If your Base class has at least one virtual member function, make the base class destructor virtual. The result is undefined when you delete a pointer of a base class, pointing to one of the derived classes.

Also for iterators its better to have a pre increment rather than a post increment.

And use initializer lists for classes rather than to assignment in the constructors body.

Posted on: 2015/1/18 9:20
Open in new window
Top
Re: n00b's programming question
Home away from home
Joined:
2009/7/30 4:40
From P.R.C
Group:
Registered Users
Senior Members
Posts: 849
Offline
Quote:

Huor wrote:
If your Base class has at least one virtual member function, make the base class destructor virtual. The result is undefined when you delete a pointer of a base class, pointing to one of the derived classes.

Also for iterators its better to have a pre increment rather than a post increment.

And use initializer lists for classes rather than to assignment in the constructors body.


OK, now I have virtual destructor and pre increment iterator (I found this after readed your reply: http://stackoverflow.com/questions/13 ... een-iterator-and-iterator).

So I still have Undefined Behaviour problems even after 'correct' delete? How to solve that problem? Or I can only go with smart pointer (The shared_ptr)?

Posted on: 2015/1/18 10:09
Sorry for my poor english...
Top
Re: n00b's programming question
Just can't stay away
Joined:
2008/6/16 20:41
Group:
Registered Users
FLServer Admins
Trusted Speciality Developers
Posts: 312
Offline
you should not have, make sure you have a virtual destructor in all upper base classes available, so when you have derived1 and derived2 and derived inherits from derived1 then you also need a virtual destructor in derived1 and so on and so forth.

I am not so familiar with shared_prt but in principal you could use them too. But even shared_ptr wont solve the problem of eventual non-virtual destructors. It only takes over destructing if you forget to destruct your objects (as far as i know).

Posted on: 2015/1/18 10:44
Open in new window
Top
Re: n00b's programming question
Home away from home
Joined:
2009/7/30 4:40
From P.R.C
Group:
Registered Users
Senior Members
Posts: 849
Offline
OK, I will carefully go with raw pointer and "virtual destructor" every upper classes too.

Thank you Huor.

Posted on: 2015/1/18 12:01
Sorry for my poor english...
Top
Re: n00b's programming question
Starport Admin
Joined:
2009/2/21 21:42
Group:
Webmasters
Registered Users
Posts: 3520
Offline
Naked pointers is asking for trouble. Get a C++11 compliant compiler (VS2012 and up) and use unique_ptr.

Posted on: 2015/1/18 15:49
"Cynicism is not realistic and tough. It's unrealistic and kind of cowardly because it means you don't have to try."
-Peggy Noonan
Top
Re: n00b's programming question
Home away from home
Joined:
2009/7/30 4:40
From P.R.C
Group:
Registered Users
Senior Members
Posts: 849
Offline
Quote:

FriendlyFire wrote:
Naked pointers is asking for trouble. Get a C++11 compliant compiler (VS2012 and up) and use unique_ptr.


Wow, I learned a lot today LOL.

Yeah, I think unique_ptr is better since I don't need copy them.

And looks I also don't need to care about class slicing because memory will be erased by internal magic stuffs itself. (Is I'm right?)

OK, I will change code and go with smart pointer.

Thank you FF.

Posted on: 2015/1/18 16:37
Sorry for my poor english...
Top
Re: n00b's programming question
Starport Admin
Joined:
2009/2/21 21:42
Group:
Webmasters
Registered Users
Posts: 3520
Offline
Yeah, if you use a unique_ptr the memory will be released and the destructors called as soon as the pointer gets out of scope. This will be the case even if there's an exception somewhere, whereas an explicit delete call might be skipped due to an exception.

Posted on: 2015/1/18 18:28
"Cynicism is not realistic and tough. It's unrealistic and kind of cowardly because it means you don't have to try."
-Peggy Noonan
Top