STL vector crashes as a class member in push_back.

Started by
7 comments, last by Kande 15 years, 8 months ago
Hi all, what i have is a class like this:

namespace srv
{

class section
{
public:

void AddSubSection(section* newSection);

private:

vector<section*> mSubSections;

};

}

void section::AddSubSection(section* newSection)
{
mSubSections.push_back(newSection);     // crash
}
When i call the AddSubSection method i get a crash when the push_back function is called.

section mySection;
section* newSection;

newSection = new(section);

mySection.AddSubSection(newSection);
The program crashes in the push_back function

void push_back(const _Ty& _Val)
{
if (size()<capacity())
....
where it steps into the size function

size_type size() const
{
return (_Mylast-Myfirst);   // here the crash happens.
}
The error message states: Unhandled exception at 0x.... in test.exe: 0xC000000005 I am sitting here since ages and i am getting out of ideas. I found some solutions here in the forum but none of them really helped. If anybody here has a solution i would really appreciate it. Thanks in advance Kande
Advertisement
Have you confirmed this in the debugger? It is quite impossible for that to happen in isolation, and I see nothing in your code that would cause it. I'd suspect something is corrupting the vector at some point. With more code, someone might be able to spot it.
Quote:newSection = new(section);


What is this supposed to be?

While it might create 'new section', it looks more like placement new.
I recommend trying to write the smallest possible program that shows the problem, and then posting it here. That way we can try to reproduce the problem and we have all the data we need to help you. You may also just discover what the problem really is in the process of stripping the program down to its basic elements.
Quote:Original post by Antheus
Quote:newSection = new(section);


What is this supposed to be?

While it might create 'new section', it looks more like placement new.


I was wondering about that, but the following compiles and appears to work:

#include <iostream>int main(){    int *i=new(int)(23);    std::cout << *i << "\n";}


It can't be a placement new since the term in the brackets is a type, so I guess this must just be a peculiarity of the parser that allows the type to be in brackets? Although:

int main(){    new(int);}


throws an error. Weird. [EDIT - only Borland BCC55, VS has no problem with this]

Kande - I believe that 0xC000000005 is normally an indication that you are trying to access invalid memory. Your code that inserts the subsection - is that in a class method or in a free function? If the former, is it possible it is inside a class that has been deleted? It is possible that the point deep in the function stack that you find the error occurs on is the first time in the method that the this pointer is dereferenced.

[Edited by - EasilyConfused on July 30, 2008 4:30:19 PM]
Quote:Original post by EasilyConfused
int main(){    new(int);}


throws an error. Weird.

Hmmm... My compiler (gcc-4.1.0) doesn't complain.

Quote:Original post by Antheus
Quote:newSection = new(section);


What is this supposed to be?

While it might create 'new section', it looks more like placement new.


This is perfectly valid. It is equivalent to 'new section', which creates a new default-constructed section. In the case of 'new(section)' the parentheses are entirely superfluous.
Sounds like the section object you're calling AddSubSection() on is bad. It wouldn't be in the code you posted, is that the exact code you're using or are you simplifying things? 0xC0000005 is an access violation and it's probably occuring when trying to access the member variables of the vector because the section object of which the vector is a member is bad (null pointer, junk pointer, freed memory, etc.). If you look at the 'this' pointer inside size() you'll probably see a bad value, depending on what it is we might be able to give you further clues to where your problem might lie.

Game Programming Blog: www.mattnewport.com/blog

Sorry for answering so late but i havent been home.

Thanks for all the suggestions and answers. I still could not figure out the problem but it must have been a memory problem. I erased my code and started again and it works now.

Thanks to all, cheers
Kande

This topic is closed to new replies.

Advertisement