#ifndef BUFFER_H
#define BUFFER_H
#include <string>
#include <cstring>
class CBuffer
{
public:
CBuffer():m_MaxLength(1024*10),m_OriMaxLength(m_MaxLength),m_pMsg(new char[m_MaxLength]), m_CurLen(0), m_LogLvl(0), m_GOLen(0)
{}
CBuffer(int size):m_MaxLength(size),m_OriMaxLength(m_MaxLength),m_pMsg(new char[m_MaxLength]), m_CurLen(0), m_LogLvl(0), m_GOLen(0)
{}
bool Append(std::string & str)
{
Append(str.c_str(),str.size());
return true;
}
char* GetCurData(){
return m_pMsg + m_GOLen;
}
int GetDataLength(){
return m_CurLen;
}
bool Append(const char * pData, int n32Length)
{
//#include<iostream>
//std::cout << "len " << n32Length;
// while(m_CurLen + n32Length > m_MaxLength)
// {
// m_MaxLength *= 2;
// m_pMsg = (char*)realloc(m_pMsg, m_MaxLength);
// }
std::memcpy(m_pMsg + m_CurLen,pData,n32Length);
m_CurLen+= n32Length;
return true;
}
void Clear(){
if (m_MaxLength > m_OriMaxLength){
delete []m_pMsg;
m_pMsg = new char[m_OriMaxLength];
m_MaxLength = m_OriMaxLength;
}
if (m_CurLen > 0){
m_CurLen = 0;
}
m_GOLen = 0;
}
int m_LogLvl;
private:
char * m_pMsg;
int m_GOLen;
const int m_OriMaxLength;
int m_CurLen;
int m_MaxLength;
};
#endif /* BUFFER_H */
std::string str = rMsg.SerializeAsString();
if (pBuffer == NULL) return false;
pBuffer->Append(str);
Ошибка
> *** Error in `/home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver':
> munmap_chunk(): invalid pointer: 0x00007faba0002540 ***
> ======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7fabad4f67e5]
> /lib/x86_64-linux-gnu/libc.so.6(cfree+0x1a8)[0x7fabad503698]
> /home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver[0x42ca09]
> /home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver[0x445d7a]
> /home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver[0x44cb8f]
> /home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver[0x44a907]
> /home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver[0x449f15]
> /home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver[0x448f2f]
> /home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver[0x447e69]
> /home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver[0x41d4d4]
> /home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver[0x42c93c]
> /home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver[0x42d537]
> /home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver[0x42d48b]
> /home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver[0x42d301]
> /home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver[0x42d11a]
> /home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver[0x41d4d4]
> /home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver[0x41939a]
> /home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver[0x4225ff]
> /home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver[0x422562]
> /home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver[0x422186]
> /home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver[0x421c10]
> /usr/local/lib/libboost_thread.so.1.65.1(+0x3cf55)[0x7fabae777f55]
> /lib/x86_64-linux-gnu/libpthread.so.0(+0x76ba)[0x7fabae2f56ba]
> /lib/x86_64-linux-gnu/libc.so.6(clone+0x6d)[0x7fabad5863dd]
> ======= Memory map: ======== 00400000-004c3000 r-xp 00000000 08:05 4982169
> /home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver
> 006c2000-006c3000 r--p 000c2000 08:05 4982169
> /home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver
> 006c3000-006c4000 rw-p 000c3000 08:05 4982169
> /home/cucaracha/NetBeansProjects/GameMSServer/dist/Debug/GNU-Linux/gamemsserver
> 006c4000-006c5000 rw-p 00000000 00:00 0 02210000-02283000 rw-p
> 00000000 00:00 0 [heap]
> 7faba0000000-7faba0021000 rw-p 00000000 00:00 0
> 7faba0021000-7faba4000000 ---p 00000000 00:00 0
> 7faba7761000-7faba7762000 ---p 00000000 00:00 0
> 7faba7762000-7faba7f62000 rw-p 00000000 00:00 0
> 7faba7f62000-7faba7f6d000 r-xp 00000000 08:05 4592261
> /lib/x86_64-linux-gnu/libnss_files-2.23.so 7faba7f6d000-7faba816c000
> ---p 0000b000 08:05 4592261 /lib/x86_64-linux-gnu/libnss_files-2.23.so 7faba816c000-7faba816d000
> r--p 0000a000 08:05 4592261
> /lib/x86_64-linux-gnu/libnss_files-2.23.so 7faba816d000-7faba816e000
> rw-p 0000b000 08:05 4592261
> /lib/x86_64-linux-gnu/libnss_files-2.23.so 7faba816e000-7faba8174000
> rw-p 00000000 00:00 0 7faba8174000-7faba817f000 r-xp 00000000 08:05
> 4592271 /lib/x86_64-linux-gnu/libnss_nis-2.23.so
> 7faba817f000-7faba837e000 ---p 0000b000 08:05 4592271
> /lib/x86_64-linux-gnu/libnss_nis-2.23.so 7faba837e000-7faba837f000
> r--p 0000a000 08:05 4592271
> /lib/x86_64-linux-gnu/libnss_nis-2.23.so 7faba837f000-7faba8380000
> rw-p 0000b000 08:05 4592271
> /lib/x86_64-linux-gnu/libnss_nis-2.23.so 7faba8380000-7faba8396000
> r-xp 00000000 08:05 4592255
> /lib/x86_64-linux-gnu/libnsl-2.23.so 7faba8396000-7faba8595000 ---p
> 00016000 08:05 4592255
> /lib/x86_64-linux-gnu/libnsl-2.23.so 7faba8595000-7faba8596000 r--p
> 00015000 08:05 4592255
> /lib/x86_64-linux-gnu/libnsl-2.23.so 7faba8596000-7faba8597000 rw-p
> 00016000 08:05 4592255
> /lib/x86_64-linux-gnu/libnsl-2.23.so 7faba8597000-7faba8599000 rw-p
> 00000000 00:00 0 7faba8599000-7faba85a1000 r-xp 00000000 08:05
> 4592257 /lib/x86_64-linux-gnu/libnss_compat-2.23.so
> 7faba85a1000-7faba87a0000 ---p 00008000 08:05 4592257
> /lib/x86_64-linux-gnu/libnss_compat-2.23.so 7faba87a0000-7faba87a1000
> r--p 00007000 08:05 4592257
> /lib/x86_64-linux-gnu/libnss_compat-2.23.so 7faba87a1000-7faba87a2000
> rw-p 00008000 08:05 4592257
> /lib/x86_64-linux-gnu/libnss_compat-2.23.so 7faba87a2000-7faba87a9000
> r-xp 00000000 08:05 139893
> /usr/lib/x86_64-linux-gnu/libffi.so.6.0.4 7faba87a9000-7faba89a8000
> ---p 00007000 08:05 139893 /usr/lib/x86_64-linux-gnu/libffi.so.6.0.4 7faba89a8000-7faba89a9000
> r--p 00006000 08:05 139893
> /usr/lib/x86_64-linux-gnu/libffi.so.6.0.4 7faba89a9000-7faba89aa000
> rw-p 00007000 08:05 139893
> /usr/lib/x86_64-linux-gnu/libffi.so.6.0.4 7faba89aa000-7faba89b3000
> r-xp 00000000 08:05 4592172
> /lib/x86_64-linux-gnu/libcrypt-2.23.so 7faba89b3000-7faba8bb2000 ---p
> 00009000 08:05 4592172
> /lib/x86_64-linux-gnu/libcrypt-2.23.so 7faba8bb2000-7faba8bb3000 r--p
> 00008000 08:05 4592172
> /lib/x86_64-linux-gnu/libcrypt-2.23.so 7faba8bb3000-7faba8bb4000 rw-p
> 00009000 08:05 4592172
> /lib/x86_64-linux-gnu/libcrypt-2.23.so 7faba8bb4000-7faba8be2000 rw-p
> 00000000 00:00 0 7faba8be2000-7faba8cb1000 r-xp 00000000 08:05 140662
> /usr/lib/x86_64-linux-gnu/libsqlite3.so.0.8.6
> 7faba8cb1000-7faba8eb1000 ---p 000cf000 08:05 140662
> /usr/lib/x86_64-linux-gnu/libsqlite3.so.0.8.6
> 7faba8eb1000-7faba8eb4000 r--p 000cf000 08:05 140662
> /usr/lib/x86_64-linux-gnu/libsqlite3.so.0.8.6
> 7faba8eb4000-7faba8eb6000 rw-p 000d2000 08:05 140662
> /usr/lib/x86_64-linux-gnu/libsqlite3.so.0.8.6
> 7faba8eb6000-7faba8eb7000 rw-p 00000000 00:00 0
> 7faba8eb7000-7faba8efe000 r-xp 00000000 08:05 140178
> /usr/lib/x86_64-linux-gnu/libhx509.so.5.0.0 7faba8efe000-7faba90fd000
> ---p 00047000 08:05 140178 /usr/lib/x86_64-linux-gnu/libhx509.so.5.0.0 7faba90fd000-7faba90ff000
> r--p 00046000 08:05 140178
> /usr/lib/x86_64-linux-gnu/libhx509.so.5.0.0 7faba90ff000-7faba9101000
> rw-p 00048000 08:05 140178
> /usr/lib/x86_64-linux-gnu/libhx509.so.5.0.0 7faba9101000-7faba9102000
> rw-p 00000000 00:00 0 7faba9102000-7faba9110000 r-xp 00000000 08:05
> 140160
> /usr/lib/x86_64-linux-gnu/libheimbase.so.1.0.0
> 7faba9110000-7faba930f000 ---p 0000e000 08:05 140160
> /usr/lib/x86_64-linux-gnu/libheimbase.so.1.0.0
> 7faba930f000-7faba9310000 r--p 0000d000 08:05 140160
> /usr/lib/x86_64-linux-gnu/libheimbase.so.1.0.0
> 7faba9310000-7faba9311000 rw-p 0000e000 08:05 140160
> /usr/lib/x86_64-linux-gnu/libheimbase.so.1.0.0
> 7faba9311000-7faba9338000 r-xp 00000000 08:05 140835
> /usr/lib/x86_64-linux-gnu/libwind.so.0.0.0 7faba9338000-7faba9538000
> ---p 00027000 08:05 140835 /usr/lib/x86_64-linux-gnu/libwind.so.0.0.0 7faba9538000-7faba9539000
> r--p 00027000 08:05 140835
> /usr/lib/x86_64-linux-gnu/libwind.so.0.0.0 7faba9539000-7faba953a000
> rw-p 00028000 08:05 140835
> /usr/lib/x86_64-linux-gnu/libwind.so.0.0.0 7faba953a000-7faba95b9000
> r-xp 00000000 08:05 140026
> /usr/lib/x86_64-linux-gnu/libgmp.so.10.3.0 7faba95b9000-7faba97b8000
> ---p 0007f000 08:05 140026 /usr/lib/x86_64-linux-gnu/libgmp.so.10.3.0 7faba97b8000-7faba97b9000
> r--p 0007e000 08:05 140026
> /usr/lib/x86_64-linux-gnu/libgmp.so.10.3.0 7faba97b9000-7faba97ba000
> rw-p 0007f000 08:05 140026
> /usr/lib/x86_64-linux-gnu/libgmp.so.10.3.0 7faba97ba000-7faba97ec000
> r-xp 00000000 08:05 140164
> /usr/lib/x86_64-linux-gnu/libhogweed.so.4.2 7faba97ec000-7faba99eb000
> ---p 00032000 08:05 140164 /usr/lib/x86_64-linux-gnu/libhogweed.so.4.2 7faba99eb000-7faba99ec000
> r--p 00031000 08:05 140164
> /usr/lib/x86_64-linux-gnu/libhogweed.so.4.2 7faba99ec000-7faba99ed000
> rw-p 00032000 08:05 140164
> /usr/lib/x86_64-linux-gnu/libhogweed.so.4.2 7faba99ed000-7faba9a21000
> r-xp 00000000 08:05 140389
> /usr/lib/x86_64-linux-gnu/libnettle.so.6.2 7faba9a21000-7faba9c20000
> ---p 00034000 08:05 140389 /usr/lib/x86_64-linux-gnu/libnettle.so.6.2 7faba9c20000-7faba9c22000
> r--p 00033000 08:05 140389
> /usr/lib/x86_64-linux-gnu/libnettle.so.6.2 7faba9c22000-7faba9c23000
> rw-p 00035000 08:05 140389
> /usr/lib/x86_64-linux-gnu/libnettle.so.6.2 7faba9c23000-7faba9c34000
> r-xp 00000000 08:05 140681
> /usr/lib/x86_64-linux-gnu/libtasn1.so.6.5.1 7faba9c34000-7faba9e34000
> ---p 00011000 08:05 140681 /usr/lib/x86_64-linux-gnu/libtasn1.so.6.5.1 7faba9e34000-7faba9e35000
> r--p 00011000 08:05 140681
> /usr/lib/x86_64-linux-gnu/libtasn1.so.6.5.1 7faba9e35000-7faba9e36000
> rw-p 00012000 08:05 140681
> /usr/lib/x86_64-linux-gnu/libtasn1.so.6.5.1 7faba9e36000-7faba9e67000
> r-xp 00000000 08:05 131383
> /usr/lib/x86_64-linux-gnu/libidn.so.11.6.15 7faba9e67000-7fabaa067000
> ---p 00031000 08:05 131383 /usr/lib/x86_64-linux-gnu/libidn.so.11.6.15 7fabaa067000-7fabaa068000
> r--p 00031000 08:05 131383
> /usr/lib/x86_64-linux-gnu/libidn.so.11.6.15 7fabaa068000-7fabaa069000
> rw-p 00032000 08:05 131383
> /usr/lib/x86_64-linux-gnu/libidn.so.11.6.15 7fabaa069000-7fabaa0c2000
> r-xp 00000000 08:05 140452
> /usr/lib/x86_64-linux-gnu/libp11-kit.so.0.1.0
> 7fabaa0c2000-7fabaa2c1000 ---p 00059000 08:05 140452
> /usr/lib/x86_64-linux-gnu/libp11-kit.so.0.1.0
> 7fabaa2c1000-7fabaa2cb000 r--p 00058000 08:05 140452
> /usr/lib/x86_64-linux-gnu/libp11-kit.so.0.1.0
> 7fabaa2cb000-7fabaa2cd000 rw-p 00062000 08:05 140452
> /usr/lib/x86_64-linux-gnu/libp11-kit.so.0.1.0
> 7fabaa2cd000-7fabaa2e2000 r-xp 00000000 08:05 140576
> /usr/lib/x86_64-linux-gnu/libroken.so.18.1.0 7fabaa2e2000-7fabaa4e1000
> ---p 00015000 08:05 140576 /usr/lib/x86_64-linux-gnu/libroken.so.18.1.0 7fabaa4e1000-7fabaa4e2000
> r--p 00014000 08:05 140576
> /usr/lib/x86_64-linux-gnu/libroken.so.18.1.0 7fabaa4e2000-7fabaa4e3000
> rw-p 00015000 08:05 140576
> /usr/lib/x86_64-linux-gnu/libroken.so.18.1.0 7fabaa4e3000-7fabaa513000
> r-xp 00000000 08:05 140158
> /usr/lib/x86_64-linux-gnu/libhcrypto.so.4.1.0
> 7fabaa513000-7fabaa713000 ---p 00030000 08:05 140158
> /usr/lib/x86_64-linux-gnu/libhcrypto.so.4.1.0
> 7fabaa713000-7fabaa714000 r--p 00030000 08:05 140158
> /usr/lib/x86_64-linux-gnu/libhcrypto.so.4.1.0
> 7fabaa714000-7fabaa715000 rw-p 00031000 08:05 140158
> /usr/lib/x86_64-linux-gnu/libhcrypto.so.4.1.0
> 7fabaa715000-7fabaa716000 rw-p 00000000 00:00 0
> 7fabaa716000-7fabaa7b5000 r-xp 00000000 08:05 139595
> /usr/lib/x86_64-linux-gnu/libasn1.so.8.0.0 7fabaa7b5000-7fabaa9b4000
> ---p 0009f000 08:05 139595 /usr/lib/x86_64-linux-gnu/libasn1.so.8.0.0 7fabaa9b4000-7fabaa9b5000
> r--p 0009e000 08:05 139595
> /usr/lib/x86_64-linux-gnu/libasn1.so.8.0.0 7fabaa9b5000-7fabaa9b8000
> rw-p 0009f000 08:05 139595
> /usr/lib/x86_64-linux-gnu/libasn1.so.8.0.0 7fabaa9b8000-7fabaaa3c000
> r-xp 00000000 08:05 140265
> /usr/lib/x86_64-linux-gnu/libkrb5.so.26.0.0 7fabaaa3c000-7fabaac3b000
> ---p 00084000 08:05 140265 /usr/lib/x86_64-linux-gnu/libkrb5.so.26.0.0 7fabaac3b000-7fabaac3e000
> r--p 00083000 08:05 140265
> /usr/lib/x86_64-linux-gnu/libkrb5.so.26.0.0 7fabaac3e000-7fabaac41000
> rw-p 00086000 08:05 140265
> /usr/lib/x86_64-linux-gnu/libkrb5.so.26.0.0 7fabaac41000-7fabaac42000
> rw-p 00000000 00:00 0 7fabaac42000-7fabaac4a000 r-xp 00000000 08:05
> 140162
> /usr/lib/x86_64-linux-gnu/libheimntlm.so.0.1.0
> 7fabaac4a000-7fabaae49000 ---p 00008000 08:05 140162
> /usr/lib/x86_64-linux-gnu/libheimntlm.so.0.1.0
> 7fabaae49000-7fabaae4a000 r--p 00007000 08:05 140162
> /usr/lib/x86_64-linux-gnu/libheimntlm.so.0.1.0
> 7fabaae4a000-7fabaae4b000 rw-p 00008000 08:05 140162
> /usr/lib/x86_64-linux-gnu/libheimntlm.so.0.1.0
> 7fabaae4b000-7fabaae4e000 r-xp 00000000 08:05 4592223
> /lib/x86_64-linux-gnu/libkeyutils.so.1.5 7fabaae4e000-7fabab04d000
> ---p 00003000 08:05 4592223 /lib/x86_64-linux-gnu/libkeyutils.so.1.5 7fabab04d000-7fabab04e000
> r--p 00002000 08:05 4592223
> /lib/x86_64-linux-gnu/libkeyutils.so.1.5 7fabab04e000-7fabab04f000
> rw-p 00003000 08:05 4592223
> /lib/x86_64-linux-gnu/libkeyutils.so.1.5 7fabab04f000-7fabab172000
> r-xp 00000000 08:05 133687
> /usr/lib/x86_64-linux-gnu/libgnutls.so.30.6.2 tempRes : RUN FINISHED;
> Aborted; core dumped; real time: 530ms; user: 0ms; system: 20ms
Заметьте, что m_pMsg
объявлен до m_MaxLength
. Соответственно при вызове любого конструктора m_pMsg
будет проинициализирован до m_MaxLength
(даже несмотря на то, что в списке инициализации m_MaxLength
идет раньше!) и такая инициализация m_pMsg(new char[m_MaxLength])
приводит к неопределенному поведению. Также у класса отсутствует деструктор (память утекает), конструкторы и операторы копирования / перемещения (опять же можно нарваться на неопределенное поведение), ну и при записи в буффер нет проверок что в нем имеется достаточно места.
Айфон мало держит заряд, разбираемся с проблемой вместе с AppLab
Перевод документов на английский язык: Важность и ключевые аспекты
Доброго времени сутокЕсть задание: реализовать программу, которая меняет местами последнюю строку и первый столбик квадратной матрицы
Добрый деньТакая проблема, если текст который разделен на span элемента,но т