LCDInfo.com
http://forum.lcdinfo.com/

Value class
http://forum.lcdinfo.com/viewtopic.php?f=17&t=481
Page 1 of 1

Author:  rudi Grobler [ Fri Jan 09, 2004 11:04 ]
Post subject:  Value class

Hi,

can you please explain to me how the Value class work

Code:
class Value
{
private:

public:
   void * buffer;
   int buffersize;
   void operator=(int v) { memcpy(buffer, &v, buffersize); }
   void operator=(double v) { memcpy(buffer, &v, buffersize); }
   void operator=(const char *v) { strncpy((char*)buffer, v, buffersize-1); /*buffer[buffersize-1]='\0';*/ }
};


When does buffersize get set?

One thing we should remember is to free memory after assigning it to a value. Let me explain via code:

Code:
char *Name( void )
{
  char *Result = new char [ 32 ] ;
  memset( Result, 0, 32 ) ;
  sprintf( Result, "Rudi Grobler" ) ;
  return Result ;
}


Now in the plug in we have a case statement that checks which variable you want and then returns it...

Code:
case 0:
  value = Name( ) ;
  break ;


This will cause a memory leak!!!

Code:
case 0:
  char *Result = Name( ) ;
  value = Result ;
  delete Result ;
  break ;


I know this might seem simple to some but I hade some problems with memory leaks and thought I should atleast share it so that a newbe dont have the same problems!!!

Author:  DarkElf [ Fri Jan 09, 2004 11:41 ]
Post subject: 

i don't konw if your solution for the memory leak work good, but i dislike it.
why?
because you do this:
Code:
function A
|  declaration of *z
|  z= name()
|  function B
|  | declaration of w
|  | w = new xxxx
|  | work on the object
|  | return the object address
|  work
|  delete z

i would have done this:
Code:
function A
|  declaration of *z
|  name(z)
|  function B(*w)
|  | w = new xxxx
|  | work on the object
|  work
|  delete z

you use cpu time for nothing by making declaration of var & return value...

Author:  rudi Grobler [ Fri Jan 09, 2004 12:11 ]
Post subject: 

Yes, I agree, your sulution is beter. My intention was not to give the best sulution. Only to explain what I did. The resone I used it like this was because I wanted to do the following:

Code:
char *Name( int Index ) ;

char buf[ 100 ] ;
sprintf( buf, "%d: %s", Inx, Name( Inx ) ) ;


The main objective of this thread was only to make sure nobody else declare a pointer with new in a function and not delete it!!!

Author:  DarkElf [ Fri Jan 09, 2004 12:16 ]
Post subject: 

yes, of course :)
but your post is strange to me, i wasn't able to determine if you were asking help or if you were giving councils

Author:  rudi Grobler [ Fri Jan 09, 2004 12:28 ]
Post subject: 

I was only trying to point out what I did wrong!!!

Author:  Henri [ Fri Jan 09, 2004 20:41 ]
Post subject: 

How the Value class works:
LCDInfo creates instance of the class and sets the buffersize correct depending of the value type. Also the *buffer pointer is set to point to correct place inside LCDInfo. Then the Value object is passed to the plugin's virtual void __stdcall getValue(int varNo, Value value) = 0; function. When value is set using = operator the amount of bytes set in buffersize gets copied to the memory space reserved inside LCDInfo.

LCDInfo sets the buffersize before asking for the value. For int and double it is sizeof(int), sizeof(double) and for strings it is set to 256.

I hope my explanation made any sense.

As you noticed it's always good to remember delete memory reserved by new. ;) Sometimes it can be easily forgotten causing nasty memory leaks.

Author:  rudi Grobler [ Mon Jan 12, 2004 8:43 ]
Post subject: 

Code:
virtual void __stdcall getValue(int varNo, Value value) = 0;


So before Value is passed to getValue, its size get set. So inside the getValue function I only see the switch statement and not the setup of Value class :?:

I was looking at some sort of constructor or something and when I didn't find it, I couldn't work out how the Value class gets set. :oops:

Thanks

Author:  Henri [ Mon Jan 12, 2004 20:31 ]
Post subject: 

rudi Grobler wrote:
So before Value is passed to getValue, its size get set. So inside the getValue function I only see the switch statement and not the setup of Value class :?:

Yes.

Page 1 of 1 All times are UTC + 2 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/