Toon posts:

[C++] Detected memory leaks

Pagina: 1
Acties:
  • 49 views sinds 30-01-2008

Verwijderd

Topicstarter
Ik gebruik dus hetvolgende voor memleaks te detecten:
C++:
1
2
3
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>


Dan in het begin van mijn programma doe ik:
C++:
1
  _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );


Ik heb een programma gemaakt:

C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  test_t e;

  KString a;
  KString b(a);
  KString c("tmp");
  KString d(c);

  int *i;

  MessageBox(NULL, __FILE__, "C++",MB_OK);


  i = new int;
  i = new int;

  b=d;
  c=a;

  a="test";

  e.testv = b;

  return 0;


zoals je kan zien is "i = new int" een memleak... Die detecteert het perfect. (dit was slechts een test dus).


Maar in een ander programma (dll) van mij, daar krijg ik zo van die rare fouten zoals:
code:
1
2
3
4
5
6
7
8
Detected memory leaks!
Dumping objects ->
{85} normal block at 0x0CA90068, 90112 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
{84} normal block at 0x0B0816E0, 10 bytes long.
 Data: <weapon_m3 > 77 65 61 70 6F 6E 5F 6D 33 00 
{83} normal block at 0x0B081720, 8 bytes long.
 Data: <shotgun > 73 68 6F 74 67 75 6E 00


Wat MSDN daarover zegt is:
Programma stoppen op het begin, en dan een watch zetten op _crtBreakAlloc, dan de waarde modifien naar ... (85 bvb in dit geval).

Dan breakt mijn debugger in assembly mode. (hij vraagt achter crtheap.c [cancel])
een paar keer op shift f11 (jump out function) en hij vraagt achter strdup.c [cancel]
en terug debug-assembly. trug een paar keer op shift f11 en ik zit terug in mijn gewone code waar ik een _strdup( ) van een const char* heb gedaan.

Deze memleaks komen voor bij mijn zelfgemaakte string-class... Maar ze komen NIET voor in mijn andere programmaatje?

Nog even wat uitgebreidere informatie:
C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
typedef struct weapon_info_s {
  KString       pszName;     //                           STRING(pev->classname);

  KString       pszAmmo1;    // ammo 1 name               ".50mm"
  int           iMaxAmmo1;   // max ammo 1                35

  KString       pszAmmo2;    // ammo 2 type               NULL
  int           iMaxAmmo2;   // max ammo 2                -1

  int           iMaxClip;    // max amount in 1 clip      7
  int           m_iDefaultAmmo; // default ammo dat gegeven word (meestal = iMaxClip)

  int           iFlags;      // ITEM_FLAG_SELECTONEMPTY, ITEM_FLAG_NOAUTORELOAD, ITEM_FLAG_NOAUTOSWITCHEMPTY, ITEM_FLAG_LIMITINWORLD, ITEM_FLAG_EXHAUSTIBLE

  int           iSlot;       // Welk menutje? (bvb in CS: 0 = guns, 1 = pistols, 2 = knife, 3 = grenade, 4 = C4)

  int           iPosition;   // positie in het menu...
  int           iId;         // algemeen ID in het spel!
  int           iWeight;     // this value used to determine this weapon's importance in autoselection.

  // modelnames
  KString       p_model;     // how other people see this gun @ me
  KString       v_model;     // how I see the gun
  KString       w_model;     // how it looks like when it's dropped

  KString       sounds[MAX_SOUNDS];

  unsigned short m_usEvent;   // need to be precached!!! ( = m_usNormal for normal guns )

  float         f_NextShoot;  // Next time we may use our primary fire
  float         f_NextShoot2; // Next time we may use our secondary fire
  float         f_HolsterTime;// deploy/holster delay
  float         f_ReloadTime; // How long it takes to reload the gun
  float         f_FireEmptyDelay; // If you fire this gun when it's empty, how long does it takes for the next 'tek' to be send?
  float         f_PumpTime; // we need this for shotgun & snipers (how much time between the shots?)
  float         f_WeaponIdleNonEmpty; // time until we encounter the WeaponIdle function when our gun IS NOT empty
  float         f_WeaponIdleEmpty; // time until we encounter the WeaponIdle function when our gun IS empty

  Vector        f_MySpread;   // How much the bullets spread out

  int           anim_draw;    // drawing   animation
  int           anim_reload;  // reloading animation
  int           anim_idle;    // idle      animation

  KString       szAnimExt;      // onehanded, shotgun ... etc

  int           skiplocal;      // skip local?
  int           body;           // which body should be shown?

  int           m_iDamage;         // damage the gun does
  Bullet        m_eBulletType;     // type of the bullet (as defined in 'weapons.h')
  int           m_iGunVolume;      // LOUD_GUN_VOLUME, NORMAL_GUN_VOLUME, QUIET_GUN_VOLUME
  int           m_iGunFlash;       // BRIGHT_GUN_FLASH, NORMAL_GUN_FLASH, DIM_GUN_FLASH
  int           m_iPelletsATime;   // for shotguns... How much pellets are fired at once?
  int           m_iBulletsATime;   // for glock etc... How much bullets fired a time?
  int           iRange;            // range how far the gun goes (normal: 8192, shotgun: 2048)

  bool          m_bCanFireUnderwater;
} weapon_info_t;


KString is mijn eigen klasse...

deze weapon_info_t word gebruikt in:
C++:
1
2
3
4
5
6
7
struct _BaseWeapon : public CBasePlayerWeapon
{
...
protected:
  weapon_info_t   wp;
...
};


default constructor van _BaseWeapon:
C++:
1
2
3
4
5
6
7
8
9
10
11
12
_BaseWeapon :: _BaseWeapon ( )
{
  wp.skiplocal = 0;

  m_bIsPistol = false;
  m_bIsShotgun = false;
  m_bIsSubMachineGun = false;
  m_bIsRifle = false;
  m_bIsSniper = false;
  m_bIsMachineGun = false;

}


en default constructor van een afgeleide daarvan [ struct CM3: public _BaseWeapon ]
C++:
1
2
3
4
5
6
7
CM3 :: CM3 ( )
{
  wp.sounds[0] = "weapons/m3_insertshell.wav";
  wp.sounds[1] = "weapons/m3_pump.wav";
  wp.sounds[2] = "weapons/m3-1.wav";

  wp.iRange = 2048;


default constructor van KString:
C++:
1
2
3
4
KString::KString()
{
  InitWithString( NULL, true );
}


default destructor van KString:
C++:
1
2
3
4
KString::~KString()
{
  InitWithString(NULL);
}


De functie "InitWithString":
C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
inline void KString::InitWithString( char * newptr, bool FirstTime )
{
  if ( FirstTime )
  {
    vbindex = 0;
    ignore_case = false;
  }
  else
  {
    if ( _ptr )
      delete _ptr;
  }
  _ptr = newptr;

  if ( _ptr )
    len = strlen( _ptr );
  else
    len = 0;
}


Dan misschien ook nog nodig voor dit probleem te ontdekken:
C++:
1
2
3
4
void KString::operator =( const char *invoer )
{
  InitWithString( _strdup(invoer) );
}



PS: whoami ... Is dit genoeg informatie? En een goede vraag? ^_^

  • curry684
  • Registratie: Juni 2000
  • Laatst online: 13-08 16:46

curry684

left part of the evil twins

Anders leer je zelf een keer debuggen ipv ieder moeilijkheidje op GoT te flikkeren?

Tis niet alsof iemand hier 6 pagina's uitgebreid door gaat lezen en analyseren voor je... we zijn wel goed maar niet compleet van lotje getikt.

Professionele website nodig?


Verwijderd

Deze memleaks komen voor bij mijn zelfgemaakte string-class... Maar ze komen NIET voor in mijn andere programmaatje?
Dan lijkt het me dat je in die "zelfgemaakte string-class" moet gaan kijken :)

En idd, wat curry684 hierboven zei is hier denk ik wel van toepassing ...

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 22-08 13:19

.oisyn

Moderator Devschuur®

Demotivational Speaker

Zie de reply van curry, het is natuurlijk van de zotte dat wij al je code door moeten spitten op zoek naar waar het precies fout gaat.
Een tip: misschien dat je je handen kunt leggen op een kopie van Rational's Purify? Die detecteert memory leaks voor je, en geeft precies aan in je code waar het geheugen gealloceerd is (samen met een stack trace en code weergave in geval van debug builds)

En als ik voor whoami mag antwoorden: er is genoeg info, zelfs teveel, maar je vraagstelling is te vaag en te algemeen met de grote lap code

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


Dit topic is gesloten.