⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmemthread.c

📁 Extended C/C++ Dynamic Memory Control And Debug Library
💻 C
字号:
/*****************************************************************************/
/*
    XMEMTHREAD.C - Tests for extended C/C++ Dynamic Memory Control And Debug Library

    Copyright (C) Juergen Mueller (J.M.) 1987-2008
    All rights reserved.

    You are expressly prohibited from selling this software in any form
    or removing this notice.

    THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
    EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, THE
    IMPLIED WARRANTIES OF MERCHANTIBILITY, FITNESS FOR A PARTICULAR
    PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR
    ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
    OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. THE ENTIRE RISK
    AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM AND DOCUMENTATION
    IS WITH YOU.

    Permission to modify the code and to distribute modified code is granted,
    provided the above notices are retained, and a notice that the code was
    modified is included with the above copyright notice.

    written by: Juergen Mueller, D-70806 Kornwestheim, GERMANY

    FILE       : XMEMTHREAD.C
    REVISION   : 01-Jan-2008
                 18:08:01
 */
/*****************************************************************************/

#include <windows.h>
#include <process.h>

#include "xmem.h"

#define LOOPCNT     100

#ifdef __cplusplus
class C_T101                /* class C_T101 */
{
public:
  C_T101()
  {
    m_a = 1;
    m_pdbl = new double;
    m_pstr = new char [10];
  };
  virtual ~C_T101()
  {
    m_a = 0;
    if (m_pdbl)
    {
      *m_pdbl = 1.0;
      delete m_pdbl;
      m_pdbl = NULL;
    }
    if (m_pstr)
    {
      *m_pstr = '\0';
      delete [] m_pstr;
      m_pstr = NULL;
    }
  };

private:
  int m_a;
  double *m_pdbl;
  char *m_pstr;
};

class C_T102                /* class C_T102 */
{
public:
  C_T102()
  {
    m_t101 = new C_T101 [2];
  };
  virtual ~C_T102()
  {
    if (m_t101)
    {
      delete [] m_t101;
      m_t101 = NULL;
    }
  };

private:
  C_T101 *m_t101;
};

class C_T103                /* class C_T103 */
{
public:
  C_T103()
  {
    m_t102 = new C_T102;
  };
  virtual ~C_T103()
  {
    if (m_t102)
    {
      delete m_t102;
      m_t102 = NULL;
    }
  };

private:
  C_T102 *m_t102;
};

class C_T104                /* class C_T104 */
{
public:
  C_T104()
  {
    m_t103 = new C_T103;
  };
  virtual ~C_T104()
  {
    if (m_t103)
    {
      delete m_t103;
      m_t103 = NULL;
    }
  };

private:
  C_T103 *m_t103;
};

class C_T105                /* class C_T105 */
{
public:
  C_T105()
  {
    m_t104 = new C_T104;
  };
  virtual ~C_T105()
  {
    if (m_t104)
    {
      delete m_t104;
      m_t104 = NULL;
    }
  };

private:
  C_T104 *m_t104;
};
#endif

unsigned int start_thread(unsigned int(__stdcall *)(void*), void *);

unsigned int __stdcall function1(void *);
void function1_1();
void function1_2();
unsigned int flag_function1 = FALSE;

unsigned int __stdcall function2(void *);
unsigned int flag_function2 = FALSE;

unsigned int __stdcall function3(void *);
unsigned int flag_function3 = FALSE;

unsigned int __stdcall function4(void *);
unsigned int flag_function4 = FALSE;


/*****************************************************************************/
/* XMEM main test function */
/*****************************************************************************/
int main(int argc, char *argv[])
{
  int   i;
  void *ptr;

  argc = argc;
  argv = argv;

  start_thread(function1, NULL);            /* start thread 1 */
  ptr = strdup("this is a memory leak");    /* memory leak */
  while (flag_function1 == FALSE)
  {
    Sleep(1);                               /* wait until thread started */
  }

  start_thread(function2, NULL);            /* start thread 2 */
  ptr = strdup("this is a memory leak");    /* memory leak */
  while (flag_function2 == FALSE)
  {
    Sleep(1);                               /* wait until thread started */
  }

  start_thread(function3, NULL);            /* start thread 3 */
  ptr = strdup("this is a memory leak");    /* memory leak */
  while (flag_function3 == FALSE)
  {
    Sleep(1);                               /* wait until thread started */
  }

  start_thread(function4, NULL);            /* start thread 4 */
  ptr = strdup("this is a memory leak");    /* memory leak */
  while (flag_function4 == FALSE)
  {
    Sleep(1);                               /* wait until thread started */
  }

  i = 0;        /* initialize counter */

  while (flag_function1 || flag_function2 || flag_function3 || flag_function4)
  { /* loop as long as threads are running */
    Sleep(10);

    if (++i > 10)
    {
      i = 0;
      ptr = malloc(100);
      free(ptr);
    }
  }

#ifndef __cplusplus
  xmem_check_free();
#endif

  return(0);
}
/*****************************************************************************/
/* start a thread */
/*****************************************************************************/
unsigned int start_thread(unsigned int(__stdcall *pFunc)(void *), void *pParameter)
{
  unsigned int ThreadId;
  uintptr_t    hProcess;
    
  hProcess = _beginthreadex(NULL,                         // void *security,
                            0,                            // unsigned int stack_size,
                            (unsigned int (__stdcall *)(void *))pFunc,   // unsigned int (__stdcall *start_address)(void *),
                            pParameter,                   // void *arglist,
                            0,                            // unsigned int initflag,
                            (unsigned int *)&ThreadId     // unsigned int *thrdaddr 
                           );
  return(ThreadId);
}
/*****************************************************************************/
/* */
/*****************************************************************************/
unsigned int __stdcall function1(void *dummy)
{
  char *ptr;
  int   i;

  dummy = dummy;
  flag_function1 = TRUE;        /* signalize thread activity */

  for (i = 0; i < LOOPCNT; ++i)
  {
    printf("\nfunction1 (i = %d)", i);
    ptr = (char *)malloc(1000);
    Sleep(1);
    free(ptr);

#if 1
    if (i % 30)
    {
      free(ptr);                /* attempt to free already freed (illegal) pointer */
    }
#endif

    function1_1();              /* call sub function */
  }

  Sleep(1000);
  flag_function1 = FALSE;       /* reset thread activity */
  return(0);
}
/*****************************************************************************/
/* */
/*****************************************************************************/
void function1_1()
{
  char *ptr;
  ptr = (char *)malloc(1000);
  Sleep(1);
  free(ptr);
  function1_2();
}
/*****************************************************************************/
/* */
/*****************************************************************************/
void function1_2()
{
  char *ptr;
  ptr = (char *)malloc(1000);
  Sleep(1);
  free(ptr);
}
/*****************************************************************************/
/* */
/*****************************************************************************/
unsigned int __stdcall function2(void *dummy)
{
  char *ptr;
  int   i;

  dummy = dummy;
  flag_function2 = TRUE;

  for (i = 0; i < LOOPCNT; ++i)
  {
    printf("\nfunction2 (i = %d)", i);
    ptr = (char *)malloc(10000);
    Sleep(1);
    free(ptr);

#if __cplusplus
    ptr = new char [2000];
    Sleep(1);
    delete [] ptr;
#endif
  }

#if __cplusplus
    C_T105 *p_t105 = new C_T105;      /* test nested C++ new */
    if (p_t105 != NULL)
    {
      Sleep(1);
      delete p_t105;                  /* test nested C++ delete and C++ delete stack */
      p_t105 = NULL;
    }
#endif

  Sleep(1000);
  flag_function2 = FALSE;
  return(0);
}
/*****************************************************************************/
/* */
/*****************************************************************************/
unsigned int __stdcall function3(void *dummy)
{
  char *ptr;
  int   i;

  dummy = dummy;
  flag_function3 = TRUE;

  for (i = 0; i < LOOPCNT; ++i)
  {
    printf("\nfunction3 (i = %d)", i);
    ptr = (char *)malloc(20000);
    Sleep(1);
    free(ptr);
  }

  Sleep(1000);
  flag_function3 = FALSE;
  return(0);
}
/*****************************************************************************/
/* */
/*****************************************************************************/
unsigned int __stdcall function4(void *dummy)
{
  char *ptr;
  int   i;

  dummy = dummy;
  flag_function4 = TRUE;

  for (i = 0; i < LOOPCNT; ++i)
  {
    printf("\nfunction4 (i = %d)", i);
    ptr = (char *)malloc(5000);
    Sleep(1);
    free(ptr);
  }

#if __cplusplus
    C_T105 *p_t105 = new C_T105;      /* test nested C++ new */
    if (p_t105 != NULL)
    {
      Sleep(1);
      delete p_t105;                  /* test nested C++ delete and C++ delete stack */
      p_t105 = NULL;
    }
#endif

  Sleep(1000);
  flag_function4 = FALSE;
  return(0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -