public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/50184] New: Segmentation fault. Copy Constructor.
@ 2011-08-25  9:02 EugeneSm at yandex dot ru
  2011-08-25  9:16 ` [Bug c++/50184] " EugeneSm at yandex dot ru
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: EugeneSm at yandex dot ru @ 2011-08-25  9:02 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50184

             Bug #: 50184
           Summary: Segmentation fault. Copy Constructor.
    Classification: Unclassified
           Product: gcc
           Version: 4.4.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: EugeneSm@yandex.ru


#include <iostream>
#include <map>
using namespace std;

class CData
{
    class CItem
    {
    public:
        string m_str1;
    };

public:
    map<string, CItem>  m_map;

    int                 m_nWaitTime2ReadSS;
    int                 m_eQueueOrderType;

    //- Data ---------------
    bool                m_bDFRead;
    std::string         m_strDFName;
    int                 m_nDFLoopCount;
    int                 m_nDFLoopTimeout;
    int                 m_nDFMsgTimeout;
};

class A
{
public:
    CData func()
    {
        CData data;
        data.m_map["Test"].m_str1 = "Data";
        return data;
    }
};

class B : public CData
{
public:
    template <class T>
    B(T& a)
        : CData(a.func())
    {
        map<string, CItem>::iterator it = m_map.begin();
        for (; it != m_map.end(); it++)//In this place m_map.end() returns
wrong value as result I get segmentation fault.
//I noticed that copy constructor when I added one into CData is never called.
        {
            cout << (*it).first<< " "<<(*it).second.m_str1<<"\n";
        }
        cout << "GOOD"<<"\n";
    }
};

int main()
{
    A a;
    B b1(a);
    return 0;
}

Result of this code is Segmentation fault.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/50184] Segmentation fault. Copy Constructor.
  2011-08-25  9:02 [Bug c++/50184] New: Segmentation fault. Copy Constructor EugeneSm at yandex dot ru
@ 2011-08-25  9:16 ` EugeneSm at yandex dot ru
  2011-08-25 10:24 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: EugeneSm at yandex dot ru @ 2011-08-25  9:16 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50184

--- Comment #1 from Eugene <EugeneSm at yandex dot ru> 2011-08-25 09:01:52 UTC ---
gcc version 4.4.4 20100726 (Red Hat 4.4.4-13) (GCC)

**** Build of configuration Debug for project Test ****

make all 
Building file: ../src/Test.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Test.d"
-MT"src/Test.d" -o"src/Test.o" "../src/Test.cpp"
Finished building: ../src/Test.cpp

Building target: Test
Invoking: GCC C++ Linker
g++  -o"Test"  ./src/Test.o   
Finished building target: Test


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/50184] Segmentation fault. Copy Constructor.
  2011-08-25  9:02 [Bug c++/50184] New: Segmentation fault. Copy Constructor EugeneSm at yandex dot ru
  2011-08-25  9:16 ` [Bug c++/50184] " EugeneSm at yandex dot ru
@ 2011-08-25 10:24 ` redi at gcc dot gnu.org
  2011-08-25 11:14 ` redi at gcc dot gnu.org
  2015-08-24 12:07 ` zeccav at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2011-08-25 10:24 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50184

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2011-08-25
     Ever Confirmed|0                           |1
      Known to fail|                            |4.1.2, 4.4.3, 4.5.2, 4.6.1,
                   |                            |4.7.0

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-08-25 10:12:21 UTC ---
(that code is actually invalid and g++ should reject it because CData::CItem is
private in B's constructor, but there's a g++ bug that doesn't do access
checking for template arguments, and fixing that doesn't prevent the segfault)

reduced:

#include <map>
#include <string>
using namespace std;

struct CData
{
    struct CItem
    {
        string m_str1;
    };

    map<string, CItem>  m_map;

    std::string         m_strDFName;
    int                 m_nDFMsgTimeout;
};

    CData func()
    {
        CData data;
        data.m_map["Test"].m_str1 = "Data";
        return data;
    }

class B : public CData
{
public:
    B()
        : CData(func())
    {
        std::string s;
        map<string, CItem>::iterator it = m_map.begin();
        for (; it != m_map.end(); it++) // loops past the end
        {
            s += it->second.m_str1 + it->first;;
        }
    }
};

int main()
{
    B b1;
    return 0;
}


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/50184] Segmentation fault. Copy Constructor.
  2011-08-25  9:02 [Bug c++/50184] New: Segmentation fault. Copy Constructor EugeneSm at yandex dot ru
  2011-08-25  9:16 ` [Bug c++/50184] " EugeneSm at yandex dot ru
  2011-08-25 10:24 ` redi at gcc dot gnu.org
@ 2011-08-25 11:14 ` redi at gcc dot gnu.org
  2015-08-24 12:07 ` zeccav at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2011-08-25 11:14 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50184

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-08-25 10:48:59 UTC ---
-fno-elide-constructors prevents the segfault

something goes wrong copying the return value of func() into the CData base
class of B


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/50184] Segmentation fault. Copy Constructor.
  2011-08-25  9:02 [Bug c++/50184] New: Segmentation fault. Copy Constructor EugeneSm at yandex dot ru
                   ` (2 preceding siblings ...)
  2011-08-25 11:14 ` redi at gcc dot gnu.org
@ 2015-08-24 12:07 ` zeccav at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: zeccav at gmail dot com @ 2015-08-24 12:07 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=50184

Vittorio Zecca <zeccav at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |zeccav at gmail dot com

--- Comment #4 from Vittorio Zecca <zeccav at gmail dot com> ---
I still have the same bug in g++ 5.2.0 in the following:

#include <map>
#include <string>
using namespace std;

struct CData
{
    struct CItem
    {
        string m_str1;
    };

    map<string, CItem>  m_map;

    std::string         m_strDFName;
    int                 m_nDFMsgTimeout;
};

    CData func()
    {
        CData data;
        data.m_map["Test"].m_str1 = "Data";
        return data;
    }

class B : public CData
{
public:
    B()
        : CData(func())
    {
        std::string s;
        map<string, CItem>::iterator it = m_map.begin();
        for (; it != m_map.end(); it++) // loops past the end
        {
            s += it->second.m_str1 + it->first;;
        }
    }
};

int main()
{
    B b1;
    return 0;
}


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-08-24 12:07 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-25  9:02 [Bug c++/50184] New: Segmentation fault. Copy Constructor EugeneSm at yandex dot ru
2011-08-25  9:16 ` [Bug c++/50184] " EugeneSm at yandex dot ru
2011-08-25 10:24 ` redi at gcc dot gnu.org
2011-08-25 11:14 ` redi at gcc dot gnu.org
2015-08-24 12:07 ` zeccav at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).