[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Program crashing if tree is empty

Posted on 2008-11-19
6
Medium Priority
?
182 Views
Last Modified: 2012-05-05
My program crashes when my tree is empty. When i search for a value that is in the tree it finds and displays it fine. Yet if i enter a value that is not in the tree it will crash.

Also my tree gets overwritten after one sucessful search.

What am i doing wrong?

0
Comment
Question by:Prima12
5 Comments
 
LVL 18

Expert Comment

by:Johnjces
ID: 22998163
If you can post some code of how you are searching, that could help out a bunch.

John
0
 

Author Comment

by:Prima12
ID: 22998208
Doh!

All the code that creates the tree and all that stuff is 100% fine and tested. The problems are within these two pieces of code.


function tuneSearching(var t:melodyTree; phraseSearch:string; element:integer):melodyTree;
begin
  if (element <= length(phraseSearch)) then begin
      if(upcase(phraseSearch[element]) ='D') then begin
        tuneSearching:= tuneSearching(t^.downBranch, phraseSearch, element+1);
      end else begin
 
      if(upcase(phraseSearch[element]) = 'U') then begin
        tuneSearching:= tuneSearching(t^.upBranch, phraseSearch, element+1);
      end else begin
 
      if(upcase(phraseSearch[element]) = 'S') then begin
        tuneSearching:= tuneSearching(t^.sameBranch, phraseSearch, element+1);
      end;
      end;
      end;
      end else begin
        tuneSearching:= t;
  end;
end;
 
procedure tuneSearch(var t:MelodyTree; phraseSearch:string);
begin
  phraseSearch:= trim(phraseSearch);  //removes extra whitespacing
  if (length(phraseSearch)>0) then //test the string is not empty
  begin
    t:= tuneSearching(t, phraseSearch, 1);
    writeln('A match was found: phrase ', t^.phrase, ' in the song ', t^.title);
  end else
    writeln('There was no input please try again');
end;

Open in new window

0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 22998684

function tuneSearching(var t:melodyTree; phraseSearch:string; element:integer):melodyTree;
begin
  result := nil; // default
...
... etc...
 
procedure tuneSearch(var t:MelodyTree; phraseSearch:string);
begin
  phraseSearch:= trim(phraseSearch);  //removes extra whitespacing
  if (length(phraseSearch)>0) then //test the string is not empty
  begin
    t:= tuneSearching(t, phraseSearch, 1);
    if assigned(t) then
      writeln('A match was found: phrase ', t^.phrase, ' in the song ', t^.title)
    else
      writeln('A match was not found');
  end
  else
    writeln('There was no input please try again');
end;

Open in new window

0
 
LVL 2

Accepted Solution

by:
Rabmis earned 1500 total points
ID: 23009828
Access violation was fixed in this code
function tuneSearching(var t:melodyTree; phraseSearch:string; element:integer):melodyTree;
begin
  tuneSearching := nil;
  if not assigned(t) then // Check to nil refence
    Exit;
  if (element <= length(phraseSearch)) then begin
      if(upcase(phraseSearch[element]) ='D') then begin
        tuneSearching:= tuneSearching(t^.downBranch, phraseSearch, element+1);
      end else begin
 
      if(upcase(phraseSearch[element]) = 'U') then begin
        tuneSearching:= tuneSearching(t^.upBranch, phraseSearch, element+1);
      end else begin
 
      if(upcase(phraseSearch[element]) = 'S') then begin
        tuneSearching:= tuneSearching(t^.sameBranch, phraseSearch, element+1);
      end;
      end;
      end;
      end else begin
        tuneSearching:= t;
  end;
end;
 
procedure tuneSearch(var t:MelodyTree; phraseSearch:string);
begin
  phraseSearch:= trim(phraseSearch);  //removes extra whitespacing
  if (length(phraseSearch)>0) then //test the string is not empty
  begin
    t:= tuneSearching(t, phraseSearch, 1);
    if assigned(t) then
      writeln('A match was found: phrase ', t^.phrase, ' in the song ', t^.title)
    else
      writeln('A match was not found');
  end else
    writeln('There was no input please try again');
end;

Open in new window

0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 23026324
I believe my solution stopped the crash and is still valid.
Just because you "dont need an answer to this question anymore" does not mean the answers are wrong...
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month19 days, 13 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question