Connecting Tech Pros Worldwide Help | Site Map

inexplicable System.NullReferenceException when using stl vector in managed class

razilon
Guest
 
Posts: n/a
#1: May 25 '06
Hi,

I've written a managed class that makes use of stl vectors of a few
unmanaged structs for data handling/manipulation, but I'm getting a few
very strange errors. I get an

"Unhandled Exception: System.NullReferenceException: Object reference
not set to an instance of an object" occasionally when adding a new
element to a vector. By

occasionally I mean that the exact same code works fine most of the
time, throwing the error around 1% of the time. Based on sample data
that I'm feeding the program, the

errors occur on the exact same data every execution, but I don't see
anything wrong with the offending data or the code. Can someone help?
Even if you only have a vague

idea of something I can look into, please let me know.

Here's the relevant code snippet:

// if a headline occurs after a normal paragraph, split the block
there
for (int i = thisDoc->blocks.size() - 1; i >= 0; i--)
{
block* curBlock = &thisDoc->blocks[i];
if (curBlock->type == blockType::text)
{
// search through all the paragraphs (from end to start)
bool lastWasHeadline = false;
for (int j = curBlock->paragraphs.size() - 1; j >= 0; j--)
{
// is this paragraph a headline?
if (curBlock->paragraphs[j].type == paragraphType::headline)
lastWasHeadline = true;
else
{
// did we just leave a headline?
if (lastWasHeadline)
{
// split the remainder of the block into a new block
block newBlock;
newBlock.type = blockType::text;
for (int k = curBlock->paragraphs.size() - 1; k > j; k--)
{
newBlock.paragraphs.insert(newBlock.paragraphs.beg in(),
curBlock->paragraphs[k]);
curBlock->removeParagraph(k);
}
//!!!!!!!!!!!!!!!!!!ERROR!!!!!!!!!!!!!!!!!!
//Unhandled Exception: System.NullReferenceException: Object
reference not set to an instance of an object
thisDoc->addBlock(newBlock);
//!!!!!!!!!!!!!!!!!!ERROR!!!!!!!!!!!!!!!!!!
}
lastWasHeadline = false;
}
}
}
}

[color=blue]
>From the class header (to show you what my struct declarations look[/color]
like):

__nogc struct block
{
rectangle bounds;
rectangle adjacent;
rectangle gutterDist;
int type;
vector<paragraph> paragraphs;

block()
{
bounds.left = bounds.right = bounds.top = bounds.bottom = 0;
adjacent.left = adjacent.right = adjacent.top = adjacent.bottom =
-1;
gutterDist.left = gutterDist.right = gutterDist.top =
gutterDist.bottom = 2147483647;
type = blockType::undecided;
paragraphs.clear();
}

~block() {paragraphs.clear();}

void addParagraph(paragraph newParagraph)
{
// if this is the first line to be added to the paragraph
if (paragraphs.size() == 0)
{
bounds.left = newParagraph.bounds.left;
bounds.right = newParagraph.bounds.right;
bounds.top = newParagraph.bounds.top;
bounds.bottom = newParagraph.bounds.bottom;
}
else
{
if (newParagraph.bounds.left < bounds.left)
bounds.left = newParagraph.bounds.left;
if (newParagraph.bounds.right > bounds.right)
bounds.right = newParagraph.bounds.right;
if (newParagraph.bounds.top < bounds.top)
bounds.top = newParagraph.bounds.top;
if (newParagraph.bounds.bottom > bounds.bottom)
bounds.bottom = newParagraph.bounds.bottom;
}
paragraphs.push_back(newParagraph);
}

void removeParagraph(int index)
{
paragraphs.erase(paragraphs.begin() + index);
}
};

__nogc struct page
{
int width, height, resolution;
vector<block> blocks;
double avgLineHeight; // calculated in reFormat(...)

page()
{
width = height = resolution = 0;
blocks.clear();
}

~page() {blocks.clear();}

void addBlock(block newBlock) {blocks.push_back(newBlock);}

void removeBlock(int index)
{
blocks.erase(blocks.begin() + index);
}
};

// all necessary internal variables
page __nogc* thisDoc;

Guest
 
Posts: n/a
#2: May 26 '06

re: inexplicable System.NullReferenceException when using stl vector in managed class


you might find some clue from the following posts:

http://msdn.microsoft.com/msdnmag/issues/06/04/PureC/
How Templates and Generics Work Together

http://www.codeguru.com/columns/kate...le.php/c10297/
STL.NET: Combining Generics and Templates


"razilon" <razilon@hotmail.com> wrote in message
news:1148535687.777378.211380@j73g2000cwa.googlegr oups.com...[color=blue]
> Hi,
>
> I've written a managed class that makes use of stl vectors of a few
> unmanaged structs for data handling/manipulation, but I'm getting a few
> very strange errors. I get an
>
> "Unhandled Exception: System.NullReferenceException: Object reference
> not set to an instance of an object" occasionally when adding a new
> element to a vector. By
>
> occasionally I mean that the exact same code works fine most of the
> time, throwing the error around 1% of the time. Based on sample data
> that I'm feeding the program, the
>
> errors occur on the exact same data every execution, but I don't see
> anything wrong with the offending data or the code. Can someone help?
> Even if you only have a vague
>
> idea of something I can look into, please let me know.
>
> Here's the relevant code snippet:
>
> // if a headline occurs after a normal paragraph, split the block
> there
> for (int i = thisDoc->blocks.size() - 1; i >= 0; i--)
> {
> block* curBlock = &thisDoc->blocks[i];
> if (curBlock->type == blockType::text)
> {
> // search through all the paragraphs (from end to start)
> bool lastWasHeadline = false;
> for (int j = curBlock->paragraphs.size() - 1; j >= 0; j--)
> {
> // is this paragraph a headline?
> if (curBlock->paragraphs[j].type == paragraphType::headline)
> lastWasHeadline = true;
> else
> {
> // did we just leave a headline?
> if (lastWasHeadline)
> {
> // split the remainder of the block into a new block
> block newBlock;
> newBlock.type = blockType::text;
> for (int k = curBlock->paragraphs.size() - 1; k > j; k--)
> {
> newBlock.paragraphs.insert(newBlock.paragraphs.beg in(),
> curBlock->paragraphs[k]);
> curBlock->removeParagraph(k);
> }
> //!!!!!!!!!!!!!!!!!!ERROR!!!!!!!!!!!!!!!!!!
> //Unhandled Exception: System.NullReferenceException: Object
> reference not set to an instance of an object
> thisDoc->addBlock(newBlock);
> //!!!!!!!!!!!!!!!!!!ERROR!!!!!!!!!!!!!!!!!!
> }
> lastWasHeadline = false;
> }
> }
> }
> }
>
>[color=green]
>>From the class header (to show you what my struct declarations look[/color]
> like):
>
> __nogc struct block
> {
> rectangle bounds;
> rectangle adjacent;
> rectangle gutterDist;
> int type;
> vector<paragraph> paragraphs;
>
> block()
> {
> bounds.left = bounds.right = bounds.top = bounds.bottom = 0;
> adjacent.left = adjacent.right = adjacent.top = adjacent.bottom =
> -1;
> gutterDist.left = gutterDist.right = gutterDist.top =
> gutterDist.bottom = 2147483647;
> type = blockType::undecided;
> paragraphs.clear();
> }
>
> ~block() {paragraphs.clear();}
>
> void addParagraph(paragraph newParagraph)
> {
> // if this is the first line to be added to the paragraph
> if (paragraphs.size() == 0)
> {
> bounds.left = newParagraph.bounds.left;
> bounds.right = newParagraph.bounds.right;
> bounds.top = newParagraph.bounds.top;
> bounds.bottom = newParagraph.bounds.bottom;
> }
> else
> {
> if (newParagraph.bounds.left < bounds.left)
> bounds.left = newParagraph.bounds.left;
> if (newParagraph.bounds.right > bounds.right)
> bounds.right = newParagraph.bounds.right;
> if (newParagraph.bounds.top < bounds.top)
> bounds.top = newParagraph.bounds.top;
> if (newParagraph.bounds.bottom > bounds.bottom)
> bounds.bottom = newParagraph.bounds.bottom;
> }
> paragraphs.push_back(newParagraph);
> }
>
> void removeParagraph(int index)
> {
> paragraphs.erase(paragraphs.begin() + index);
> }
> };
>
> __nogc struct page
> {
> int width, height, resolution;
> vector<block> blocks;
> double avgLineHeight; // calculated in reFormat(...)
>
> page()
> {
> width = height = resolution = 0;
> blocks.clear();
> }
>
> ~page() {blocks.clear();}
>
> void addBlock(block newBlock) {blocks.push_back(newBlock);}
>
> void removeBlock(int index)
> {
> blocks.erase(blocks.begin() + index);
> }
> };
>
> // all necessary internal variables
> page __nogc* thisDoc;
>[/color]


Closed Thread