Results 1 to 3 of 3

Thread: memory corruption

  1. #1

    Question memory corruption

    Hi,

    I'm having a problem here and I wonder if someone can help me:

    I have an Email Notification class which inherits from QThread so the email sending does not block the main thread. The function that sends notifications can be called from various location in the program so I've created a small Ring buffer class which stores quint32 then I add the email details in a struct and store the address of the struct in the ring buffer. Then the run() function loops through the ring buffer and sends the emails.

    The problem I'm having is that as soon as I call the function once and then close the application (in Debug mode), I get a message saying "Unhandled exception at 0x009eb9df in MyApp.exe: 0xC0000005: Access violation writing location 0xfeeefeee." and the Call Stack shows me a random location each time as if the memory would be corrupted and anything would crash.

    Here are the two functions that are called:

    Qt Code:
    1. bool CNotification::sendAlertNotification(QStringList qszEmailTo, QString qszAlert)
    2. {
    3. if(isNotificationEnabled() == false)
    4. {
    5. return false;
    6. }
    7.  
    8. SNotificationData *pNotData = new SNotificationData;
    9. pNotData->qszTo = qszEmailTo;
    10. pNotData->qszBody = qszAlert;
    11.  
    12. m_ringBuffer.put((quint32)pNotData);
    13.  
    14. start();
    15.  
    16. return true;
    17. }
    18.  
    19. void CNotification::run()
    20. {
    21. QMutexLocker locker(&m_mutex);
    22.  
    23. while(m_ringBuffer.level() > 0)
    24. {
    25. SNotificationData *pNotData = (SNotificationData*)m_ringBuffer.get();
    26.  
    27. // --> removed the email sending code from here but it crashes anyway
    28.  
    29. delete pNotData, pNotData = 0;
    30. msleep(100);
    31. }
    32. }
    To copy to clipboard, switch view to plain text mode 

    Do you think it's because I create the structs on the heap in the main thread and then delete them in the new thread? And how could I code it for it to work then?

    Thanks a lot!
    Simon

    **EDIT**
    Here's the code of the ring buffer class, it can maybe help someone else

    RingBuffer32.cpp

    Qt Code:
    1. #include "RingBuffer32.h"
    2.  
    3. CRingBuffer32::CRingBuffer32()
    4. : m_uLevel(0),
    5. m_uDepth(0),
    6. m_pR(0),
    7. m_pW(0),
    8. m_pBuffer(0)
    9. {
    10.  
    11. }
    12.  
    13. CRingBuffer32::~CRingBuffer32()
    14. {
    15. if(m_pBuffer)
    16. {
    17. delete[] m_pBuffer;
    18. m_pBuffer = 0;
    19. }
    20. }
    21.  
    22. void CRingBuffer32::initialize(quint32 uDepth)
    23. {
    24. m_uLevel = 0;
    25. m_uDepth = uDepth;
    26.  
    27. if(m_pBuffer)
    28. {
    29. delete[] m_pBuffer;
    30. }
    31.  
    32. m_pBuffer = new quint32[m_uLevel];
    33.  
    34. m_pR = m_pBuffer;
    35. m_pW = m_pBuffer;
    36. }
    37.  
    38. void CRingBuffer32::uninitialize()
    39. {
    40. m_uLevel = 0;
    41. m_uDepth = 0;
    42. m_pR = 0;
    43. m_pW = 0;
    44.  
    45. if(m_pBuffer)
    46. {
    47. delete[] m_pBuffer;
    48. m_pBuffer = 0;
    49. }
    50. }
    51.  
    52. void CRingBuffer32::clear()
    53. {
    54. m_uLevel = 0;
    55. m_pR = m_pBuffer;
    56. m_pW = m_pBuffer;
    57. }
    58.  
    59. quint32 CRingBuffer32::getStat()
    60. {
    61. if(m_uLevel >= m_uDepth)
    62. {
    63. return Stat_Full;
    64. }
    65.  
    66. if(m_uLevel == 0)
    67. {
    68. return Stat_Empty;
    69. }
    70.  
    71. return Stat_HasData;
    72. }
    73.  
    74. quint32 CRingBuffer32::level()
    75. {
    76. return m_uLevel;
    77. }
    78.  
    79. quint32 CRingBuffer32::put(quint32 u)
    80. {
    81. if(m_uLevel < m_uDepth)
    82. {
    83. *m_pW++ = u;
    84.  
    85. if(m_pW >= (m_pBuffer + m_uDepth))
    86. {
    87. m_pW = m_pBuffer;
    88. }
    89.  
    90. m_uLevel++;
    91.  
    92. return u;
    93. }
    94. else
    95. {
    96. return (quint32) -1;
    97. }
    98. }
    99.  
    100. quint32 CRingBuffer32::get()
    101. {
    102. quint32 u;
    103.  
    104. if(m_uLevel > 0)
    105. {
    106. u = *m_pR++;
    107.  
    108. if(m_pR >= (m_pBuffer + m_uDepth))
    109. {
    110. m_pR = m_pBuffer;
    111. }
    112.  
    113. m_uLevel--;
    114.  
    115. return u;
    116. }
    117. else
    118. {
    119. return (quint32) -1;
    120. }
    121. }
    122.  
    123. quint32 CRingBuffer32::write(quint32 *pSrc, quint32 uCount)
    124. {
    125. quint32 i = 0;
    126.  
    127. while(i < uCount)
    128. {
    129. if(m_uLevel < m_uDepth)
    130. {
    131. *m_pW++ = *pSrc++;
    132.  
    133. if(m_pW >= (m_pBuffer + m_uDepth))
    134. {
    135. m_pW = m_pBuffer;
    136. }
    137.  
    138. m_uLevel++;
    139. i++;
    140. }
    141. else
    142. {
    143. return i;
    144. }
    145. }
    146.  
    147. return i;
    148. }
    149.  
    150. quint32 CRingBuffer32::read(quint32 *pDest, quint32 uCount)
    151. {
    152. quint32 i = 0;
    153.  
    154. while(i < uCount)
    155. {
    156. if(m_uLevel > 0)
    157. {
    158. *pDest++ = *m_pR++;
    159.  
    160. if(m_pR >= (m_pBuffer + m_uDepth))
    161. {
    162. m_pR = m_pBuffer;
    163. }
    164.  
    165. m_uLevel--;
    166. i++;
    167. }
    168. else
    169. {
    170. return i;
    171. }
    172. }
    173.  
    174. return i;
    175. }
    To copy to clipboard, switch view to plain text mode 

    RingBuffer32.h

    Qt Code:
    1. #ifndef RINGBUFFER32_H
    2. #define RINGBUFFER32_H
    3.  
    4. #include <QObject>
    5.  
    6. class CRingBuffer32
    7. {
    8. public:
    9.  
    10. enum
    11. {
    12. Stat_Empty=0,
    13. Stat_HasData=1,
    14. Stat_Full=2
    15. };
    16.  
    17. CRingBuffer32();
    18. ~CRingBuffer32();
    19.  
    20. void initialize(quint32 uDepth);
    21. void uninitialize();
    22. void clear();
    23. quint32 getStat();
    24. quint32 level();
    25. quint32 put(quint32 u);
    26. quint32 get();
    27. quint32 write(quint32 *pSrc, quint32 uCount);
    28. quint32 read(quint32 *pDest, quint32 uCount);
    29.  
    30. private:
    31. quint32 m_uLevel;
    32. quint32 m_uDepth;
    33. quint32 *m_pR;
    34. quint32 *m_pW;
    35. quint32 *m_pBuffer;
    36. };
    37.  
    38. #endif
    To copy to clipboard, switch view to plain text mode 
    Last edited by Rambobino; 10th August 2010 at 20:48.

  2. #2

    Default Re: memory corruption

    Ok after further investigation it looks like the problem is in the put() and get() functions of the ring buffer class. When they are not called everything is fine.
    Even if everything is on the stack, it crashes after using put() and get().

  3. #3

    Default Re: memory corruption

    Ok forget about my post, I found the problem...

    The buffer always had a size of 0 because i was using the wrong variable to create it..

    Qt Code:
    1. void CRingBuffer32::initialize(quint32 uDepth)
    2. {
    3. m_uLevel = 0;
    4. m_uDepth = uDepth;
    5.  
    6. if(m_pBuffer)
    7. {
    8. delete[] m_pBuffer;
    9. }
    10.  
    11. m_pBuffer = new quint32[m_uDepth]; // <-- was using m_uLevel here...
    12.  
    13. m_pR = m_pBuffer;
    14. m_pW = m_pBuffer;
    15. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Crash: Heap corruption due to selectedRows()
    By Ankitha Varsha in forum Qt Programming
    Replies: 16
    Last Post: 1st October 2010, 00:55
  2. Data corruption with read() / ReadFile()
    By Teuniz in forum Qt Programming
    Replies: 5
    Last Post: 20th November 2009, 15:56
  3. Heap Corruption when calling availablePrinters
    By andyp in forum Qt Programming
    Replies: 0
    Last Post: 20th November 2009, 15:15
  4. Replies: 0
    Last Post: 29th September 2009, 01:16
  5. Widget corruption
    By grzywacz in forum Qt Tools
    Replies: 5
    Last Post: 28th May 2006, 11:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.