We help IT Professionals succeed at work.

How do I delete a varible I'm returning?

pcvii
pcvii asked
on
259 Views
Last Modified: 2013-12-14
I'm trying to kill the memory leaks. One thing that bugs me though is i have 2 gbs of ram and this program has memory alloc errors and task manager says its only taking up 3mbs. I've killed some of them by deleting some of the big stuff and by making curtain variables static. Basically, I'm trying to go though trying to find anything that uses new, and put it when a delete.

Okay, my question is how do I delete ret? Or will it be freed up on it's own? It's just getting passed to another function.

This function does two things it converts rt to a char. And it cuts off when it finds 3 consecutive spaces.

I'm using VC++ 8.0 Express.

char *trim(unsigned short * rt)
{
      unsigned char i=0;
      unsigned char len = 0;
      union u {unsigned short int vi; char c;};
      union u un;
      while(!((char)rt[len] == 32 && (char)rt[len+1] == 32 && (char)rt[len+2] == 32 || (char)rt[len] == 0))
      {
            len ++;
      }
      char * ret = new char[len];
      while(i<len){
            un.vi = rt[i];
            ret[i++] = un.c;
      }
      ret[i]=0;
      return ret;
}

std::string GAMEVARS::desc(void)
{
      static const char *el = "\r\n";
      static const char *Level[5] = {LDMSG_VERYLOW,LDMSG_LOW,LDMSG_MEDIUM,LDMSG_HIGH,LDMSG_VERYHIGH};
      static const char *Bool[2] = {"False","True"};
      descbuff.clear();
      descbuff.str("");
      descbuff << trim(m_desc.Mapname)<< el << trim(m_desc.Description) << el <<
            GDMSG_HOST << m_hostedby << el << 
            GDMSG_DEATHMATCH << Bool[m_desc.Deathmatch?1:0] << el <<
            GDMSG_MAXPLAY << (unsigned short)m_maxPlayers << el << 
            GDMSG_HOLDTIME << (unsigned short)m_desc.Holdtime << el <<
            GDMSG_TIMELIMIT << m_timelimit << el <<      
            GDMSG_POWERUP << Bool[m_desc.Powerups?1:0] << el <<
            GDMSG_RECHARGE << Level[(unsigned short)m_desc.Rechargerate] << el <<
            GDMSG_LASER << Level[(unsigned short)m_desc.Laserdamage] << el <<
            GDMSG_SPECIAL << Level[(unsigned short)m_desc.Specialdamage] << el << 
            GDMSG_MISSILE << Bool[m_desc.Missilies?1:0] << el << 
            GDMSG_GRENADE << Bool[m_desc.Bombs?1:0] << el << 
            GDMSG_BOUNCY << Bool[m_desc.Bouncies?1:0] << el << 
            GDMSG_MINE << Bool[m_desc.Mines?1:0] << el << 
            GDMSG_FLAME << Bool[m_desc.Flame?1:0];
      return descbuff.str();
}
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2012
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2009

Commented:
From jkr's code : do the same for the other trim in this line :

      descbuff << mymap << el << trim(m_desc.Description) << el <<

Author

Commented:
I've been rewriting this lobby server. and the game servers send a structure of data all at once. loaded with spaces and no null terminators. I was thinking 3 spaces for now would be good for test. I can change it later if I need to. maybe I should just work my way from the end. *shrugs* :P

You guys all helped. I'll do the method jkr was suggesting for now. might change the function around to be like abrith was talking about later.

thanks.

Author

Commented:
what am i saying arith's version of the function is much nicer than mine. in it goes :P
Minimum edit fix is to use std::string as a temporary object to return from trim and to delete the temporary string allocated within trim. Cleanup for std::string gets done automatically for you and your "hand coding" is confined to the scope of trim.


std::string trim(unsigned short * rt)  
{
      unsigned char i=0;
      unsigned char len = 0;
      union u {unsigned short int vi; char c;};
      union u un;
      while(!((char)rt[len] == 32 && (char)rt[len+1] == 32 && (char)rt[len+2] == 32 || (char)rt[len] == 0))
      {
            len ++;
      }
      char * ret = new char[len];
      while(i<len){
            un.vi = rt[i];
            ret[i++] = un.c;
      }
      ret[i]=0;
//--------8<--------
//    return ret;
//--------8<--------
      std::string str(ret);
      delete []ret;
      return str;
//--------8<--------
}


A better approach would be to make ret an std::ostringstream, which you insert into in the loop, and return the std::string ret.str(). That way you loose the new and delete.

If unsigned short* is really a wchar_t*, you could even use a std::wostringstream and return a std::string, using a WideCharToMultiByte() conversion.

For what it's worth, I disagree with abith about the pointer. It is more legible to index an array than use pointer arithmetic, I reckon, and the generated code will be just as efficient with a modern compiler.


I wonder if unsigned short  is really a wchar_t*? If so

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.