Quick question about wxLog usage in socketserver example. Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
koekhaos
Earned a small fee
Earned a small fee
Posts: 17
Joined: Tue Jun 13, 2017 7:55 pm

Quick question about wxLog usage in socketserver example.

Post by koekhaos »

I've been messing around with server.cpp in the socket/server example in the codebase and came across a c++ syntax that just looked really strange to me.

Code: Select all

delete wxLog::SetActiveTarget(new wxLogTextCtrl(m_text));
I found this code in the constructor. Why would you delete something at the same time you create it? Is it that the function tells the m_text what it needs to do and then wxLogTextCtrl isn't needed anymore so delete it as soon as you use it? I've just never seen delete used in this way before so it's left me a bit curious and confused.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Quick question about wxLog usage in socketserver example.

Post by doublemax »

wxLog::SetActiveTarget returns the previously active log target. This line sets a new log target and deletes the old one.
Use the source, Luke!
koekhaos
Earned a small fee
Earned a small fee
Posts: 17
Joined: Tue Jun 13, 2017 7:55 pm

Re: Quick question about wxLog usage in socketserver example.

Post by koekhaos »

Ok thanks, that makes sense. I'm still a bit confused in this context though since it is the only mention of creating the log in the whole file. Does wxWidgets automatically create a wxLog target during initialization? If so is this something that should be done in every app? Are there other objects created that aren't used like this that could be cleaned up to take up less memory? If not, then this makes little sense to me. Thanks again for the answers!
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Quick question about wxLog usage in socketserver example.

Post by doublemax »

If there is no previous log target, wxLog::SetActiveTarget will return NULL. And it's safe to call delete with a NULL pointer.
Use the source, Luke!
koekhaos
Earned a small fee
Earned a small fee
Posts: 17
Joined: Tue Jun 13, 2017 7:55 pm

Re: Quick question about wxLog usage in socketserver example.

Post by koekhaos »

So just a standard way that some people use it, maybe best practice? Very neat to learn! Thanks again Doublemax!
Post Reply