LCDInfo.com

http://www.lcdinfo.com
It is currently Sat Aug 19, 2017 1:57

All times are UTC + 2 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Value class
PostPosted: Fri Jan 09, 2004 11:04 
Offline

Joined: Tue Dec 02, 2003 12:03
Posts: 107
Location: South Africa
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!!!

_________________
Rudi Grobler


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2004 11:41 
Offline

Joined: Tue Feb 18, 2003 12:33
Posts: 118
Location: Angers, France
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...

_________________
Shadows move where light should be


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2004 12:11 
Offline

Joined: Tue Dec 02, 2003 12:03
Posts: 107
Location: South Africa
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!!!

_________________
Rudi Grobler


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2004 12:16 
Offline

Joined: Tue Feb 18, 2003 12:33
Posts: 118
Location: Angers, France
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

_________________
Shadows move where light should be


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2004 12:28 
Offline

Joined: Tue Dec 02, 2003 12:03
Posts: 107
Location: South Africa
I was only trying to point out what I did wrong!!!

_________________
Rudi Grobler


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2004 20:41 
Offline

Joined: Sun May 05, 2002 22:05
Posts: 2063
Location: Lappeenranta, Finland
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.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2004 8:43 
Offline

Joined: Tue Dec 02, 2003 12:03
Posts: 107
Location: South Africa
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

_________________
Rudi Grobler


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2004 20:31 
Offline

Joined: Sun May 05, 2002 22:05
Posts: 2063
Location: Lappeenranta, Finland
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC + 2 hours


Who is online

Users browsing this forum: No registered users and 1 guest


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

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group