473,464 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Release build seg faults at vector.push_back above certain size, b

hi everyone.

i'm currently experiencing a strange problem under vc++ 2005 express. i hope
someone has a hint for me, i'm kind of lost atm.

i'm using a vectors of pointers in my code.
using the release build i can add and remove elements aslong as i stay below
a certain vector size (13 in this case, no joke). as soon as the vector tries
to add element 14 i get a runtime error.

the whole thing get's really strange when i use the debug build. i can add
as much elements as i want without any problems (tried above 500). but that's
not all. it works with the debug build aslong as i start from the IDE or if i
doubleclick the debug.exe. it will crash with the same error as the release
build when i start it from a cmd prompt (just one push_back earlier)

i have no clue what to do now. i can't debug cause the debug build works.
inserting some debug msgs show that vector.push_back() seems to trigger the
crash...

suggestions more than welcome
Nov 17 '05 #1
4 1556
On Wed, 16 Mar 2005 08:15:05 -0800, "whocares"
<wh******@discussions.microsoft.com> wrote:
hi everyone.

i'm currently experiencing a strange problem under vc++ 2005 express. i hope
someone has a hint for me, i'm kind of lost atm.

i'm using a vectors of pointers in my code.
using the release build i can add and remove elements aslong as i stay below
a certain vector size (13 in this case, no joke). as soon as the vector tries
to add element 14 i get a runtime error.

the whole thing get's really strange when i use the debug build. i can add
as much elements as i want without any problems (tried above 500). but that's
not all. it works with the debug build aslong as i start from the IDE or if i
doubleclick the debug.exe. it will crash with the same error as the release
build when i start it from a cmd prompt (just one push_back earlier)

i have no clue what to do now. i can't debug cause the debug build works.
inserting some debug msgs show that vector.push_back() seems to trigger the
crash...

suggestions more than welcome


Running a debugger introduces extra data and code pages into your
process. So while the debugger is active, you're probably overwriting
some non-critical debug information, or reading some debug info or
code using invalid pointers.

My initial guess is that you're releasing a pointer while it's still
part of a vector and it's being reaccessed later.

To help during the build, set the warning level to 4 (/W4) and define
STRING (-DSTRICT) to detect some coding errors. Do not ignore any
warning unless you understand *exactly* why it can be ignored.

For running (debug or not), be sure all run-time checks are enabled.

Two other things that may help:

1) ero any pointer after you release it:

delete[] ptr;
ptr = NULL;

2) When declaring pointers, always initialize them to null:

OBJECT *ptr = NULL;

Good luck!

--
Sev
Nov 17 '05 #2
hi sev,

thx for your suggestions.
Running a debugger introduces extra data and code pages into your
process. So while the debugger is active, you're probably overwriting
some non-critical debug information, or reading some debug info or
code using invalid pointers.
ok, that explains why it works in debug mode.
My initial guess is that you're releasing a pointer while it's still
part of a vector and it's being reaccessed later.


i do remove elements from the vector if some conditions are met. but the
error even occurs if i add more than 13 items to the vector _before_ anything
is removed. no changes are made to the vector but adding items.

the (pseudo) code that adds a pointer to the vector:

funtion addObject()
{
ClassA *ptr = NULL;
ptr = new ClassA;

ptr->someFunction();

vector.push_back( ptr );

ptr = NULL;
}

i don't know what could be going wrong with this code.
is it possible that the push_back() fails with more than x elements because
it's trying to access memory i f**d up with some buggy code earlier?

Nov 17 '05 #3
"whocares" <wh******@discussions.microsoft.com> wrote in message
news:D0**********************************@microsof t.com...
....
funtion addObject()
{
ClassA *ptr = NULL;
ptr = new ClassA;

ptr->someFunction();

vector.push_back( ptr );

ptr = NULL;
}

i don't know what could be going wrong with this code.
That code is fine, at least insofar as it is shown.
(Anything could happen during that member call.)
is it possible that the push_back() fails with more than x elements because
it's trying to access memory i f**d up with some buggy code earlier?


That is highly possible. Whenever you see
behavioral changes under circumstance that
should not change the behavior, you should
be thinking "Oh oh, undefined behavior!
Where have I clobbered something that
should have been left alone, used an object
not properly intialized, or abused the heap?"

You should consider yourself fortunate that
the problem shows up while debugging
rather than only when not debugging.

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Nov 17 '05 #4
i didn't find the particular error yet, but by taking out some of the
routines that might corrupt the heap i got back to normal behaviour.

thx for your help guys.
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Steve | last post by:
Hi, I'm using a std::vector to store a list of user defined objects. The vector may have well over 1000 elements, and I'm suffering a performance hit. If I use push_back I get a much worse...
7
by: William Payne | last post by:
(This post is related to "recent files menu"-post below. If I should have kept this in that thread, I apologise.) Hello, I have a function that adds a std::string to a std::vector. New entries are...
6
by: Joe | last post by:
I have a: vector<string> which contains a few dozen elements. I want to find the index of the element containing a certain string. for example: vector<string> strings;...
3
by: Daniel J Watkins | last post by:
Hi, Some runtime memory exceptions are being exhibited with some code I've written. Can you clarify the following with you to see if my understanding of the principles under question are...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
9
by: Jess | last post by:
Hello, I tried to clear a vector "v" using "v.clear()". If "v" contains those objects that are non-built-in (e.g. string), then "clear()" can indeed remove all contents. However, if "v"...
6
by: Jia | last post by:
Hi all, I have a class foo which has a static vector of pointers of type base class, and a static function to set this vector. #include <iostream> #include <vector> using namespace std;...
6
by: hsmit.home | last post by:
Hello, I came across a strange error and it's really been bugging me. Maybe someone else has come across this and any insight would be appreciated. What I'm trying to accomplish is using...
3
by: Ramon F Herrera | last post by:
Newbie alert: I come from C programming, so I still have that frame of mind, but I am trying to "Think in C++". In C this problem would be solved using unions. Hello: Please consider the...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.