1. Dashboard
  2. Forum
    1. Unerledigte Themen
  3. Mitglieder
    1. Letzte Aktivitäten
    2. Benutzer online
    3. Team-Mitglieder
    4. Trophäen
    5. Mitgliedersuche
  4. Tutorial Bereich
  • Anmelden
  • Registrieren
  • Suche
Dieses Thema
  • Alles
  • Dieses Thema
  • Dieses Forum
  • Seiten
  • Forum
  • Lexikon
  • Erweiterte Suche
  1. Informatik Forum
  2. Webmaster & Internet
  3. Entwicklung

Invalid read of size 1

  • Leocor
  • 19. April 2008 um 18:50
  • Unerledigt
  • Leocor
    4
    Leocor
    Mitglied
    Punkte
    165
    Beiträge
    23
    • 19. April 2008 um 18:50
    • #1

    ICh habe 2-3 klassen (für exceptions) geschrieben und verwende den polymorphismus zur Fehlerausgabe! ... soweit so gut ....

    ich hab die files geschrieben und jetzt bringt mir valgrind andauernd "Invalid read of size 1" als error und ich komm nicht darauf warum!! (ich steh wahrscheinlich ur auf der leitung)

    könnt ihr mir helfen?

    Code
    ==10892== Memcheck, a memory error detector.
    ==10892== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
    ==10892== Using LibVEX rev 1732, a library for dynamic binary translation.
    ==10892== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
    ==10892== Using valgrind-3.2.3, a dynamic binary instrumentation framework.
    ==10892== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
    ==10892== For more details, rerun with: -v
    ==10892==
    ==10892== Invalid read of size 1
    ==10892==    at 0x40235C8: strlen (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
    ==10892==    by 0x40D98EB: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_tr
    aits<char> >&, char const*) (in /usr/lib/libstdc++.so.6.0.9)
    ==10892==    by 0x80492D0: main (in /home/strenkwalder/workspace/mymath/a.out)
    ==10892==  Address 0x42A43A4 is 12 bytes inside a block of size 52 free'd
    ==10892==    at 0x4022156: operator delete(void*) (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
    ==10892==    by 0x40E403C: std::string::_Rep::_M_destroy(std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.9)
    ==10892==    by 0x40E5FB9: std::string::~string() (in /usr/lib/libstdc++.so.6.0.9)
    ==10892==    by 0x8049579: MathException::what() const (in /home/strenkwalder/workspace/mymath/a.out)
    ==10892==    by 0x80492C0: main (in /home/strenkwalder/workspace/mymath/a.out)
    ==10892==
    ==10892== Invalid read of size 1
    ==10892==    at 0x40235D3: strlen (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
    ==10892==    by 0x40D98EB: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_tr
    aits<char> >&, char const*) (in /usr/lib/libstdc++.so.6.0.9)
    ==10892==    by 0x80492D0: main (in /home/strenkwalder/workspace/mymath/a.out)==10892==  Address 0x42A43A5 is 13 bytes inside a block of size 52 free'd
    ==10892==    at 0x4022156: operator delete(void*) (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
    ==10892==    by 0x40E403C: std::string::_Rep::_M_destroy(std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.9)
    ==10892==    by 0x40E5FB9: std::string::~string() (in /usr/lib/libstdc++.so.6.0.9)
    ==10892==    by 0x8049579: MathException::what() const (in /home/strenkwalder/workspace/mymath/a.out)
    ==10892==    by 0x80492C0: main (in /home/strenkwalder/workspace/mymath/a.out)
    ==10892==
    ==10892== Syscall param write(buf) points to unaddressable byte(s)
    ==10892==    at 0x40007F2: (within /lib/ld-2.6.1.so)
    ==10892==    by 0x41D0F14: (within /lib/libc-2.6.1.so)
    ==10892==    by 0x41D11C5: _IO_file_xsputn (in /lib/libc-2.6.1.so)
    ==10892==    by 0x41C715E: fwrite (in /lib/libc-2.6.1.so)
    ==10892==    by 0x40D6CB5: (within /usr/lib/libstdc++.so.6.0.9)
    ==10892==    by 0x40D96FE: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, int) (in /usr/lib/libstdc++.so.6.0.9)
    ==10892==    by 0x40D98FB: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) (in /usr/lib/libstdc++.so.6.0.9)
    ==10892==    by 0x80492D0: main (in /home/strenkwalder/workspace/mymath/a.out)
    ==10892==  Address 0x42A43A4 is 12 bytes inside a block of size 52 free'd
    ==10892==    at 0x4022156: operator delete(void*) (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
    ==10892==    by 0x40E403C: std::string::_Rep::_M_destroy(std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.9)
    ==10892==    by 0x40E5FB9: std::string::~string() (in /usr/lib/libstdc++.so.6.0.9)
    ==10892==    by 0x8049579: MathException::what() const (in /home/strenkwalder/workspace/mymath/a.out)
    ==10892==    by 0x80492C0: main (in /home/strenkwalder/workspace/mymath/a.out)
    Error: The result is out of Range: ***
    ==10892==
    ==10892== ERROR SUMMARY: 41 errors from 3 contexts (suppressed: 3 from 1)
    ==10892== malloc/free: in use at exit: 0 bytes in 0 blocks.
    ==10892== malloc/free: 5 allocs, 5 frees, 725 bytes allocated.
    ==10892== For counts of detected errors, rerun with: -v
    ==10892== All heap blocks were freed -- no leaks are possible.
    Alles anzeigen

    mian.cpp

    Code
    int main(int argc, char *argv[] ){
    	[COLOR='#ff0000']try{
    		throw OverflowException("MSG");
    	}catch(MathException &e){
    		cerr << e.what() << "\n";
                }[/COLOR]
    	return EXIT_SUCCESS;
    }


    Code
    #include "MathException.h"
    
    
    
    
    MathException::MathException(const char *sz) throw(){
    	m_msg = sz;
    }
    
    
    MathException::~MathException() throw(){
    }
    [COLOR='Red']
    const char* MathException::what()  const throw(){
    	stringstream strs;
    	strs << "Error: " << m_errortyp.c_str() << ": " << m_msg.c_str();
    	return (strs.str()).c_str();
    }[/COLOR]
    Alles anzeigen

    im markierten text muss ich irgend einen fehler gemacht haben
    kann mir einer sagen was?

    http://de.youtube.com/watch?v=H9B4a2KEoGY&feature=related
    http://de.youtube.com/watch?v=HhHsXAVHyaA&feature=related

  • Ringding
    11
    Ringding
    Mitglied
    Reaktionen
    12
    Punkte
    1.237
    Beiträge
    244
    • 19. April 2008 um 19:02
    • #2

    Ja - in der letzten Zeile lieferst du einen char * auf einen schon zerstörten std::string zurück.

  • Leocor
    4
    Leocor
    Mitglied
    Punkte
    165
    Beiträge
    23
    • 19. April 2008 um 20:29
    • #3

    Ich programmier c++ noh nicht so lang!

    ich denk du meinst die zeile:
    return (strs.str()).c_str();

    warum ist der string schon zerstört?

    http://de.youtube.com/watch?v=H9B4a2KEoGY&feature=related
    http://de.youtube.com/watch?v=HhHsXAVHyaA&feature=related

  • WhiskeyCola
    2
    WhiskeyCola
    Mitglied
    Punkte
    20
    Beiträge
    4
    • 20. April 2008 um 12:30
    • #4

    Der Speicherbereich des in der Funktion erzeugten stringstream wird nach dem Verlassen der Funktion freigegeben. Du lieferst aber einen Pointer auf den String zurück, der vorher da gelegen ist.

    :winking_face: Lebe wie du, wenn du stirbst, wünschen wirst gelebt zu haben (Christian F. Gellert) :winking_face:

  • Maximilian Rupp 27. Dezember 2024 um 12:04

    Hat das Thema aus dem Forum Programmieren nach Entwicklung verschoben.

Jetzt mitmachen!

Sie haben noch kein Benutzerkonto auf unserer Seite? Registrieren Sie sich kostenlos und nehmen Sie an unserer Community teil!

Benutzerkonto erstellen Anmelden

Rechtliches

Impressum

Datenschutzerklärung